Exceptional Control Flow II Oct 23, 2001 Topics Exceptions Process context switches class17.ppt 15-213 “The course that gives CMU its Zip!”

Slides:



Advertisements
Similar presentations
Recitation 8: 10/28/02 Outline Processes Signals –Racing Hazard –Reaping Children Annie Luo Office Hours: Thursday 6:00.
Advertisements

Carnegie Mellon 1 Exceptional Control Flow: Exceptions and Processes /18-243: Introduction to Computer Systems 13 th Lecture, June 15, 2011 Instructors:
Fabián E. Bustamante, Spring 2007 Exceptional Control Flow II Today Process Hierarchy Shells Signals Nonlocal jumps Next time Virtual memory.
Carnegie Mellon Introduction to Computer Systems /18-243, spring th Lecture, Mar. 5 th Instructors: Gregory Kesden and Markus Püschel.
Program and OS Interation: Multitasking and Signals Andrew Case
Signals Adapted from slides of Gregory Kesden and Markus Püschel of Carnegie Mellon Univ.
1. Processes 2. Signals. Processes Definition: A process is an instance of a running program.  One of the most profound ideas in computer science  Not.
University of Washington Today Finish up cache-aware programming example Processes  So we can move on to virtual memory 1.
Carnegie Mellon 1 Exceptional Control Flow: Signals and Nonlocal Jumps : Introduction to Computer Systems 13 th Lecture, Oct. 7, 2010 Instructors:
University of Washington Today Move on to … 1. University of Washington Hmm… Lets look at this again Disk Main Memory L2 unified cache L1 I-cache L1 D-cache.
Processes Topics Process context switches Creating and destroying processes CS 105 “Tour of the Black Holes of Computing!”
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
Exceptional Control Flow Part II October 21, 2004 Topics Process Hierarchy Shells Signals Nonlocal jumps class16.ppt “The course that gives CMU.
Exceptional Control Flow Part I September 22, 2008 Topics Exceptions Process context switches Creating and destroying processes class11.ppt ,
Exceptional Control Flow Part II Oct. 22, 2002 Topics Process Hierarchy Shells Signals Nonlocal jumps class17.ppt “The course that gives CMU its.
Exceptional Control Flow Part II October 24, 2007 Topics Process Hierarchy Shells Signals Nonlocal jumps class15.ppt , F’07.
Exceptional Control Flow Part I Topics Exceptions Process context switches Creating and destroying processes class14.ppt CS 123.
Section A (March 14) Outline Exceptions Process Signals Non-local jumps Reminders Lab4: Due Next Thursday TA: Kun Gao Shamelessly Modified from Minglong.
Exceptional Control Flow Part I Topics Exceptions Process context switches Creating and destroying processes.
CS 3214 Computer Systems Godmar Back Lecture 14. Announcements Project 3 due Oct 22 Exercise 6 due Oct 14 Project 3 Help Session: –Holden Hall 114, Thursday,
Exceptional Control Flow Part II Topics Process Hierarchy Signals CS213.
Exceptional Control Flow Topics Exceptions Process hierarchy Shells Signals CS 105 “Tour of the Black Holes of Computing”
1 Program and OS interactions: Exceptions and Processes Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
Fabián E. Bustamante, Spring 2007 Exceptional Control Flow Part I Today Exceptions Process context switches Creating and destroying processes Next time.
CS 201 Exceptions and System calls Part II. – 2 – Shell Programs A shell is an application program that runs programs on behalf of the user. sh – Original.
Carnegie Mellon Introduction to Computer Systems , fall th Lecture, Oct. 14 th Instructors: Majd Sakr and Khaled Harras.
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
Exceptional Control Flow Part I March 4, 2004 Topics Exceptions Process context switches Creating and destroying processes class16.ppt “The course.
Operating Systems Chapter 2
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Processes Topics Process context switches Creating and destroying processes CS 105 “Tour of the Black Holes of Computing!”
Carnegie Mellon 1 Exceptional Control Flow: Exceptions and Processes : Introduction to Computer Systems 13 th Lecture, Oct. 7, 2014 Instructors:
System-level programming II: Processes Feb 24, 2000 Topics User-level view of processes Exceptions Context switches Higher level control flow mechanisms.
Processes Topics Process context switches Creating and destroying processes CS 105 “Tour of the Black Holes of Computing!”
Carnegie Mellon Introduction to Computer Systems /18-243, spring th Lecture, Mar. 17 th Instructors: Gregory Kesden and Markus Püschel.
Program Translation and Execution II: Processes Oct 1, 1998 Topics User-level view of processes Implementation of processes setjmp/longjmp class12.ppt.
Exceptional Control Flow Part I Topics Exceptions Process context switches Creating and destroying processes class16.ppt.
Exceptional Control Flow Part II Topics Process Hierarchy Shells Signals.
Exceptional Flow Control Part II. – 2 – ECF Exists at All Levels of a System Exceptions Hardware and operating system kernel software Process Context.
Exceptional Control Flow
Exceptional Control Flow I March 12, 2002
Computer System Chapter 8. Exceptional Control Flow
1 Exceptional Control Flow III. 2 Outline Loading and Running Programs Multitasking, shells Suggested reading 8.4.
1 Exceptional Control Flow Ⅱ. 2 Outline Kernel Mode and User Mode Process context switches Three States of Processes Context Switch System Calls and Error.
Overview of today’s lecture Re-view of the previous lecture Process management models and state machines (cont’d from the previous lecture) What is in.
Recitation 7 – 3/18/02 Outline fork and scheduling Signals
Recitation 7 (Oct. 25) Outline Minglong Shao Office hours: Reminders
Exceptional Control Flow & Processes
Exceptional Control Flow Oct 24, 2000
Exceptional Control Flow Part I
Instructors: Anthony Rowe, Seth Goldstein and Gregory Kesden
Exceptional Control Flow Part II
Exceptional Control Flow
CS703 – Advanced Operating Systems
Instructors: Randy Bryant, Dave O’Hallaron, and Greg Kesden
Exceptional Control Flow
Exceptional Control Flow Part II
Exceptional Control Flow Part II
Exceptional Control Flow
Exceptional Control Flow Part I Mar. 11, 2003
Exceptional Control Flow
Exceptional Control Flow Part I
CS 105 “Tour of the Black Holes of Computing!”
Processes Prof. Ikjun Yeom TA – Mugyo
Exceptional Control Flow
CS 105 “Tour of the Black Holes of Computing!”
CS 105 “Tour of the Black Holes of Computing!”
Exceptional Control Flow & Processes October 2, 2008
Exceptional Control Flow
Exceptional Control Flow
Presentation transcript:

