1 Exceptional Control Flow III. 2 Outline Loading and Running Programs Multitasking, shells Suggested reading 8.4.

Slides:



Advertisements
Similar presentations
Recitation 8 (Nov. 1) Outline Process & job control Lab 5 Reminder Lab 5: Due Thursday Minglong Shao Office hours: Thursdays 5-6PM.
Advertisements

Recitation 8: 10/28/02 Outline Processes Signals –Racing Hazard –Reaping Children Annie Luo Office Hours: Thursday 6:00.
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Carnegie Mellon 1 Exceptional Control Flow: Exceptions and Processes /18-243: Introduction to Computer Systems 13 th Lecture, June 15, 2011 Instructors:
CS283 Systems Programming Process Management and Signals
University of Washington Today Midterm grades posted  76. HW 3 due Lab 4 fun! 1.
Fabián E. Bustamante, Spring 2007 Exceptional Control Flow II Today Process Hierarchy Shells Signals Nonlocal jumps Next time Virtual memory.
Carnegie Mellon 1 Exceptional Control Flow : Introduction to Computer Systems Recitation 9: Monday, October 21 st, 2013 Ian Hartwig Section E.
Carnegie Mellon Introduction to Computer Systems /18-243, spring th Lecture, Mar. 5 th Instructors: Gregory Kesden and Markus Püschel.
15-213, Fall 06 Outline Shell Lab Processes Signals.
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.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
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:
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 Oct. 22, 2002 Topics Process Hierarchy Shells Signals Nonlocal jumps class17.ppt “The course that gives CMU its.
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Threads and Scheduling 6.
CSSE Operating Systems
Section A (March 14) Outline Exceptions Process Signals Non-local jumps Reminders Lab4: Due Next Thursday TA: Kun Gao Shamelessly Modified from Minglong.
Processes, Signals, I/O, Shell lab
Exceptional Control Flow Part II Topics Process Hierarchy Signals CS213.
Carnegie Mellon 1 Processes, Signals, I/O, Shell Lab : Introduction to Computer Systems Recitation 9: 10/21/2013 Tommy Klein Section B.
5 UNIX Processes. Introduction  Processes  How to list them  How to terminate them  Process priorities  Scheduling jobs  Signals.
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.
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.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Creating and Executing Processes
Carnegie Mellon 1 Exceptional Control Flow: Exceptions and Processes : Introduction to Computer Systems 13 th Lecture, Oct. 7, 2014 Instructors:
University of Amsterdam Computer Systems – Processes Arnoud Visser 1 Computer Systems Processes.
System calls for Process management
Unix Process Model Simple and powerful primitives for process creation and initialization. fork syscall creates a child process as (initially) a clone.
Operating Systems Process Creation
Outline for Today Objectives –Finish discussion of Birrell –UNIX Signals –Eraser Administrative –Spider talk after class.
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
Exceptional Control Flow Part II Topics Process Hierarchy Shells Signals.
Operating Systems Recitation 4, April th, 2002 Signals.
Exceptional Control Flow Topics Exceptions Shells Signals CS 105 “Tour of the Black Holes of Computing”
Exceptional Control Flow
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
System calls for Process management Process creation, termination, waiting.
1 Exceptional Control Flow Ⅱ. 2 Outline Kernel Mode and User Mode Process context switches Three States of Processes Context Switch System Calls and Error.
1 Lecture 19: Unix signals and Terminal management n what is a signal n signal handling u kernel u user n signal generation n signal example usage n terminal.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
Exceptional Control Flow II Oct 23, 2001 Topics Exceptions Process context switches class17.ppt “The course that gives CMU its Zip!”
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
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.
EXTRA SLIDES fork, execve, wait, waitpid, shells, background jobs, (from the book and CMU slides)
Recitation 7 (Oct. 25) Outline Minglong Shao Office hours: Reminders
CS 3214 Introduction to Computer Systems
Section 8: Processes What is a process Creating processes Fork-Exec
Unix Process Management
Instructors: Anthony Rowe, Seth Goldstein and Gregory Kesden
Exceptional Control Flow Part II
Signals Adapted from slides of Gregory Kesden and Markus Püschel of Carnegie Mellon Univ.
Exceptional Control Flow
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
Processes Prof. Ikjun Yeom TA – Mugyo
Exceptional Control Flow
Exceptional Control Flow
Exceptional Control Flow
Exceptional Control Flow
Presentation transcript:

