4/20/17 Review from last lecture • Exceptions • Events that require nonstandard control flow Exceptions and Processes • Generated externally (interrupts) or internally (traps and faults) Samira Khan • Processes April 20, 2017 • At any given time, system has multiple active processes • Only one can execute at a time on any single core • Each process appears to have total control of processor + private memory space 2 Asynchronous Exceptions (Interrupts) Synchronous Exceptions • Caused by events that occur as a result of executing an instruction: • Caused by events external to the processor • Traps • Indicated by setting the processor’s interrupt pin • Intentional • Handler returns to “next” instruction • Examples: system calls , breakpoint traps, special instructions • Returns control to “next” instruction • Faults • Examples: • Unintentional but possibly recoverable • Timer interrupt • Examples: page faults (recoverable), protection faults (unrecoverable), floating point exceptions • Every few ms, an external timer chip triggers an interrupt • Either re-executes faulting (“current”) instruction or aborts • Used by the kernel to take back control from user programs • Aborts • I/O interrupt from external device • Unintentional and unrecoverable • Hitting Ctrl-C at the keyboard • Examples: illegal instruction, parity error, machine check • Arrival of a packet from a network • Aborts current program • Arrival of data from a disk 3 4 1
4/20/17 ECF Exists at All Levels of a System Taxonomy Handled in kernel Handled in user process • Exceptions ECF • Hardware and operating system kernel software • Process Context Switch Asynchronous Synchronous • Hardware timer and kernel software Interrupts Traps Faults Aborts • Signals • Kernel software and application software Signals 5 6 Signals Fault Example: Invalid Memory Reference • A signal is a small message that notifies a process that an int a[1000]; main () event of some type has occurred in the system { a[5000] = 13; • Akin to exceptions and interrupts } • Sent from the kernel (sometimes at the request of another 80483b7: c7 05 60 e3 04 08 0d movl $0xd,0x804e360 process) to a process • Signal type is identified by small integer ID’s (1-30) User code Kernel code • Only information in a signal is its ID and the fact that it arrived Exception: page fault ID Name Default Action Corresponding Event movl 2 SIGINT Terminate User typed ctrl-c Detect invalid address 9 SIGKILL Terminate Kill program (cannot override or ignore) Signal process 11 SIGSEGV Terminate Segmentation violation 14 SIGALRM Terminate Timer signal • Sends SIGSEGV signal to user process 17 SIGCHLD Ignore Child stopped or terminated • User process exits with “segmentation fault” 7 8 2
4/20/17 Signal Concepts: Receiving a Signal Signal Concepts: Sending a Signal • A destination process receives a signal when it is forced by the kernel to react in some way to the delivery of the signal • Kernel sends (delivers) a signal to a destination process by updating some state in the context of the destination process • Some possible ways to react: • Ignore the signal (do nothing) • Terminate the process (with optional core dump) • Kernel sends a signal for one of the following reasons: • Catch the signal by executing a user-level function called signal handler • Kernel has detected a system event such as divide-by-zero • Akin to a hardware exception handler being called in response to an (SIGFPE) or the termination of a child process (SIGCHLD) asynchronous interrupt: • Another process has invoked the kill system call to explicitly request the kernel to send a signal to the destination (1) Signal received (2) Control passes process by process to signal handler I curr I next (3) Signal handler runs (4) Signal handler returns to 9 next instruction 10 Signal Concepts: Pending and Blocked Signal Concepts: Pending/Blocked Bits Signals • A signal is pending if sent but not yet received • Kernel maintains pending and blocked bit vectors • There can be at most one pending signal of any particular in the context of each process type • pending : represents the set of pending signals • Important: Signals are not queued • Kernel sets bit k in pending when a signal of type k is delivered • If a process has a pending signal of type k, then subsequent signals of type k that are sent to that process are discarded • Kernel clears bit k in pending when a signal of type k is received • blocked : represents the set of blocked signals • A process can block the receipt of certain signals • Can be set and cleared by using the sigprocmask function • Blocked signals can be delivered, but will not be received until • Also referred to as the signal mask . the signal is unblocked • A pending signal is received at most once 11 12 3
4/20/17 Signal Concepts: Sending a Signal Signal Concepts: Sending a Signal User level User level Process B Process B Process A Process A Process C Process C kernel kernel Pending for A Blocked for A Pending for A Blocked for A Pending for B Blocked for B Pending for B Blocked for B Pending for C Blocked for C Pending for C Blocked for C 13 14 Signal Concepts: Sending a Signal Signal Concepts: Sending a Signal User level User level Process B Process B Process A Process A Process C Process C kernel kernel Pending for A Blocked for A Pending for A Blocked for A Pending for B Blocked for B Pending for B Blocked for B 1 Pending for C Blocked for C 1 Pending for C Blocked for C 15 16 4
4/20/17 Signal Concepts: Sending a Signal Sending Signals: Process Groups • Every process belongs to exactly one process group User level Process B pid=10 Shell pgid=10 Process A Back- Fore- Back- pid=20 pid=32 pid=40 ground ground ground pgid=20 pgid=32 pgid=40 Process C job job #1 job #2 Background Background process group 32 process group 40 Child Child kernel getpgrp() pid=21 pid=22 Return process group of current process pgid=20 pgid=20 Pending for A Blocked for A Foreground setpgid() Pending for B Blocked for B process group 20 Change process group of a process (see 0 Pending for C Blocked for C text for details) 17 18 Sending Signals from the Keyboard Sending Signals with /bin/kill Program • /bin/kill program • Typing ctrl-c (ctrl-z) causes the kernel to send a SIGINT (SIGTSTP) to every job in the foreground process group. sends arbitrary signal linux> ./forks 16 • SIGINT – default action is to terminate each process to a process or process Child1: pid=24818 pgrp=24817 • SIGTSTP – default action is to stop (suspend) each process Child2: pid=24819 pgrp=24817 group pid=10 linux> ps Shell pgid=10 PID TTY TIME CMD 24788 pts/2 00:00:00 tcsh • Examples 24818 pts/2 00:00:02 forks 24819 pts/2 00:00:02 forks • /bin/kill –9 Fore- Back- Back- 24820 pts/2 00:00:00 ps pid=20 pid=32 24818 ground ground ground pid=40 pgid=20 linux> /bin/kill -9 -24817 pgid=32 pgid=40 job job #1 job #2 Send SIGKILL to process 24818 linux> ps PID TTY TIME CMD Background Background 24788 pts/2 00:00:00 tcsh process group 32 process group 40 • /bin/kill –9 – 24823 pts/2 00:00:00 ps Child Child linux> 24817 Send SIGKILL to every process in pid=21 pid=22 pgid=20 pgid=20 process group 24817 Foreground 19 20 process group 20 5
4/20/17 Example of ctrl-c and ctrl-z Sending Signals with kill Function STAT (process state) Legend: void fork12() bluefish> ./forks 17 { Child: pid=28108 pgrp=28107 First letter: pid_t pid[N]; Parent: pid=28107 pgrp=28107 <types ctrl-z> S: sleeping int i; Suspended T: stopped int child_status; bluefish> ps w R: running PID TTY STAT TIME COMMAND for (i = 0; i < N; i++) 27699 pts/8 Ss 0:00 -tcsh Second letter: if ((pid[i] = fork()) == 0) { 28107 pts/8 T 0:01 ./forks 17 s: session leader /* Child: Infinite Loop */ 28108 pts/8 T 0:01 ./forks 17 +: foreground proc group while(1) 28109 pts/8 R+ 0:00 ps w ; bluefish> fg ./forks 17 See “man ps” for more } <types ctrl-c> details bluefish> ps w for (i = 0; i < N; i++) { PID TTY STAT TIME COMMAND printf("Killing process %d\n", pid[i]); 27699 pts/8 Ss 0:00 -tcsh kill(pid[i], SIGINT); 28110 pts/8 R+ 0:00 ps w } } forks.c 21 22 Receiving Signals Receiving Signals • Suppose kernel is returning from an exception handler • Suppose kernel is returning from an exception and is ready to pass control to process p handler and is ready to pass control to process p • Kernel computes pnb = pending & ~blocked • The set of pending nonblocked signals for process p Process A Process B user code • If ( pnb == 0 ) context switch • Pass control to next instruction in the logical flow for p kernel code Time • Else user code • Choose least nonzero bit k in pnb and force process p to kernel code context switch receive signal k • The receipt of the signal triggers some action by p user code • Repeat for all nonzero k in pnb • Pass control to next instruction in logical flow for p 23 24 6
Recommend
More recommend