Saturday, October 11, 2014

Unix Prog: Unreliable Signals

1. Unreliable Signals

In earlier versions of UNIX system, signals were unreliable: a signal could occur and the process would never know about it.

The problem with unreliable signal:
Whenever the signal handler catches the signal, the signal was reset to its default each time the signal occurred.

Example:
If the signal is caught by handler "sigint", at this time the signal was reset to the default. Then inside the "sigint", it is going to re-setup the signal handler. But if right before calling the "signal" system call again, the second SIGINT occurs, then it will cause kernel run the default action -- terminate the process.

 int sigint(int signo)  
 {  
   signal(SIGINT, sigint);  
   ......  
 }  

2. Problem with turning off unreliable signal

The program set up the signal handler for SIGINT, and then go to pause(), while waiting for the SIGINT coming.
But if the SIGINT comes between "checking the sig_int_flag" and the "pasue()" call, then the process will sleep forever.
 int sig_int_flag  
   
 void sig_int(int signo)  
 {  
   signal(SIGINT, sig_int);  
   sig_int_flag = 1;  
 }  
   
 int main()  
 {  
  ......  
  signal(SIGINT, sig_int);  
  ......  
  while(sig_int_flag == 0)  
    pause();  
  ......  
 }  

No comments:

Post a Comment