DevX10
DevX10

Reputation: 505

Custom Signal Handler in C

Consider the following chunk of C code:

void TERMHandler(int sig){
 signal (sig, SIG_DFL);
}

main() { 
 pid_t pid;
 pid = fork()

if (pid == 0) { 
     signal(SIGTERM,TERMHandler);
     while(1); 
  }
else
  {  
   sleep(3);
   kill(pid,SIGTERM);
   sleep(3);
   kill(pid,SIGTERM);
  }
}

We create a new process and distinguish between child (pid = 0) and parent.

Can a custom handler be used for every type of signals? If so, assuming we create a custom handler, is it right that there wouldn't be any difference between all signals if I only use the signal once (or never reset the signal handler), since it would just execute my handler without considering the signal in the function?

What I'm trying to say is, is it right that:

 signal(SIGTERM,CustomHandler);
 signal(SIGTSTP,CustomHandler);
 signal(SIGHUP,CustomHandler);
 ...

will execute the same code when the parent runs kill(pid, SomeSignal)?

Upvotes: 1

Views: 7253

Answers (2)

pilcrow
pilcrow

Reputation: 58544

Can a custom handler be used for every type of signals?

Yes. You can install a custom "signal-catching" function for all signals which can be caught. (For example, SIGKILL and SIGSTOP may not be caught.)

[I]s it right that there wouldn't be any difference between all signals if I only use the signal once (or never reset the signal handler), since it would just execute my handler without considering the signal in the function?

That depends on how you code your signal catching function. The system will pass the caught signal to the function, so the same function could do something different upon catching a SIGTERM rather than a SIGHUP, for instance. If your handler ignores its sig argument and ignores the signal environment generally (masks, stacks, dispositions), then, yes, each invocation would be like any other.

Upvotes: 2

John Bollinger
John Bollinger

Reputation: 180201

Can a custom handler be used for every type of signals?

Yes, the same custom signal-handler function can be registered to handle different types of signals, up to and including all the signals that can be caught on the system in question. Note, however, that there may be defined signals that cannot be caught. On POSIX-conforming systems, for example, SIGKILL and SIGSTOP have this property.

If so, assuming we create a custom handler, is it right that there wouldn't be any difference between all signals if I only use the signal once (or never reset the signal handler), since it would just execute my handler without considering the signal in the function?

The signal handler function is not obligated to consider the signal number in determining what to do. It can perform the same action no matter what, or, as in your example function, it can simply pass the signal number on to some other function. You may or may not consider the latter to be a special case of the former.

Do note, however, that on a POSIX system, the sigaction() function is preferable to signal() for modifying signal dispositions. Its behavior is both more flexible and more consistent than signal()'s over various operating systems.

Upvotes: 2

Related Questions