1 Exceptional Control Flow III

2 Outline Loading and Running Programs Multitasking, shells Suggested reading 8.4

3 execve : Loading and Running Programs Loads and runs in current process –the executable object file filename –with the argument list argv –the environment variable list envp. #include int execve(char *filename, char *argv[], char *envp); does not return if OK, returns -1 on error

execve : Loading and Running Programs Does not return (unless error) –cannot find filename Overwrites code, data, and stack –keeps pid, open files and signal context Environment variables: –“name=value” strings –getenv and putenv Null-terminated env var strings unused Null-terminated cmd line arg strings envp[n] == NULL envp[n-1] envp[0] … Linker vars argv[argc] == NULL argv[argc-1] argv[0] … envp argc argv Stack bottom Stack frame for main Stack top environ 4

5 Loading and Running #include char *getenv(const char *name); returns: ptr to name if exists, NULL if no match. int putenv(char *string); returns: 0 on success, nonzero on error. int setenv(const char *name, const char *newvalue, int overwrite); returns: 0 on success, -1 on error. void unsetenv(const char *name); returns: nothing.

execve Example if ((pid = Fork()) == 0) { /* Child runs user job */ if (execve(argv[0], argv, environ) < 0) { printf("%s: Command not found.\n", argv[0]); exit(0); } envp[n] = NULL envp[n-1] envp[0] … argv[argc] = NULL argv[argc-1] argv[0] … “ls” “-lt” “/usr/include” “USER=droh” “PRINTER=iron” “PWD=/usr/droh” environ argv 6

The World of Multitasking System runs many processes concurrently Process: executing program –State includes memory image + register values + program counter Regularly 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 7

The World of Multitasking 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 Exceptions –Events that require nonstandard control flow –Generated externally (interrupts) or internally (traps and faults) 8

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 –execve runs new program in existing process Called once, (normally) never returns 9

Programmer’s Model of Multitasking Programming challenge –Understanding the nonstandard semantics of the functions –Avoiding improper use of system resources E.g. “Fork bombs” can disable a system 10

Unix Process Hierarchy Login shell Child Grandchild [0] Daemon e.g. httpd init [1] 11

Shell Programs A shell is an application program that runs programs on behalf of the user. –sh Original Unix shell (Stephen Bourne, AT&T Bell Labs, 1977) –csh BSD Unix C shell ( tcsh: enhanced csh at CMU and elsewhere ) –bash “ Bourne-Again” Shell int main() { char cmdline[MAXLINE]; while (1) { /* read */ printf("> "); Fgets(cmdline, MAXLINE, stdin); if (feof(stdin)) exit(0); /* evaluate */ eval(cmdline); } Execution is a sequence of read/evaluate steps 12

Simple Shell eval Function void eval(char *cmdline) { char *argv[MAXARGS]; /* argv for execve() */ int bg; /* should the job run in bg or fg? */ pid_t pid; /* process id */ bg = parseline(cmdline, argv); if (!builtin_command(argv)) { if ((pid = Fork()) == 0) { /* child runs user job */ if (execve(argv[0], argv, environ) < 0) { printf("%s: Command not found.\n", argv[0]); exit(0); } if (!bg) { /* parent waits for fg job to terminate */ int status; if (waitpid(pid, &status, 0) < 0) unix_error("waitfg: waitpid error"); } else /* otherwise, don’t wait for bg job */ printf("%d %s", pid, cmdline); } 13

What Is a “Background Job”? Users generally run one command at a time –Type command, read output, type another command Some programs run “for a long time” –Example: “delete this file in two hours” A “background” job is a process we don't want to wait for unix> sleep 7200; rm /tmp/junk # shell stuck for 2 hours unix> (sleep 7200 ; rm /tmp/junk) & [1] 907 unix> # ready for next command 14

15 Foreground and Background If the last argument is a “&” character –parseline returns 1 –indicating the program should be executed in the background (the shell returns to the top of the loop and waits for the next command) Otherwise –returns 0 –indicating that the program should be run in the foreground (the shell waits for it to complete)

16 Parsing command line Parse the space separated command-line arguments Builds the argv vector –which will eventually be passed to execve The first argument is assumed to be –either the name of a built-in shell command that is interpreted immediately –or an executable object file that will be loaded and run in the context of a new child process

Problem with Simple Shell Example Our example shell correctly waits for and reaps foreground jobs But what about background jobs? –Will become zombies when they terminate –Will never be reaped because shell (typically) will not terminate –Will create a memory leak that could run the kernel out of memory –Modern Unix: once you exceed your process quota, your shell can't run any new commands for you: fork() returns -1 unix> limit maxproc # csh syntax maxproc unix> ulimit -u # bash syntax

ECF to the Rescue! Problem –The shell doesn't know when a background job will finish –By nature, it could happen at any time –The shell's regular control flow can't reap exited background processes in a timely fashion –Regular control flow is “wait until running job completes, then reap it” Solution: Exceptional control flow –The kernel will interrupt regular processing to alert us when a background process completes –In Unix, the alert mechanism is called a signal 18

19 Outline Signals –Signal Terminology –Sending Signals Suggested reading 8.5

20 Signals Unix signal –A higher-level software form of exception –That allows processes to interrupt other processes

21 The kill Program Kill program –is located in directory /bin/ –sends an arbitrary signal to another process unix> kill –sends signal 9 (SIGKILL) to process

22 Signals Type Signal is a message that notifies a process –that an event of some type has occurred in the system. Each type corresponds to some kind of system event –Low-level hardware exceptions are processed by the kernel’s exception handlers would not normally be visible to user processes. Signals provide a mechanism for exposing the occurrence of such exceptions to user processes. –Higher-level software events in the kernel in other user processes

23 Hardware Events SIGFPE signal (number 8) –If a process attempts to divide by zero, then the kernel sends it a SIGFPE signal SIGILL signal (number 4) –If a process executes an illegal instruction, the kernel sends it a SIGILL signal. SIGSEGV signal (number 11) –If a process makes an illegal memory reference, the kernel sends it a SIGSEGV signal.

24 Software Events SIGINT signal (number 2) –While a process is running in the foreground –if you type a ctrl-c –then the kernel sends a SIGINT signal to the process SIGKILL signal (number 9) –A process can forcibly terminate another process –by sending it a SIGKILL signal SIGCHLD signal (number 17) –When a child process terminates or stops, –the kernel sends a SIGCHLD signal to the parent.

25 Signal Terminology Two steps to transfer a signal to a destination process –Sending a signal –Receiving a signal

26 Sending a signal The kernel –sends (delivers) a signal to a destination process by updating some state in the context of the destination process. The signal is delivered for one of two reasons: –The kernel has detected a system event –A process has invoked the kill function to explicitly request the kernel to send a signal to the destination process. A process can send a signal to itself.

27 Receiving 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 The process can –Ignore the signal –Terminate –Catch the signal by executing a user-level function called a signal handler

28 Pending Signal Pending signal –A signal that has been sent but not yet received Only one –At any point in time, –there can be at most one pending signal of a particular type Not queued –If a process has a pending signal of type k –then any subsequent signals of type k sent to that process are not queued –they are simply discarded. A pending signal is received at most once.

29 Blocking a Signal A process can selectively block the receipt of certain signals. When a signal is blocked –it can be delivered –but the resulting pending signal will not be received –until the process unblocks the signal

30 Internal Data Structures For each process, the kernel maintains –the set of pending signals in the pending bit vector –the set of blocked signals in the blocked bit vector The kernel sets bit k in pending –whenever a signal of type k is delivered The kernel clears bit k in pending –whenever a signal of type k is received.

31 Process Groups To send signals to processes –Unix systems provide a number of mechanisms –All of them rely on the notion of a process group Every process belongs to exactly one process group –which is identified by a positive integer process group ID. By default, a child process belongs to the same process group as its parent.

Process Groups Every process belongs to exactly one process group Fore- ground job Back- ground job #1 Back- ground job #2 Shell Child pid=10 pgid=10 Foreground process group 20 Background process group 32 Background process group 40 pid=20 pgid=20 pid=32 pgid=32 pid=40 pgid=40 pid=21 pgid=20 pid=22 pgid=20 getpgrp() Return process group of current process setpgid() Change process group of a process 32

33 Process Groups The getpgrp function: #include pid_t getpgrp(void); returns: process group ID of calling process The setpgrp function: #include pid_t setpgid(pid_t pid, pid_t pgid); returns: 0 on success, -1 on error

34 setpgid Function Changes the process group of process pid to pgid If pid is zero, the PID of the current process is used If pgid is zero, the PID of the process specified by pid is used for the process group ID

35 setpgid Function setpgid(0, 0) ; –Suppose process is the calling process, –then this function call creates a new process group whose process group ID is adds process to this new group.

Sending Signals with /bin/ kill Program /bin/kill program sends arbitrary signal to a process or process group Examples –/bin/kill – Send SIGKILL to process –/bin/kill –9 –24817 Send SIGKILL to every process in process group linux>./forks 16 Child1: pid=24818 pgrp=24817 Child2: pid=24819 pgrp=24817 linux> ps PID TTY TIME CMD pts/2 00:00:00 tcsh pts/2 00:00:02 forks pts/2 00:00:02 forks pts/2 00:00:00 ps linux> /bin/kill linux> ps PID TTY TIME CMD pts/2 00:00:00 tcsh pts/2 00:00:00 ps linux> 36

37 Sending Signals from the Keyboard Job –An abstraction used by Unix shells –To represent the processes that are created as a result of evaluating a single command line –Foreground –Background –unix> ls | sort

38 Sending Signals from the Keyboard The shell creates a separate process group for each job –Typically, the process group ID is taken from one of the parent processes in the job

39 Sending Signals from the Keyboard Fore- ground job Back- ground job #1 Back- ground job #2 Shell Child pid=10 pgid=10 Foreground process group 20 Background process group 32 Background process group 40 pid=20 pgid=20 pid=32 pgid=32 pid=40 pgid=40 pid=21 pgid=20 pid=22 pgid=20

40 Sending Signals from the Keyboard Typing ctrl-c at the keyboard –causes a SIGINT signal to be sent to the shell –The shell catches the signal –Then sends a SIGINT to every process in the foreground process group –The result is to terminate the foreground job (default)

41 Sending Signals from the Keyboard Typing crtl-z –sends a SIGTSTP signal to the shell –The shell catches the signal –Sends a SIGTSTP signal to every process in the foreground process group –The result is to stop (suspend) the foreground job (default)

Example of ctrl-c and ctrl-z bluefish>./forks 17 Child: pid=28108 pgrp=28107 Parent: pid=28107 pgrp=28107 Suspended bluefish> ps w PID TTY STAT TIME COMMAND pts/8 Ss 0:00 -tcsh pts/8 T 0:01./forks pts/8 T 0:01./forks pts/8 R+ 0:00 ps w bluefish> fg./forks 17 bluefish> ps w PID TTY STAT TIME COMMAND pts/8 Ss 0:00 -tcsh pts/8 R+ 0:00 ps w STAT (process state) Legend: First letter: S: sleeping T: stopped R: running Second letter: s: session leader +: foreground proc group See “man ps” for more details 42

43 Sending Signals with kill Function If pid is greater than zero, –then the kill function sends signal number sig to process pid If pid is less than zero –then kill sends signal sig to every process in process group abs(pid) #include int kill(pid_t pid, int sig); returns: 0 if OK, -1 on error

Sending Signals with kill Function 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); } 44

45 Next Signals –Receiving Signals –Signal Handling –Blocking Signals –Suggested reading 8.5 Nonlocal Jumps –Suggested reading 8.6