DOC PREVIEW
CMU CS 15213 - Lecture

This preview shows page 1-2-3-20-21-22-41-42-43 out of 43 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

15 213 The course that gives CMU its Zip Exceptional Control Flow Part II October 7 2008 Topics lecture 12 ppt Process Hierarchy Shells Signals Nonlocal jumps ECF Exists at All Levels of a System Exceptions Hardware and operating system kernel software Previous Lecture Concurrent processes Hardware timer and kernel software Signals Kernel software This Lecture Non local jumps 2 Application code 15 213 F 08 The World of Multitasking System Runs Many Processes Concurrently Process executing program z State includes memory image register values program counter Regularly switches from one process to another z Suspend process when it needs I O resource or timer event occurs z Resume process when I O available or given scheduling priority Appears to user s as if all processes executing simultaneously z Even though most systems can only execute one process at a time z Except possibly with lower performance than if running alone 3 15 213 F 08 Programmer s Model of Multitasking Basic Functions fork spawns new process z Called once returns twice exit terminates own process z Called once never returns z Puts it into zombie status wait and waitpid wait for and reap terminated children execl and execve run new program in existing process z Called once normally never returns Programming Challenge Understanding the nonstandard semantics of the functions Avoiding improper use of system resources z E g Fork bombs can disable a system 4 15 213 F 08 wait Synchronizing with Children int wait int child status 5 suspends current process until one of its children terminates return value is the pid of the child process that terminated if child status NULL then the object it points to will be set to a status indicating why the child process terminated 15 213 F 08 wait Synchronizing with Children void fork9 int child status if fork 0 printf HC hello from child n else printf HP hello from parent n wait child status printf CT child has terminated n printf Bye n exit 6 HC Bye HP CT Bye 15 213 F 08 wait Example If multiple children completed will take in arbitrary order Can use macros WIFEXITED and WEXITSTATUS to get information about exit status void fork10 pid t pid N int i int child status for i 0 i N i if pid i fork 0 exit 100 i Child 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 15 213 F 08 7 waitpid Waiting for a Specific Process waitpid pid status options z suspends current process until specific process terminates z various options that we won t talk about void fork11 pid t pid N int i int child status for i 0 i N i if pid i fork 0 exit 100 i Child for i 0 i N i pid t wpid waitpid pid i child status 0 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 8 15 213 F 08 exec Loading and Running Programs int execl char path char arg0 char arg1 0 Loads and runs executable at path with args arg0 arg1 z path is the complete path of an executable object file z By convention arg0 is the name of the executable object file z Real arguments to the program start with arg1 etc z List of args is terminated by a char 0 argument z Environment taken from char environ which points to an array of name value strings USER droh LOGNAME droh HOME afs cs cmu edu user droh 9 Returns 1 if error otherwise doesn t return Family of functions includes execv execve base function execvp execl execle and execlp 15 213 F 08 exec Loading and Running Programs main if fork 0 execl usr bin cp cp foo bar 0 wait NULL printf copy completed n exit 10 15 213 F 08 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 csh enhanced 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 Execution is a sequence of read evaluate steps evaluate eval cmdline 11 15 213 F 08 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 12 15 213 F 08 Background Job What is a background job Users generally run one command at a time zType command read output type another command Some programs run for a long time zExample delete this file in two hours sleep 7200 rm tmp junk hours A shell stuck for 2 background job is a process we don t want to wait for sleep 7200 rm tmp junk 1 907 ready for next command 13 15 213 F 08 Problem with Simple Shell 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 theoretically run the kernel out of memory zIn modern Unix once you exceed your process quota your shell can t run any new commands for you fork returns 1 limit maxproc csh syntax maxproc 3574 ulimit u bash syntax 3574 14 15 213 F 08 ECF to the Rescue Problem The By shell doesn t know when a background job will finish nature it could happen at any time The shell s regular control flow can t reap exited background processes in a timely fashion zRegular 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 15 Unix the alert mechanism is called a signal 15 213 F 08 Signals A signal is a small message that notifies a process that an event of some type has occurred in the system ID akin to exceptions and interrupts sent from the kernel sometimes at the request of another process to a process signal type is identified by small integer ID s 1 30 only information in a signal is its ID and the fact that it arrived Name 2 SIGINT 9 SIGKILL 11 SIGSEGV 14 SIGALRM Default Action Terminate 17 SIGCHLD Ignore 16 Corresponding Event Interrupt e g ctl c from keyboard Terminate Kill …