Exceptional Control Flow II Oct 23, 2001 Topics Exceptions Process context switches class17.ppt “The course that gives CMU its Zip!”

CS 213 F’01– 2 – class17.ppt Exceptions An exception is a transfer of control to the OS in response to some event (i.e., change in processor state) User ProcessOS exception exception processing by exception handler exception return (optional) event current next

CS 213 F’01– 3 – class17.ppt Role of Exceptions Error Handling Error conditions detected by hardware and/or OS –Divide by zero –Invalid pointer reference Getting Help from OS Initiate I/O operation Fetch memory page from disk Process Management Create illusion that running many programs and services simultaneously

CS 213 F’01– 4 – class17.ppt The World of Multitasking System Runs Many Processes Concurrently Process: executing program –State consists of memory image + register values + program counter Continually switches from one process to another –Suspend process when it needs I/O resource or timer event occurs –Resume process when I/O available or given scheduling priority Appears to user(s) as if all processes executing simultaneously –Even though most systems can only execute one process at a time –Except possibly with lower performance than if running alone

CS 213 F’01– 5 – class17.ppt Programmer’s Model of Multitasking Basic Functions fork() spawns new process –Called once, returns twice exit() terminates own process –Called once, never returns –Puts it into “zombie” status wait() and waitpid() wait for and reap terminated children execl() and execve() replace state of existing process with that of newly started program –Called once, never returns Programming Challenge Understanding the nonstandard semantics of the functions Avoiding improper use of system resources –Fewer safeguards provided

CS 213 F’01– 6 – class17.ppt Fork Example #4 void fork4() { printf("L0\n"); if (fork() != 0) { printf("L1\n"); if (fork() != 0) { printf("L2\n"); fork(); } printf("Bye\n"); } Key Points Both parent and child can continue forking L1L2Bye L0

CS 213 F’01– 7 – class17.ppt Fork Example #5 void fork5() { printf("L0\n"); if (fork() == 0) { printf("L1\n"); if (fork() == 0) { printf("L2\n"); fork(); } printf("Bye\n"); } Key Points Both parent and child can continue forking ByeL0 ByeL1 ByeL2 Bye

