We summarize our ndings in the form of a protocol

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: 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include "csapp.h" void handler2(int sig) { pid_t pid; while ((pid = waitpid(-1, NULL, 0)) > 0) printf("Handler reaped child %d\n", (int)pid); if (errno != ECHILD) unix_error("waitpid error"); Sleep(2); return; } int main() { int i, n; char buf[MAXBUF]; pid_t pid; if (signal(SIGCHLD, handler2) == SIG_ERR) unix_error("signal error"); /* parent creates children */ for (i = 0; i < 3; i++) { pid = Fork(); if (pid == 0) { printf("Hello from child %d\n", (int)getpid()); Sleep(1); exit(0); } } /* Manually restart the read call if it is interrupted */ while ((n = read(STDIN_FILENO, buf, sizeof(buf))) < 0) if (errno != EINTR) unix_error("read error"); printf("Parent processing input\n"); while (1) ; exit(0); } code/ecf/signal3.c Figure 8.30: signal3: An improved version of Figure 8.29 that correctly accounts for the fact that system calls can be interrupted. 436 CHAPTER 8. EXCEPTIONAL CONTROL FLOW code/src/csapp.c 1 2 3 4 5 6 7 8 9 10 11 12 handler_t *Signal(in...
View Full Document

Ask a homework question - tutors are online