DOC PREVIEW
CMU CS 15213 - Recitation

This preview shows page 1-2-22-23 out of 23 pages.

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

Unformatted text preview:

15 213 Fall 06 Outline Shell Lab Processes Signals 1 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 Make a new process group for myself and my children setpgid 0 0 2 Process concurrency int fork void Create a new process identical to parent process Return 0 to child process Return child s pid to the parent process Any scheduling order of processes is possible Unless code uses explicit synchronization Context switching can happen at any point 3 Lab 5 A tiny shell with job control I O redirection Key points Reap all child processes Handle SIGCHLD SIGTSTP SIGINT Avoid race hazards 4 How to Send Signals To a single process int kill pid t pid int sig To every process in group abs gid int kill pid t gid int sig where gid is negative Can we use signals to count events No Why Signals not queued 5 Signals sending Process 1 Process 2 kill pid SIGINT 1 blocked pending OS signal manager divide by zero SIGFPE ctrl c SIGINT child process exit SIGCHLD other events OS Kernel 6 Signals receiving OS delivers the pending nonblocked signals Process 2 0 1 blocked pending OS signal manager OS Kernel 7 Key signals in the shell lab SIGINT Triggered by Interrupt signal ctrl c Default action the process terminates SIGTSTP Triggered by Stop signal from terminal ctrl z Default action the process stops Can be restarted later by sending SIGCONT to it SIGCHLD Triggered by a child process has stopped or terminated 8 Shell the process tree pid 10 pgid 10 Background job 1 pid 20 Forepgid 20 ground job Child pid 21 pgid 20 Shell Child pid 22 pgid 20 Foreground process group 20 pid 32 pgid 32 Background process group 32 Background job 2 pid 40 pgid 40 Backgroud process group 40 Each job has a unique process group id int setpgid pid t pid pid t pgid setpgid 0 0 9 Process tree for tsh pid 5 pgid 5 pid 10 pgid 10 rd a rw Fo pid 21 pgid 20 Child pid 22 pgid 20 Foreground process group 20 Foreground job receives SIGINT SIGTSTP when you type ctrl c ctrl z tsh Background job 1 pid 20 Forepgid 20 ground job Child als n sig UNIX shell pid 32 pgid 32 Background process group 32 Background job 2 pid 40 pgid 40 Backgroud process group 40 int kill pid t pid int sig pid 0 send sig to process with PID pid pid 0 send sig to all processes in my group pid 1 send sig to all processes with PID 1 10 pid 1 send sig to group abs pid Execute program int execve const char fname char const argv char const envp Examples execve bin ls NULL NULL execve mytest argv envp What happens to the caller process It effectively terminates The new program overwrites its state and takes its PID Any signal handlers installed by the caller are reset 11 Reaping terminated child processes pid t waitpid pid t pid int status int options pid 0 wait for process with PID pid 1 wait for any process pid 1 wait for any process from group abs pid By default waitpid blocks until at least one zombie process becomes available options WNOHANG return immediately if no zombies available WUNTRACED also return if some process has been stopped WNOHANG WUNTRACED combination is very useful in the shell lab it detects all the necessary events and doesn t block if no events 12 Reaping terminated child processes pid t waitpid pid t pid int status int options pid 0 wait for process with PID pid 1 wait for any process pid 1 wait for any process from group abs pid Return value pid of the process that exited or zero if WNOHANG was used and no zombie process available or 1 on error then see errno status gives info on why the process terminated or stopped if WUNTRACED used 13 Status int status waitpid pid status WNOHANG WUNTRACED Macros to evaluate status WIFEXITED status process exited normally WEXITSTATUS status exit code of the process WIFSIGNALED status process exited because a signal was not caught SIGINT SIGKILL etc WTERMSIG status identifies the signal that was not caught WIFSTOPPED status process was stopped WSTOPSIG status identifies the stopping signal 14 Reaping child process in tsh Where to put waitpid As the handout suggests One centralized reaping for both fg and bg In sigchld handler 15 Busy wait for foreground job tsh should still wait for fg job to complete how At an appropriate place in eval while fg process still alive do nothing 16 Better than simple busy waiting Sleep At an appropriate place in eval while fg process still alive sleep 1 17 Race hazards 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 a race hazard sigchld handler waitpid deletejob pid eval pid fork if pid 0 child execve parent signal handler may run BEFORE addjob addjob 19 Solution blocking signals eval sigprocmask SIG BLOCK pid fork if pid 0 child sigprocmask SIG UNBLOCK execve parent signal handler might run BEFORE addjob addjob sigprocmask SIG UNBLOCK 20 I O Redirection Covered in Chapter 11 Make file descriptor newfd a copy of oldfd dup2 int oldfd int newfd Get input from my infd instead of standard input dup2 my infd STDIN FILENO Make a copy of a file descriptor standard output in this case int my outfd dup STDOUT FILENO 21 Reminders Some important system calls fork execve waitpid sigprocmask setpgid kill Check man pages for details about system calls man 2 kill Check return values of all system calls Flush output buffers fflush stdout STEP by STEP Test your shell by typing commands first Start now 22 What are the possible program outputs include unistd h include stdio h int cnt 0 int main void if fork 0 cnt in child fork cnt cnt printf d cnt return 0 Possible outputs 133 313 331 23


View Full Document

CMU CS 15213 - Recitation

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

43 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 Recitation
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 Recitation 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 Recitation 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?