Top Banner
15213 Recitation Section C Process • Signals • Reaping Child Processes • Race Hazard Shimin Chen Oct. 28, 2002 Outli ne
23

15213 Recitation Section C

Feb 25, 2016

Download

Documents

meira

Shimin Chen. Oct. 28, 2002. 15213 Recitation Section C. Process Signals Reaping Child Processes Race Hazard. Outline. Process Concept. An instance of running program Multiple processes run “concurrently” by time slicing What is time slicing? - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 15213 Recitation Section C

15213 Recitation Section C

• Process• Signals• Reaping Child Processes• Race Hazard

Shimin ChenOct. 28, 2002

Outline

Page 2: 15213 Recitation Section C

215213 Recitation C Shimin Chen

Process Concept• An instance of running program• Multiple processes run “concurrently” by

time slicing– What is time slicing?– Preemptive scheduler of OS: it can stop a

program at any point!

Page 3: 15213 Recitation Section C

315213 Recitation C Shimin Chen

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() → −pid

Page 4: 15213 Recitation Section C

415213 Recitation C Shimin Chen

Process Tree for Shell

Fore-ground

job

Back-groundjob #1

Back-groundjob #2

Shell

Child Child

pid=10pgid=10

Foregroundprocess group 20

Backgroundprocess group 32

Backgroudprocess group 40

pid=20pgid=20

pid=32pgid=32

pid=40pgid=40

pid=21pgid=20

pid=22pgid=20

Page 5: 15213 Recitation Section C

515213 Recitation C Shimin Chen

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

Page 6: 15213 Recitation Section C

615213 Recitation C Shimin Chen

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

Page 7: 15213 Recitation Section C

715213 Recitation C Shimin Chen

Signals: sending

OS Kernel

blockedpending1

Process 1 Process 2

other events

OS procedure

kill(pid, SIGINT)

• divide by zero: SIGFPE

• ctrl-c: SIGINT

• child process exit: SIGCHLD

Page 8: 15213 Recitation Section C

815213 Recitation C Shimin Chen

Signals: receiving

OS Kernel

blockedpending1

Process 2

OS procedure

0

Check when schedule the process to run

Page 9: 15213 Recitation Section C

915213 Recitation C Shimin Chen

Receiving a Signal• Default action

– The process terminates [and dumps core]– The process stops until restarted by a

SIGCONT signal– The process ignore the signal

• Can modify (additional action)– “Handle the signal”

•void sigint_handler(int sig);•signal(SIGINT, sigint_handler);

Page 10: 15213 Recitation Section C

1015213 Recitation C Shimin Chen

Reaping Child Process

• Child process becomes zombie when terminates– Still consume system resources– Parent performs reaping on terminated child– wait() waitpid()

• Straightforward for reaping a single child• Tricky for Shell implementation!

– multiple child processes– both foreground and background

Page 11: 15213 Recitation Section C

1115213 Recitation C Shimin Chen

Reaping Child Process• Two waits

– sigchld_handler– eval: for foreground processes

• One wait– sigchld_handler– But what about foreground processes?

Page 12: 15213 Recitation Section C

1215213 Recitation C Shimin Chen

Busy Waitif(fork() != 0) { /* parent */ addjob(…); while(fg process still alive){ /* do nothing */ }}

Page 13: 15213 Recitation Section C

1315213 Recitation C Shimin Chen

Pauseif(fork() != 0) { /* parent */ addjob(…); while(fg process still alive){ pause(); }} If signal handled before call to pause,

then pause will not return when foreground process sends SIGCHLD

Page 14: 15213 Recitation Section C

1415213 Recitation C Shimin Chen

Sleepif(fork() != 0) { /* parent */ addjob(…); while(fg process still alive){ sleep(1); }}

Page 15: 15213 Recitation Section C

1515213 Recitation C Shimin Chen

waitpid ()pid_t waitpid(pid_t pid, int *status, int options)

– pid: wait until child process with pid has terminated• -1: wait for any child process

– status: tells why child terminated– options:

• WNOHANG: return immediately if no children zombied– returns -1

• WUNTRACED: report status of stopped children too

• wait (&status) equivalent to waitpid (-1, &status,0)

Page 16: 15213 Recitation Section C

1615213 Recitation C Shimin Chen

Status in Waitpid

• int status;waitpid(pid, &status, NULL)

• Macros to evaluate status:– WIFEXITED(status): child exited normally– WEXITSTATUS(status): return code when child exits

– WIFSIGNALED(status): child exited because of a signal not caught

– WTERMSIG(status): gives the terminating signal number

– WIFSTOPPED(status): child is currently stopped– WSTOPSIG(status): gives the stop signal number

Page 17: 15213 Recitation Section C

1715213 Recitation C Shimin Chen

Man page

• Check man page for details of a system call:– man waitpid

Page 18: 15213 Recitation Section C

1815213 Recitation C Shimin Chen

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.

Page 19: 15213 Recitation Section C

1915213 Recitation C Shimin Chen

eval & sigchld_handler Race Hazardsigchld_handler() { pid = waitpid(…); deletejob(pid);}

eval() { pid = fork(); if(pid == 0) { /* child */ execve(…); } /* parent */ /* signal handler might run BEFORE addjob() */ addjob(…);}

Page 20: 15213 Recitation Section C

2015213 Recitation C Shimin Chen

An Okay Schedule

Shell Signal Handler Childfork()addjob()

execve()exit()

sigchld_handler()deletejobs()

time

Page 21: 15213 Recitation Section C

2115213 Recitation C Shimin Chen

A Problematic Schedule

Shell Signal Handler Childfork()

execve()exit()

sigchld_handler()deletejobs()

time

addjob()

Job added to job list after the signal handler tried to delete it!

Page 22: 15213 Recitation Section C

2215213 Recitation C Shimin Chen

Blocking Signalssigchld_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(…); sigprocmask(SIG_UNBLOCK, …)}

More details 8.5.6 (page 633)

Page 23: 15213 Recitation Section C

2315213 Recitation C Shimin Chen

Summary

• Process• Signals• Reaping Child Processes• Race Hazard

• Check man page to understand the system calls better– man waitpid