Sigaction act struct sigaction oact replaces the old

This preview shows page 14 - 20 out of 20 pages.

sigaction *act, struct sigaction *oact); Replaces the old signal() function. Used to bind a handler to a signal. Handler should match either void handler(int); void handler(int, siginfo_t *info, ucontext_t *uap); The handler function should match this prototype if the SA_SIGINFO bit is set in flags. It then should be pointed to by the sa_sigaction member of struct sigaction.
Image of page 14

Subscribe to view the full document.

#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h> #include <signal.h> #include <sys/types.h> #include <sys/signal.h> void *functionC(void *ptr); void handler (int status); /* definition of signal handler */ int counter = 0; main() { int rc1, rc2; pthread_t thread1, thread2; // First set up the signal handler struct sigaction sigold, signew; signew.sa_handler=handler; sigemptyset(&signew.sa_mask); sigaddset(&signew.sa_mask,SIGINT); signew.sa_flags = SA_RESTART; sigaction(SIGINT,&signew,&sigold); /* Create independent threads each of which will execute functionC */ if( (rc1=pthread_create( &thread1, NULL, &functionC, (void *)1)) ) { printf("Thread creation failed: %d\n", rc1); } if( (rc2=pthread_create( &thread2, NULL, &functionC, (void *)2)) ) { printf("Thread creation failed: %d\n", rc2); } printf("I am parent thread %d[%u]\n", 0,(unsigned int)pthread_self()); pthread_join( thread1, NULL); pthread_join( thread2, NULL); } void handler (int status) { printf("%u received signal %d\n", (unsigned int)pthread_self(), status); } void *functionC(void *ptr) { int thnum = (int)ptr; for(;;) { sleep(1); counter++; // Note: This should be protected by semaphores printf("I am thread %d[%u] counter %d\n",thnum,(unsigned int)pthread_self(), counter); } }
Image of page 15
Signal Handlers To get more information //void handler (int status, siginfo_t *info, ucontext_t *uap); void handler (int status, struct __siginfo *info, void *uap); ……. // First set up the signal handler struct sigaction sigold, signew; signew.sa_sigaction=handler; sigemptyset(&signew.sa_mask); sigaddset(&signew.sa_mask,SIGINT); signew.sa_flags = SA_RESTART | SA_SIGINFO; sigaction(SIGINT,&signew,&sigold); ……. //void handler (int status, siginfo_t *info, ucontext_t *uap) void handler (int status, struct __siginfo *info, void *uap) { printf("[%d]%u received signal %d from %d\n", getpid(), (unsigned int)pthread_self(), status, info->si_pid); }
Image of page 16

Subscribe to view the full document.

Sending a Signal int kill(pid_t pid, int sig); Causes the signal to be delivered to another process Example: kill(pID,SIGKILL); Signals are defined in /usr/include/sys/signal.h Example usage: kill the child you execed if it runs too long How do we tell if a child runs too long?
Image of page 17
Timers int getitimer(int which, struct itimerval *value); int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue); Provide microsecond granularity Will send SIGALRM every interval struct itimerval { struct timeval it_interval; /* next value */ struct timeval it_value; /* current value */ }; struct timeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */ };
Image of page 18

Subscribe to view the full document.

What have your kids been up to?
Image of page 19
Image of page 20
You've reached the end of this preview.
  • Winter '08
  • Clement,M
  • Exit, Parent process, int status, int stat

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern