Queued for each signal type just have single bit

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: /forks 13 for (i = 0; i < N; i++) if ((pid[i] = fork()) == 0)Killing process 25417 { Killing process 25418 while(1); /* child infinite loop Killing process 25419 } Killing process 25420 for (i = 0; i < N; i++) { K ", pid[i]); printf("Killing process %d\nilling process 25421 Process 25417 received signal 2 kill(pid[i], SIGINT); Process 25418 received signal 2 } Process 25420 received signal 2 for (i = 0; i < N; i++) { Process pid_t wpid = wait(&child_status); 25421 received signal 2 P if (WIFEXITED(child_status))rocess 25419 received signal 2 Child 25417 terminated d\n", printf("Child %d terminated with exit status %with exit Child 25418 terminated with exit wpid, WEXITSTATUS(child_status)); Child 25420 terminated with exit else Child 25419 terminated with printf("Child %d terminated abnormally\n", wpid); exit Child 25421 terminated with exit } linux> } status status status status status 0 0 0 0 0 27 Carnegie Mellon Signals Handlers as Concurrent Flows   A signal handler is a separate logical flow (not process) that runs concurrently with the main program   “concurrently” in the “not sequen2al” sense Process A Process A while (1) ; Process B handler(){ … } Time 28 Carnegie Mellon Another View of Signal Handlers as Concurrent Flows Process A Signal delivered Icurr Process B user code (main) kernel code context switch user code (main) kernel code Signal received context switch user code (handler) kernel code Inext user code (main) 29 Carnegie Mellon Signal Handler Funkiness int ccount = 0; void child_handler(int sig) { int child_status; pid_t pid = wait(&child_status); ccount--; safe_printf( "Received signal %d from process %d\n", sig, pid); }   Pending signals are not queued   For each signal type, just have single bit indica2ng whether or not signal is pending   Even if mul2ple processes void fork14() have sent this signal { pid_t pid[N]; int i, child_status; ccount = N; signal(SIGCHLD, child_handler); for (i = 0; i < N; i++)inux> ./forks 14 l if ((pid[i] = fork()) == 0) SIGCHLD signal 17 for process 21344 Received { sleep(1); /* deschedule child */ Received SIGCHLD signal 17 for process 21345 exit(0); /* Child: Exit */ } while (ccount > 0) pause(); /* Suspend until signal occurs */ } 30 Carnegie Mellon Living With Nonqueuing Signals   Must check for all terminated jobs   Typically loop with waitpid void child_handler2(int sig) { int child_status; pid_t pid; while ((pid = waitpid(-1, &child_status, WNOHANG)) > 0) { ccount--; safe_printf("Received signal %d from process %d\n", sig, pid); } } greatwhite> forks 15 void fork15() Received signal 17 from process 27476 { Received signal 17 from process 27477 ... Received signal 17 from process 27478 signal(SIGCHLD, child_handler2); Received signal 17 from process 27479 ... Received signal 17 from process 27480 } greatwhite> 31 Carnegie Mellon More Signal Handler Funkiness     Signal arrival during long system calls (say a read) Signal handler i...
View Full Document

This note was uploaded on 07/03/2013 for the course CS 15-213 taught by Professor Fr during the Fall '07 term at Carnegie Mellon.

Ask a homework question - tutors are online