الدرس الخامس: Real-Time Signals and Interrupt Handling

سلسلة من الدروس في الـ POSIX

الدرس الخامس: Real-Time Signals and Interrupt Handling

RT Signals:

int pthread_kill(pthread_t thread, int signal);

sigset_t *set

int sigemptyset(sigset_t *set);

int sigfillset(sigset_t *set);

int sigaddset(sigset_t *set, int signo);

int sigdelset(sigset_t *set, int signo);

int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset);

int sigwait(const sigset_t *set, int *signal);

int sigaction(int sig, const struct sigaction *reaction, struct sigaction *old_reaction);

struct sigaction {

void (*sa_handler)();

int sa_flags;

sigset_t sa_mask; };

#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

#include <signal.h>

void signal_handler(int signo)

{

int t;

printf(“In signal’s handler,signo#%d\n”,signo);

t = sleep(2);

if (t != 0)

printf(“Interruted after %d sec\n”,t);

else

printf(“Not interrupted\n”);

}

void *threadfunc(void *param)

{

int *tid;

int t;

tid = (int *)param;

printf(“Hello I am thread %d and I am waiting for a signal!\n”,tid);

t = sleep(10);

if (t != 0)

printf(“I got a signal after %d sec\n”,t);

else

printf(“i did’t get a signal\n”);

return NULL;

}

int main (int argc, char argv[])

{

int i;

pthread_t threads[3];

struct sigaction actions;

sigemptyset(&actions.sa_mask);

sigaddset(&actions.sa_mask, 36);

actions.sa_flags = 0;

actions.sa_handler = signal_handler;

for(i=0; i<3; i++)

pthread_create(&threads[i], NULL, threadfunc, (void *)i);

sleep(1);

sigaction(38,&actions,NULL);

for(i=0; i<3; i++)

pthread_kill(threads[i],38);

sleep(1);

sigaction(36,&actions,NULL);

for(i=0; i<3; i++)

pthread_kill(threads[i],36);

sleep(2);

sigaction(34,&actions,NULL);

for(i=0; i<3; i++)

pthread_kill(threads[i],34);

for(i=0; i<3; i++)

pthread_join(threads[i], NULL);

return 0;

}

#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

#include <signal.h>

void signal_handler(int signo)

{

int t;

printf(“In signal’s handler,signo#%d\n”,signo);

t = sleep(2);

if (t != 0)

printf(“Interruted after %d sec\n”,t);

else

printf(“Not interrupted\n”);

}

void *threadfunc(void *param)

{

int SignalNumber;

sigset_t SignalSet;

sigemptyset(&SignalSet);

sigaddset(&SignalSet,34);

pthread_sigmask(SIG_BLOCK, &SignalSet, NULL);

printf(“I will wait signal#34\n”);

sigwait(&SignalSet, &SignalNumber);

printf(“I got signal#%d\n”,SignalNumber);

return NULL;

}

int main (int argc, char argv[])

{

int i;

pthread_t thread;

struct sigaction actions;

sigemptyset(&actions.sa_mask);

actions.sa_flags = 0;

actions.sa_handler = signal_handler;

pthread_create(&thread, NULL, threadfunc, NULL);

//sleep(1);

sigaction(36,&actions,NULL);

pthread_kill(thread,36);

sigaction(34,&actions,NULL);

pthread_kill(thread,34);

pthread_join(thread, NULL);

return 0;

}

حل وظيفة الجلسة السابقة:

Wings Process:

/*

* Wings_Process.c

*

* Created on: Mar 16, 2012

* Author: zain

*/

#include<stdio.h>

#include<stdlib.h>

#include<semaphore.h>

#include<mqueue.h>

#include<fcntl.h>

float readWings()

{

return 10 *(rand() / (RAND_MAX + 1.0));

}

float readHeight()

{

return 10 *(rand() / (RAND_MAX + 1.0));

}

int main(int argc, char argv[])

{

float wings_v,height_v,threshold;

mqd_t plane_q;

char *msgptr;

size_t msglen;

int priority = 10;

sem_t *sem_height;

plane_q = mq_open(“/qplane”,O_WRONLY);

sem_height = sem_open(“/height_sensor”,0);

if(-1 != plane_q)

{

while(1)

{

wings_v = readWings();

sem_wait(sem_height);

height_v = readHeight();

sem_post(sem_height);

threshold = wings_v + height_v;

if(threshold > 10 && threshold < 15)

{

msglen = 2;

msgptr = “DH”;

if(mq_send(plane_q,msgptr,msglen,priority) != 0)

printf(“Error in sending!\n”);

}

else if(threshold > 15)

{

msglen = 2;

msgptr = “SS”;

if(mq_send(plane_q,msgptr,msglen,priority) != 0)

printf(“Error in sending!\n”);

}

sleep(1);

}

}

else printf(“Error in opening queue!\n”);

mq_unlink(“/qplane”);

mq_close(plane_q);

sem_unlink(“/height_sensor”);

sem_close(sem_height);

return 0;

}

Oxygen Process:

/*

* Oxygen_Process.c

*

* Created on: Mar 16, 2012

* Author: zain

*/

#include<stdio.h>

#include<stdlib.h>

#include<semaphore.h>

#include<mqueue.h>

#include<fcntl.h>

float readOxygen()

{

return 10 *(rand() / (RAND_MAX + 1.0));

}

float readHeight()

{

return 10 *(rand() / (RAND_MAX + 1.0));

}

int main(int argc, char argv[])

{

float oxygen_v,height_v,threshold;

mqd_t plane_q;

char *msgptr;

size_t msglen;

int priority = 5;

sem_t *sem_height;

plane_q = mq_open(“/qplane”,O_WRONLY);

sem_height = sem_open(“/height_sensor”,0);

if(-1 != plane_q)

{

while(1)

{

oxygen_v = readOxygen();

sem_wait(sem_height);

height_v = readHeight();

sem_post(sem_height);

threshold = oxygen_v + height_v;

if(threshold < 5)

{

msglen = 2;

msgptr = “UP”;

if(mq_send(plane_q,msgptr,msglen,priority) != 0)

printf(“Error in sending!\n”);

}

else if(threshold > 15)

{

msglen = 4;

msgptr = “DOWN”;

if(mq_send(plane_q,msgptr,msglen,priority) != 0)

printf(“Error in sending!\n”);

}

sleep(5);

}

}

else printf(“Error in opening queue!\n”);

mq_unlink(“/qplane”);

mq_close(plane_q);

sem_unlink(“/height_sensor”);

sem_close(sem_height);

return 0;

}

Temperature Process:

/*

* Temp_Process.c

*

* Created on: Mar 16, 2012

* Author: zain

*/

#include<stdio.h>

#include<stdlib.h>

#include<semaphore.h>

#include<mqueue.h>

#include<fcntl.h>

float readTemp()

{

return 10 *(rand() / (RAND_MAX + 1.0));

}

float readHeight()

{

return 10 *(rand() / (RAND_MAX + 1.0));

}

int main(int argc, char argv[])

{

float temp_v,height_v,threshold;

mqd_t plane_q;

char *msgptr;

size_t msglen;

int priority = 1;

sem_t *sem_height;

plane_q = mq_open(“/qplane”,O_WRONLY);

sem_height = sem_open(“/height_sensor”,0);

if(-1 != plane_q)

{

while(1)

{

temp_v = readTemp();

sem_wait(sem_height);

height_v = readHeight();

sem_post(sem_height);

threshold = temp_v + height_v;

if(threshold < 5)

{

msglen = 2;

msgptr = “UP”;

if(mq_send(plane_q,msgptr,msglen,priority) != 0)

printf(“Error in sending!\n”);

}

else if(threshold > 15)

{

msglen = 4;

msgptr = “DOWN”;

if(mq_send(plane_q,msgptr,msglen,priority) != 0)

printf(“Error in sending!\n”);

}

sleep(10);

}

}

else printf(“Error in opening queue!\n”);

mq_unlink(“/qplane”);

mq_close(plane_q);

sem_unlink(“/height_sensor”);

sem_close(sem_height);

return 0;

}

Central Process:

/*

* Central_Process.c

*

* Created on: Mar 11, 2012

* Author: zain

*/

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<pthread.h>

#include<semaphore.h>

#include<mqueue.h>

#include<fcntl.h>

#define QueueMaxSize 6

#define MessageMaxSize 4

void *wingshandler(void *i_msg)

{

char *msg;

msg = (char *) i_msg;

if(strcmp(msg,”SS “) == 0)

printf(“SS\n”);

else if (strcmp(msg,”DH “) == 0)

printf(“DH\n”);

else

printf(“Unknown Messgae!\n”);

pthread_exit(NULL);

}

void *oxygenhandler(void *i_msg)

{

char *msg;

msg = (char *) i_msg;

if(strcmp(msg,”UP “) == 0)

printf(“UP_OXYGEN\n”);

else if (strcmp(msg,”DOWN “) == 0)

printf(“DOWN_OXYGEN\n”);

else

printf(“Unknown Messgae!\n”);

pthread_exit(NULL);

}

void *temphandler(void *i_msg)

{

char *msg;

msg = (char *) i_msg;

if(strcmp(msg,”UP “) == 0)

printf(“UP_TEMP\n”);

else if (strcmp(msg,”DOWN “) == 0)

printf(“DOWN_TEMP\n”);

else

printf(“Unknown Messgae!\n”);

pthread_exit(NULL);

}

int main(int argc, char *argv[])

{

/* Message qeue parameters */

mqd_t plane_q;

struct mq_attr q;

q.mq_msgsize = MessageMaxSize;

q.mq_maxmsg = QueueMaxSize;

char msgptr[5];

size_t msglen = 5;

int msgsize, ret;

int priority;

/* Signal attributes */

struct sigevent sev;

sev.sigev_notify = SIGEV_SIGNAL;

sev.sigev_signo = 32;

/* Threads */

pthread_t wings,oxygen,temp;

/* Semaphore */

sem_t *sem_height;

plane_q = mq_open(“/qplane”,O_RDONLY|O_CREAT,0666,&q);

sem_height = sem_open(“/height_sensor”,O_CREAT,0666,1);

if(-1 != plane_q)

{

while(1)

{

mq_getattr(plane_q, &q);

if(q.mq_curmsgs > 0)

{

strcpy(msgptr,” “);

if(msgsize = mq_receive(plane_q,msgptr,msglen,&priority) > 0)

{

if(priority == 10)

{

pthread_create(&wings,NULL,wingshandler,(void *)msgptr);

continue;

}

else if(priority == 5)

{

pthread_create(&oxygen,NULL,oxygenhandler,(void *)msgptr);

continue;

}

else if(priority == 1)

{

pthread_create(&temp,NULL,temphandler,(void *)msgptr);

continue;

}

else

{

printf(“Unknown Message !\n”);

continue;

}

}

else printf(“Error in Receiving!\n”);

}

else

{

printf(“Queue is empty,waiting!\n”);

mq_notify(plane_q,&sev);

sleep(100);

}

}

}

else printf(“Error!\n”);

mq_unlink(“/qplane”);

mq_close(plane_q);

sem_unlink(“/height_sensor”);

sem_close(sem_height);

return 0;

}

Flush:

/*

* flush.c

*

* Created on: Mar 16, 2012

* Author: zain

*/

#include<stdio.h>

#include<stdlib.h>

#include<semaphore.h>

#include<mqueue.h>

int main()

{

mqd_t plane_q;

sem_t *sem_height;

plane_q = mq_open(“/qplane”,O_WRONLY);

sem_height = sem_open(“/height_sensor”,0);

mq_unlink(“/qplane”);

mq_close(plane_q);

sem_unlink(“/height_sensor”);

sem_close(sem_height);

return 0;

}

About زين العابدين

مهندس حواسيب - معهد IDA - جامعة Braunshweig التقنية.
هذا المنشور نشر في دروس تعليمية وكلماته الدلالية , , , , , . حفظ الرابط الثابت.

رد واحد على الدرس الخامس: Real-Time Signals and Interrupt Handling

  1. يقول salim hamad:

    لو سمحت يا مهندس عاوز السيمافور هادي بلغة باسكال

أضف تعليقاً

إملأ الحقول أدناه بالمعلومات المناسبة أو إضغط على إحدى الأيقونات لتسجيل الدخول:

WordPress.com Logo

أنت تعلق بإستخدام حساب WordPress.com. تسجيل خروج   / تغيير )

صورة تويتر

أنت تعلق بإستخدام حساب Twitter. تسجيل خروج   / تغيير )

Facebook photo

أنت تعلق بإستخدام حساب Facebook. تسجيل خروج   / تغيير )

Google+ photo

أنت تعلق بإستخدام حساب Google+. تسجيل خروج   / تغيير )

Connecting to %s