{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

23 carnegie mellon receiving signals suppose kernel

Info icon This 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: 108 pts/8 T 0:01 ./forks 17 28109 pts/8 R+ 0:00 ps w bluefish> fg ./forks 17 <types ctrl-c> bluefish> ps w PID TTY STAT TIME COMMAND 27699 pts/8 Ss 0:00 -tcsh 28110 pts/8 R+ 0:00 ps w STAT (process state) Legend: First leGer: S: sleeping T: stopped R: running Second leGer: s: session leader +: foreground proc group See “man ps” for more details 21 Carnegie Mellon Sending Signals with kill Func&on void fork12() { pid_t pid[N]; int i, child_status; for (i = 0; i < N; i++) if ((pid[i] = fork()) == 0) while(1); /* Child infinite loop */ /* Parent terminates the child processes */ for (i = 0; i < N; i++) { printf("Killing process %d\n", pid[i]); kill(pid[i], SIGINT); } /* Parent reaps terminated children */ for (i = 0; i < N; i++) { pid_t wpid = wait(&child_status); if (WIFEXITED(child_status)) printf("Child %d terminated with exit status %d\n", wpid, WEXITSTATUS(child_status)); else printf("Child %d terminated abnormally\n", wpid); } } 22 Carnegie Mellon Receiving Signals   Suppose kernel is returning from an excep&on handler and is ready to pass control to process p Process A Process B user code kernel code Time context switch user code kernel code context switch user code Important: All context switches are ini&ated by calling some excep&onal hander. 23 Carnegie Mellon Receiving Signals     Suppose kernel is returning from an excep&on handler and is ready to pass control to process p Kernel computes pnb = pending & ~blocked   The set of pending nonblocked signals for process p   If (pnb == 0)   Pass control to next instruc2on in the logical flow for p   Else   Choose least nonzero bit k in pnb and force process p to receive signal k   The receipt of the signal triggers some ac)on by p   Repeat for all nonzero k in pnb   Pass control to next instruc2on in logical flow for p 24 Carnegie Mellon Default Ac&ons   Each signal type has a predefined default ac)on, which is one of:         The process terminates The process terminates and dumps core The process stops un2l restarted by a SIGCONT signal The process ignores the signal 25 Carnegie Mellon Installing Signal Handlers   The signal func&on modifies the default ac&on associated with the receipt of signal signum:   handler_t *signal(int signum, handler_t *handler)   Different values for handler:   SIG_IGN: ignore signals of type signum   SIG_DFL: revert to the default ac2on on receipt of signals of type signum   Otherwise, handler is the address of a signal handler         Called when process receives signal of type signum Referred to as “installing” the handler Execu2ng handler is called “catching” or “handling” the signal When the handler executes its return statement, control passes back to instruc2on in the control flow of the process that was interrupted by receipt of the signal 26 Carnegie Mellon Signal Handling Example void int_handler(int sig) { safe_printf("Process %d received signal %d\n", getpid(), sig); exit(0); } void fork13() { pid_t pid[N]; int i, child_status; signal(SIGINT, int_handler); linux> ....
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern