Typing ctrl c ctrl z causes the shell to send a

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: r("waitpid error"); printf("Handler reaped child %d\n", (int)pid); Sleep(2); return; } int main() { int i, n; char buf[MAXBUF]; if (signal(SIGCHLD, handler1) == SIG_ERR) unix_error("signal error"); /* parent creates children */ for (i = 0; i < 3; i++) { if (Fork() == 0) { printf("Hello from child %d\n", (int)getpid()); Sleep(1); exit(0); } } /* parent waits for terminal input and then processes it */ if ((n = read(STDIN_FILENO, buf, sizeof(buf))) < 0) unix_error("read"); printf("Parent processing input\n"); while (1) ; exit(0); } code/ecf/signal1.c Figure 8.28: signal1: This program is flawed because it fails to deal with the facts that signals can block, signals are not queued, and system calls can be interrupted. 432 <ctrl-z> Suspended linux> ps PID TTY STAT TIME COMMAND ... 10319 p5 T 0:03 signal1 10321 p5 Z 0:00 (signal1 <zombie>) 10323 p5 R 0:00 ps CHAPTER 8. EXCEPTIONAL CONTROL FLOW What went wrong? The problem is that our code failed to account for the facts that signals can block and that signals are not queued. Here’s what happened: The first signal is received and caught by the parent. While the handler is still processing the first signal, the second signal is delivered and added to the set of pending signals. However, s...
View Full Document

Ask a homework question - tutors are online