View Full Document

CMU CS 15213 - Lecture

Documents in this Course
lecture

lecture

14 pages

lecture

lecture

46 pages

Caches

Caches

9 pages

lecture

lecture

39 pages

Lecture

Lecture

36 pages

Lecture

Lecture

45 pages

Lecture

Lecture

56 pages

lecture

lecture

11 pages

lecture

lecture

9 pages

Lecture

Lecture

36 pages

Lecture

Lecture

37 pages

Exam

Exam

16 pages

Lecture

Lecture

10 pages

Lecture

Lecture

43 pages

Lecture

Lecture

8 pages

Lecture

Lecture

8 pages

Lecture

Lecture

36 pages

Lecture

Lecture

43 pages

Lecture

Lecture

12 pages

Lecture

Lecture

37 pages

Lecture

Lecture

6 pages

Lecture

Lecture

40 pages

coding

coding

2 pages

Exam

Exam

17 pages

Exam

Exam

14 pages

Lecture

Lecture

29 pages

Lecture

Lecture

34 pages

Exam

Exam

11 pages

Lecture

Lecture

9 pages

Lecture

Lecture

37 pages

Lecture

Lecture

36 pages

lecture

lecture

46 pages

Lecture

Lecture

33 pages

Lecture

Lecture

57 pages

Lecture

Lecture

32 pages

Lecture

Lecture

46 pages

Lecture

Lecture

40 pages

Lecture

Lecture

11 pages

Lecture

Lecture

6 pages

Lecture

Lecture

12 pages

Lecture

Lecture

18 pages

Exam

Exam

10 pages

Lecture

Lecture

45 pages

Lecture

Lecture

37 pages

Exam

Exam

24 pages

class09

class09

21 pages

class22

class22

37 pages

class20

class20

30 pages

class27

class27

33 pages

class25

class25

21 pages

class04

class04

31 pages

Lecture

Lecture

59 pages

class01a

class01a

14 pages

class12

class12

45 pages

class29

class29

33 pages

Lecture

Lecture

39 pages

Lecture

Lecture

6 pages

class03

class03

34 pages

lecture

lecture

42 pages

Lecture

Lecture

40 pages

Lecture

Lecture

47 pages

Exam

Exam

19 pages

R06-B

R06-B

25 pages

class17

class17

37 pages

class25

class25

31 pages

Lecture

Lecture

15 pages

final-f06

final-f06

17 pages

Lecture

Lecture

9 pages

lecture

lecture

9 pages

Exam

Exam

15 pages

Lecture

Lecture

22 pages

class11

class11

45 pages

lecture

lecture

50 pages

Linking

Linking

37 pages

Lecture

Lecture

64 pages

Integers

Integers

40 pages

Exam

Exam

11 pages

Lecture

Lecture

37 pages

Lecture

Lecture

44 pages

Lecture

Lecture

37 pages

Lecture

Lecture

9 pages

Lecture

Lecture

37 pages

Lecture

Lecture

45 pages

Final

Final

25 pages

lecture

lecture

9 pages

Lecture

Lecture

30 pages

Lecture

Lecture

16 pages

Final

Final

17 pages

Lecture

Lecture

8 pages

Exam

Exam

11 pages

Lecture

Lecture

47 pages

Lecture

Lecture

9 pages

lecture

lecture

39 pages

Exam

Exam

11 pages

lecture

lecture

41 pages

lecture

lecture

37 pages

Lecture

Lecture

59 pages

Lecture

Lecture

45 pages

Exam 1

Exam 1

18 pages

Lecture

Lecture

41 pages

Lecture

Lecture

32 pages

Lecture

Lecture

30 pages

Lecture

Lecture

9 pages

Lecture

Lecture

9 pages

Lecture

Lecture

15 pages

Lecture

Lecture

11 pages

Lecture

Lecture

9 pages

Lecture

Lecture

