Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recitation 7 (Oct. 25) Outline Minglong Shao Office hours: Reminders

Similar presentations


Presentation on theme: "Recitation 7 (Oct. 25) Outline Minglong Shao Office hours: Reminders"— Presentation transcript:

1 Recitation 7 (Oct. 25) Outline Minglong Shao Office hours: Reminders
Exceptions Process Signals Non-local jumps Reminders Lab4: Due Thursday Minglong Shao Office hours: Thursdays 5-6PM Wean Hall 1315

2 Exceptional control flow (ECF)
Abrupt changes in the control flow React to changes in system state that are not captured by internal program variables and are not necessarily related to the execution of the program Happens at all levels of a computer system Exceptions Concurrent processes Signals Non-local jumps

3 Exceptions Interrupt (asynchronous exceptions) Traps Faults Aborts
I/O interrupt, hardware reset, software reset, etc. Traps System calls, breakpoint traps, etc. Faults Page fault, protection fault, etc. Aborts Parity error, machine check, etc.

4 Process concept An instance of running program
Multiple processes run “concurrently” by time slicing Context switching Control flow passes from one process to another Preemptive scheduler of OS

5 Process IDs & process groups
A process has its own, unique process ID pid_t getpid(); A process belongs to exactly one process group pid_t getpgrp(); A new process belongs to which process group? Its parent’s process group A process can make a process group for itself and its children pid_t pid = getpid(); setpgid(0, 0); getpgrp()

6 Create a new process int fork(void) Test your understanding…
Create a new process that is identical to the parent process Return 0 to child process Return child’s pid to the parent process Call once, return twice Test your understanding… Problem 1

7 Problem 1 #include <unistd.h> #include <stdio.h>
int cnt = 0; int main(void) { if (fork() == 0){ cnt ++; fork(); cnt++; } printf("%d", cnt); return 0; Possible output: 133 313 331

8 Reaping child process Child process becomes zombie when terminates
Still consume system resources Parent performs reaping on terminated child pid_t wait(int *status) pid_t waitpid(pid_t pid, int *status, int options) Straightforward for reaping a single child Tricky for Shell implementation! Multiple child processes Both foreground and background Practice time again: problem 2

9 Problem 2 int main(){ int status; int counter = 1; if (fork() == 0){
printf("%d", counter); }else { printf("9"); counter --; exit(0); if (wait(&status) > 0){ printf("6"); } printf("8"); Answers: A. Y B. N C. Y D. N E. Y

10 Signals Section 8.5 in text
Read at least twice … really! A signal tells our program that some event has occurred Can we use signals to count events? No Why? Signals not queued!!

11 Important signals (Fig 8.23)
SIGINT Interrupt signal from terminal (ctrl-c) SIGTSTP Stop signal from terminal (ctrl-z) SIGCHLD A child process has stopped or terminated

12 Signals: sending other events OS Kernel blocked pending OS procedure
Process 1 Process 2 blocked kill(pid, SIGINT) pending 1 OS procedure other events divide by zero: SIGFPE ctrl-c: SIGINT child process exit: SIGCHLD OS Kernel

13 Signals: receiving Check when schedule the process to run OS Kernel
blocked pending 1 OS procedure OS Kernel

14 Receiving a signal An example: problem 3 Default action
The process terminates [and dumps core] The process stops until restarted by a SIGCONT signal (ctrl-z) The process ignore the signal Can modify (additional action) “Handle the signal” -- install signal handler void sigint_handler(int sig); signal(SIGINT, sigint_handler); An example: problem 3

15 Problem 3 void handler(int sig){ static int beeps = 0; printf("YO\n");
if (++beeps < 2) alarm(1); /* next SIGALRM will be delivered in 1s */ else{ printf("MA\n"); kill(getpid(), SIGKILL); } int main(){ Signal(SIGALRM, handler); while (1) ; printf(" is Great!\n"); return 0; 1. Output: YO MA 2. The program will terminate

16 Signals not queued Output: sent SIGUSR2 to parent counter = 1
int counter = 0; void handler(int sig) { counter++; sleep(1); return; } int main() int i; signal(SIGUSER2, handler); if (fork() == 0){ for (i = 0; i < 5; i++){ kill(getppid(), SIGUSR2); printf(“sent SIGUSR2 to parent\n”); exit(0); wait(NULL); printf(“counter = %d\n”, counter); Output: sent SIGUSR2 to parent counter = 1

17 Race hazard A data structure is shared by two pieces of code that can run concurrently Different behaviors of program depending upon how the schedule interleaves the execution of code.

18 An example of race hazard
sigchld_handler() { pid = waitpid(…); deletejob(pid); } eval() { pid = fork(); if(pid == 0) { /* child */ execve(…); /* parent */ /* signal handler may run BEFORE addjob()*/ addjob(…);

19 An okay schedule time Shell Signal Handler Child fork() addjob()
execve() exit() sigchld_handler() deletejobs()

20 A problematic schedule
time Shell Signal Handler Child fork() execve() exit() sigchld_handler() deletejobs() addjob() Job added to job list after the signal handler tried to delete it!

21 Solution: blocking signals
sigchld_handler() { pid = waitpid(…); deletejob(pid); } eval() { sigprocmask(SIG_BLOCK, …) pid = fork(); if(pid == 0) { /* child */ sigprocmask(SIG_UNBLOCK, …) execve(…); /* parent */ /* signal handler might run BEFORE addjob() */ addjob(…); More details (page 633)

22 Non-local jump int setjmp(jmp_buf env)
Must called before longjmp Stores current register context, stack pointer, and PC in the jump buffer env First called, return 0 if no error void longjmp(jmp_buf env, int i) Restores register context from jump buffer env Jumps back to where previous setjmp is called, behaves like setjmp just completes. But this time it returns i, not 0 Can only jump to an active context A function that has been called but not yet completed

23 Non-local jump (cont) Let’s see an example: problem 4
int sigsetjmp(jmp_buf env) Also saves blocked signals void siglongjmp(jmp_buf env, int i) Restores blocked signals besides others Let’s see an example: problem 4

24 Problem 4 jmp_buf stuff; jmp_buf more_stuff; int bar(){
if (setjmp(more_stuff) == 0) return 3; else return 6; } int foo(){ char c = getc(stdin); if (c == 'x') longjmp(stuff, 42); else if (c == 'y') longjmp(more_stuff, 17); return bar(); return -1; int main() { int n; n = setjmp(stuff); while ((n+=foo())<0) sleep(1); printf("%d\n", n); } Answer: 1. 3 2. 45 3. 45 4. ? 5. ?

25 Summary Process Reaping child processes Signals Non-local jumps
fork(), waitpid(), execl() (and variant) Reaping child processes Signals signal(), install handler, signals not queued Non-local jumps Check man page to understand the system calls better man waitpid (fork, signal, …) Read test book!


Download ppt "Recitation 7 (Oct. 25) Outline Minglong Shao Office hours: Reminders"

Similar presentations


Ads by Google