CS 213 F’01– 8 – class17.ppt linux>./forks 7 & [1] 6639 Running Parent, PID = 6639 Terminating Child, PID = 6640 linux> ps PID TTY TIME CMD 6585 ttyp9 00:00:00 tcsh 6639 ttyp9 00:00:03 forks 6640 ttyp9 00:00:00 forks 6641 ttyp9 00:00:00 ps linux> kill 6639 [1] Terminated linux> ps PID TTY TIME CMD 6585 ttyp9 00:00:00 tcsh 6642 ttyp9 00:00:00 ps Zombie Example ps shows child process as “defunct” Killing parent allows child to be reaped void fork7() { if (fork() == 0) { /* Child */ printf("Terminating Child, PID = %d\n", getpid()); exit(0); } else { printf("Running Parent, PID = %d\n", getpid()); while (1) ; /* Infinite loop */ }

CS 213 F’01– 9 – class17.ppt linux>./forks 8 Terminating Parent, PID = 6675 Running Child, PID = 6676 linux> ps PID TTY TIME CMD 6585 ttyp9 00:00:00 tcsh 6676 ttyp9 00:00:06 forks 6677 ttyp9 00:00:00 ps linux> kill 6676 linux> ps PID TTY TIME CMD 6585 ttyp9 00:00:00 tcsh 6678 ttyp9 00:00:00 ps Nonterminating Child Example ps shows child process as “defunct” Killing parent allows child to be reaped void fork8() { if (fork() == 0) { /* Child */ printf("Running Child, PID = %d\n", getpid()); while (1) ; /* Infinite loop */ } else { printf("Terminating Parent, PID = %d\n", getpid()); exit(0); }

CS 213 F’01– 10 – class17.ppt Exec Example Task Sort a set of files E.g.,./sortfiles f1.txt f2.txt f3.txt Perform concurrently –Using Unix sort command –Commands of form sort f1.txt –o f1.txt Steps Invoke a process for each file Complete by waiting for all processes to complete #include int main(int argc, char *argv[]) { int cnt = invoke(argc, argv); complete(cnt); return 0; }

CS 213 F’01– 11 – class17.ppt Exec Example (cont.) Use fork and execl to spawn set of sorting processes int invoke(int argc, char *argv[]) { int i; for (i = 1; i < argc; i++) { /* Fork off a new process */ if (fork() == 0) { /* Child: Invoke sort program */ printf("Process %d sorting file %s\n", getpid(), argv[i]); if (execl("/bin/sort", "sort", argv[i], "-o", argv[i], 0) < 0) { perror("sort"); exit(1); } /* Never reach this point */ } return argc-1; }

CS 213 F’01– 12 – class17.ppt Exec Example (cont.) Use wait to wait for and reap terminating children void complete(int cnt) { int i, child_status; for (i = 0; i < cnt; i++) { pid_t wpid = wait(&child_status); if (WIFEXITED(child_status)) printf("Process %d completed with status %d\n", wpid, WEXITSTATUS(child_status)); else printf("Process %d terminated abnormally\n", wpid); }

CS 213 F’01– 13 – class17.ppt Signals Software events generated by OS and processes –an OS abstraction for exceptions and interrupts Sent from the kernel or a process to other processes. Different signals are identified by small integer ID’s Only information in a signal is its ID and the fact that it arrived. Num.NameDefaultDescription 2 SIGINT TerminateInterrupt from keyboard (cntl-c) 9 SIGKILL TerminateKill program (cannot override or ignore) 11 SIGSEGV Terminate & DumpSegmentation violation 14 SIGALRM TerminateTimer signal 17 SIGCHLD IgnoreChild stopped or terminated

CS 213 F’01– 14 – class17.ppt Sending Signals Unix kill Program Sends arbitrary signal to process e.g., /bin/kill –s 9 pid –sends SIGKILL to specified process Function kill Send signal to another process kill(pid, signal) linux>./forks 8 Terminating Parent, PID = 6675 Running Child, PID = 6676 linux> ps PID TTY TIME CMD 6585 ttyp9 00:00:00 tcsh 6676 ttyp9 00:00:06 forks 6677 ttyp9 00:00:00 ps linux> /bin/kill –s linux> ps PID TTY TIME CMD 6585 ttyp9 00:00:00 tcsh 6678 ttyp9 00:00:00 ps

CS 213 F’01– 15 – class17.ppt Kill Example Use kill to forcibly terminate children void fork12() { pid_t pid[N]; int i; int child_status; for (i = 0; i < N; i++) if ((pid[i] = fork()) == 0) { /* Child: Infinite Loop */ while(1); } for (i = 0; i < N; i++) { printf("Killing process %d\n", pid[i]); kill(pid[i], SIGINT); } 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); }

CS 213 F’01– 16 – class17.ppt Handling Signals Every Signal Type has Default Behavior Typically terminate or ignore Can Override by Declaring Special Signal Handler Function signal(sig, handler) –Indicates that signals of type sig should invoke function handler –Handler returns to point where exception occurred void int_handler(int sig) { 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);... }

CS 213 F’01– 17 – class17.ppt Signal Handler Funkiness Signals are not Queued For each signal type, just have single bit indicating whether or not signal has occurred Even if multiple processes have sent this signal int ccount = 0; void child_handler(int sig) { int child_status; pid_t pid = wait(&child_status); ccount--; printf("Received signal %d from process %d\n", sig, pid); } void fork14() { pid_t pid[N]; int i, child_status; ccount = N; signal(SIGCHLD, child_handler); for (i = 0; i < N; i++) if ((pid[i] = fork()) == 0) { /* Child: Exit */ exit(0); } while (ccount > 0) pause();/* Suspend until signal occurs */ }

CS 213 F’01– 18 – class17.ppt Living with Nonqueuing Signals Must Check for All Possible Signal Sources Typically loop with wait void child_handler2(int sig) { int child_status; pid_t pid; while ((pid = wait(&child_status)) > 0) { ccount--; printf("Received signal %d from process %d\n", sig, pid); } void fork15() {... signal(SIGCHLD, child_handler2);... }

CS 213 F’01– 19 – class17.ppt A program that reacts to externally generated events (ctrl-c) #include static void handler(int sig) { printf("You think hitting ctrl-c will stop the bomb?\n"); sleep(2); printf("Well..."); fflush(stdout); sleep(1); printf("OK\n"); exit(0); } main() { signal(SIGINT, handler); /* installs ctl-c handler */ while(1) { }

CS 213 F’01– 20 – class17.ppt A program that reacts to internally generated events #include int beeps = 0; /* SIGALRM handler */ void handler(int sig) { printf("BEEP\n"); fflush(stdout); if (++beeps < 5) alarm(1); else { printf("BOOM!\n"); exit(0); } main() { signal(SIGALRM, handler); alarm(1); /* send SIGALRM in 1 second */ while (1) { /* handler returns here */ } bass> a.out BEEP BOOM! bass>

CS 213 F’01– 21 – class17.ppt Nonlocal jumps: setjmp()/longjmp() Powerful (but dangerous) user-level mechanism for transferring control to an arbitrary location. controlled to way to break the procedure call/return discipline useful for error recovery int setjmp(jmp_buf j) must be called before longjmp identifies a return site for a subsequent longjmp. Called once, returns one or more times Implementation: remember where you are by storing the current register context, stack pointer, and PC value in jmp_buf. return 0

CS 213 F’01– 22 – class17.ppt setjmp/longjmp (cont) void longjmp(jmp_buf j, int i) meaning: –return from the setjmp remembered by jump buffer j again... –…this time returning i called after setjmp Called once, but never returns longjmp Implementation: restore register context from jump buffer j set %eax (the return value) to i jump to the location indicated by the PC stored in jump buf j.

CS 213 F’01– 23 – class17.ppt setjmp/longjmp example #include jmp_buf buf; main() { if (setjmp(buf) != 0) { printf("back in main due to an error\n"); else printf("first time through\n"); p1(); /* p1 calls p2, which calls p3 */ }... p3() { if (error) longjmp(buf, 1) }

CS 213 F’01– 24 – class17.ppt Putting it all together: A program that restarts itself when ctrl-c’d #include sigjmp_buf buf; void handler(int sig) { siglongjmp(buf, 1); } main() { signal(SIGINT, handler); if (!sigsetjmp(buf, 1)) printf("starting\n"); else printf("restarting\n"); while(1) { sleep(1); printf("processing...\n"); } bass> a.out starting processing... restarting processing... restarting processing... restarting processing... Ctrl-c

CS 213 F’01– 25 – class17.ppt Limitations of Long Jumps Works Within Stack Discipline Can only long jump to environment of function that has been called but not yet completed jmp_buf env; P1() { if (setjmp(env)) { /* Long Jump to here */ } else { P2(); } P2() {... P2();... P3(); } P3() { longjmp(env, 1); } P1 P2 P3 env P1 Before longjmp After longjmp

CS 213 F’01– 26 – class17.ppt Limitations of Long Jumps (cont.) Works Within Stack Discipline Can only long jump to environment of function that has been called but not yet completed jmp_buf env; P1() { P2(); P3(); } P2() { if (setjmp(env)) { /* Long Jump to here */ } P3() { longjmp(env, 1); } env P1 P2 At setjmp P1 P3 env At longjmp X P1 P2 P2 returns env X

CS 213 F’01– 27 – class17.ppt Summary Signals Provide Process-Level Exception Handling Can generate with kill Can define effect by declaring signal handler Some Caveats Very high overhead – >10,000 clock cycles –Only use for exceptional conditions Don’t have queues –Just one bit of status for each signal type Long Jumps Provide Exceptional Control Flow Within Process Within constraints of stack discipline