34 pages

Lecture

Lecture

40 pages

Lecture

Lecture

4 pages

Lecture

Lecture

46 pages

Lecture

Lecture

8 pages

Lecture

Lecture

65 pages

Lecture

Lecture

38 pages

Lecture

Lecture

35 pages

Lecture

Lecture

8 pages

Lecture

Lecture

34 pages

Lecture

Lecture

8 pages

Exam

Exam

13 pages

Lecture

Lecture

43 pages

Lecture

Lecture

9 pages

Lecture

Lecture

12 pages

Lecture

Lecture

9 pages

Lecture

Lecture

34 pages

Lecture

Lecture

43 pages

Lecture

Lecture

7 pages

Lecture

Lecture

45 pages

Lecture

Lecture

24 pages

Lecture

Lecture

47 pages

Lecture

Lecture

12 pages

Lecture

Lecture

20 pages

Lecture

Lecture

9 pages

Exam

Exam

11 pages

Lecture

Lecture

52 pages

Lecture

Lecture

20 pages

Exam

Exam

11 pages

Lecture

Lecture

35 pages

Lecture

Lecture

47 pages

Lecture

Lecture

18 pages

Lecture

Lecture

30 pages

Lecture

Lecture

59 pages

Lecture

Lecture

37 pages

Lecture

Lecture

22 pages

Lecture

Lecture

35 pages

Exam

Exam

23 pages

Lecture

Lecture

9 pages

Lecture

Lecture

22 pages

class12

class12

32 pages

Lecture

Lecture

8 pages

Lecture

Lecture

39 pages

Lecture

Lecture

44 pages

Lecture

Lecture

38 pages

Lecture

Lecture

69 pages

Lecture

Lecture

41 pages

Lecture

Lecture

12 pages

Lecture

Lecture

52 pages

Lecture

Lecture

59 pages

Lecture

Lecture

39 pages

Lecture

Lecture

83 pages

Lecture

Lecture

59 pages

class01b

class01b

17 pages

Exam

Exam

21 pages

class07

class07

47 pages

Lecture

Lecture

11 pages

Odyssey

Odyssey

18 pages

multicore

multicore

66 pages

Lecture

Lecture

6 pages

lecture

lecture

41 pages

lecture

lecture

55 pages

lecture

lecture

52 pages

lecture

lecture

33 pages

lecture

lecture

46 pages

lecture

lecture

55 pages

lecture

lecture

17 pages

lecture

lecture

49 pages

Exam

Exam

17 pages

lecture

lecture

56 pages

Exam 2

Exam 2

16 pages

Exam 2

Exam 2

16 pages

Notes

Notes

37 pages

Lecture

Lecture

40 pages

Lecture

Lecture

36 pages

Lecture

Lecture

43 pages

Lecture

Lecture

25 pages

Exam

Exam

13 pages

Lecture

Lecture

32 pages

Lecture

Lecture

12 pages

Lecture

Lecture

58 pages

Lecture

Lecture

29 pages

Lecture

Lecture

59 pages

Lecture

Lecture

41 pages

Lecture

Lecture

50 pages

Exam

Exam

17 pages

Lecture

Lecture

29 pages

Lecture

Lecture

44 pages

Lecture

Lecture

41 pages

Lecture

Lecture

52 pages

Lecture

Lecture

40 pages

Lecture

Lecture

33 pages

lecture

lecture

10 pages

Lecture

Lecture

27 pages

Lecture

Lecture

29 pages

Lecture

Lecture

39 pages

Lecture

Lecture

9 pages

Lecture

Lecture

29 pages

Lecture

Lecture

8 pages

Lecture

Lecture

43 pages

Lecture

Lecture

43 pages

Lecture

Lecture

75 pages

Lecture

Lecture

55 pages

Exam

Exam

12 pages

Lecture

Lecture

43 pages

Lecture

Lecture

35 pages

lecture

lecture

36 pages

Exam

Exam

33 pages

lecture

lecture

56 pages

lecture

lecture

64 pages

lecture

lecture

8 pages

Exam

Exam

14 pages

Lecture

Lecture

43 pages

Lecture

Lecture

36 pages

lecture

lecture

56 pages

