Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
SIGNAL
UNIX SYSTEM
PROGRAMMING
 A signal is used to notify a process that a particular
event has occurred.
 Signals are software Interrupts
 A Signal ...
Linux Signals
 In Linux, every signal has a name that begins with
characters SIG.
 A SIGINT signal that is generated whe...
Signal Handling
 Every signal may be handled by one of two possible
handlers.
 A default signal handler
 A user-defined...
User-defined signal handler
 Every signal has a default signal handler that is run
by the kernel when handling that signa...
SIGSEGV
SIGINT
SIGALRM
SIGALRM
 #include <signal.h>
 void alarmhandler(int a)
 {
 printf("TIME OVER n");
 exit(0);
 }
 int main()
 {
 in...
KILL - send signal to a process
 void handler(int a)
 {
 printf(" %d n" ,a);
 }
 int main()
 {
 signal(SIGALRM , ha...
SIGSEGV
 int main()
 {
 int x = 5 ;
 signal(SIGSEGV , myfunc);
 printf("Enter a number : n");
 scanf(" %d",x);
 pri...
SIGCHLD – Indicates the death of a child
process.
Assignment
 Implement wait using SIGCHILD
Ignoring a Signal SIG_IGN
 void myhandler(int a)
 {
 printf("ENTERED MYHANDLER n");
 printf("signal recieved : %d n",a...
Signal
Signal
Signal
Signal
Upcoming SlideShare
Loading in …5
×

Signal

148 views

Published on

Linux Signals

Published in: Education
  • Be the first to comment

Signal

  1. 1. SIGNAL UNIX SYSTEM PROGRAMMING
  2. 2.  A signal is used to notify a process that a particular event has occurred.  Signals are software Interrupts  A Signal may be received synchronously or asynchronously.
  3. 3. Linux Signals  In Linux, every signal has a name that begins with characters SIG.  A SIGINT signal that is generated when a user presses ctrl+c. This is the way to terminate programs from terminal.  A SIGFPE is generated when the divide by zero occurs.
  4. 4. Signal Handling  Every signal may be handled by one of two possible handlers.  A default signal handler  A user-defined signal handler
  5. 5. User-defined signal handler  Every signal has a default signal handler that is run by the kernel when handling that signal.  The default action can be overridden by a user- defined signal handler that is called to handle the signal.  Provide the handler function which has the syntax  void handler_name(int signo);  Register the signal handler with the signal  Signal(Signal_Number , handler_name);
  6. 6. SIGSEGV
  7. 7. SIGINT
  8. 8. SIGALRM
  9. 9. SIGALRM  #include <signal.h>  void alarmhandler(int a)  {  printf("TIME OVER n");  exit(0);  }  int main()  {  int i;  signal(SIGALRM , alarmhandler);  alarm(5);  for(i = 0 ; i < 10 ; i++)  {  printf(" %dn" , i);  sleep(1);  }  printf("END OF PROGRAM n");  }
  10. 10. KILL - send signal to a process  void handler(int a)  {  printf(" %d n" ,a);  }  int main()  {  signal(SIGALRM , handler);  signal(SIGSEGV , handler);  signal(SIGFPE , handler);  #ifdef SEGV  kill(getpid() , SIGSEGV);  #endif  #ifdef FPE  kill(getpid() , SIGFPE);  #endif  }
  11. 11. SIGSEGV  int main()  {  int x = 5 ;  signal(SIGSEGV , myfunc);  printf("Enter a number : n");  scanf(" %d",x);  printf("The number is %d",x );  }  void myfunc(int a)  {  printf("ENTERED MYHANDLER n");  printf("signal recieved : %d n",a);  exit(1);  }
  12. 12. SIGCHLD – Indicates the death of a child process.
  13. 13. Assignment  Implement wait using SIGCHILD
  14. 14. Ignoring a Signal SIG_IGN  void myhandler(int a)  {  printf("ENTERED MYHANDLER n");  printf("signal recieved : %d n",a);  exit(1);  }  int main()  {  int x = 0;  printf("1. Default 2. Ignore 3. Handler n");  scanf(" %d",&x);  switch(x)  {  case 1 : signal(SIGINT , SIG_DFL); break;  case 2 : signal(SIGINT , SIG_IGN); break;  case 3 : signal(SIGINT , myhandler); break;  }  sleep(5);  printf("END OF PROGRAM ");  }

×