04 Signals SIGCHLD
04 Signals SIGCHLD
pending signals
blocked signals
With 32 bit integers, up to 32 different signals can
be represented.
Example
In the example below, the SIGINT ( = 2) signal is
blocked and no signals are pending.
Sending, Receiving Signals
A signal is sent to a process setting the
corresponding bit in the pending signals integer for
the process.
Each time the OS selects a process to be run on a
handler routine.
The different default handler routines typically have
int main() {
signal(SIGINT, handle_sigint);
while (1) ;
return 0;
}
Sending signals via kill()
int kill(pid_t pid, int signal);
First parameter: id of destination process
Second parameter: the type of signal to send
Return value: 0 if signal was sent successfully
Better function name would be sendsig()
Example
pid_t iPid = getpid(); /* Process gets its id.*/
kill(iPid, SIGINT); /* Process sends itself a
SIGINT signal (commits
suicide?) */
signal0.c
int main () {
int stat;
pid_t pid;
if ((pid = fork()) == 0)
while(1) ;
else {
kill(pid, SIGINT);
wait(&stat);
if (WIFSIGNALED(stat))
psignal(WTERMSIG(stat),
"Child term due to");
}
}
Blocking signals in handlers
What happens if the same signal is sent as its
handler is running?
ignore it.
pause function
int pause(void);
while (1) {
pid = waitpid(-1, &status, WNOHANG);
if (pid <= 0) /* No more zombie children to reap. */
break;
printf(“Reaped child %d\n”, pid);
}
sleep(1);
}
Pop Quiz
25