lecture

lecture

75 pages

lecture

lecture

36 pages

Lecture

Lecture

50 pages

Lecture

Lecture

45 pages

Lecture

Lecture

13 pages

Exam

Exam

23 pages

Lecture

Lecture

10 pages

Lecture

Lecture

48 pages

Lecture

Lecture

83 pages

lecture

lecture

57 pages

Lecture

Lecture

33 pages

Lecture

Lecture

39 pages

Lecture

Lecture

33 pages

lecture

lecture

54 pages

Lecture

Lecture

30 pages

Exam

Exam

13 pages

Lecture

Lecture

36 pages

Lecture

Lecture

40 pages

Exam

Exam

17 pages

Lecture

Lecture

9 pages

Exam

Exam

15 pages

Lecture

Lecture

44 pages

Lecture

Lecture

34 pages

Lecture

Lecture

24 pages

Lecture

Lecture

29 pages

class12

class12

43 pages

lecture

lecture

43 pages

class22

class22

22 pages

R06-B

R06-B

25 pages

class01b

class01b

19 pages

lecture

lecture

29 pages

lab1

lab1

8 pages

Caches

Caches

36 pages

lecture

lecture

55 pages

Lecture,

Lecture,

37 pages

Integers

Integers

40 pages

Linking

Linking

38 pages

lecture

lecture

45 pages

Lecture

Lecture

61 pages

Linking

Linking

33 pages

lecture

lecture

40 pages

lecture

lecture

40 pages

Lecture

Lecture

32 pages

lecture

lecture

48 pages

lecture

lecture

44 pages

Exam

Exam

11 pages

Lecture

Lecture

31 pages

Lecture

Lecture

46 pages

Lecture

Lecture

40 pages

Lecture

Lecture

40 pages

Exam

Exam

12 pages

Lecture

Lecture

42 pages

Lecture

Lecture

36 pages

Lecture

Lecture

45 pages

Lecture

Lecture

41 pages

Lecture

Lecture

13 pages

Lecture

Lecture

35 pages

Lecture

Lecture

20 pages

Final

Final

19 pages

Lecture

Lecture

33 pages

Lecture

Lecture

50 pages

Lecture

Lecture

33 pages

Lecture

Lecture

27 pages

Lecture

Lecture

6 pages

Exam

Exam

15 pages

Lecture

Lecture

24 pages

Lecture

Lecture

23 pages

Lecture

Lecture

43 pages

Lecture

Lecture

32 pages

Lecture

Lecture

52 pages

Lecture

Lecture

37 pages

Lecture

Lecture

36 pages

Lecture

Lecture

34 pages

Lecture

Lecture

40 pages

Lecture

Lecture

15 pages

lecture

lecture

21 pages

Lecture

Lecture

58 pages

Lecture

Lecture

49 pages

Lecture

Lecture

36 pages

Lecture

Lecture

11 pages

Lecture

Lecture

12 pages

Lecture

Lecture

58 pages

Lecture

Lecture

33 pages

Exam

Exam

15 pages

Lecture

Lecture

35 pages

Lecture

Lecture

10 pages

Lecture

Lecture

25 pages

Lecture

Lecture

31 pages

Lecture

Lecture

24 pages

Lecture

Lecture

34 pages

Lecture

Lecture

50 pages

lecture

lecture

35 pages

Lecture

Lecture

11 pages

Lecture

Lecture

39 pages

Lecture

Lecture

45 pages

Lecture

Lecture

41 pages

exam1-f05

exam1-f05

11 pages

Lecture

Lecture

4 pages

Lecture

Lecture

17 pages

Exam

Exam

17 pages

malloc()

malloc()

12 pages

Lecture

Lecture

57 pages

Lecture

Lecture

30 pages

Lecture

Lecture

30 pages

Lecture

Lecture

47 pages

Lecture

Lecture

33 pages

Exam

Exam

12 pages

Lecture

Lecture

43 pages

Lectures

Lectures

33 pages

Lecture

Lecture

36 pages

lecture

lecture

33 pages

Exam

Exam

14 pages

Lecture

Lecture

43 pages

Lecture

Lecture

25 pages

Load more
Download Lecture
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?