15-213 Recitation 8: 10/28/02 Outline Processes Signals –Racing Hazard –Reaping Children Annie Luo Office Hours: Thursday 6:00.

Slides:



Advertisements
Similar presentations
Process Management.
Advertisements

Recitation 8 (Nov. 1) Outline Process & job control Lab 5 Reminder Lab 5: Due Thursday Minglong Shao Office hours: Thursdays 5-6PM.
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
CS283 Systems Programming Process Management and Signals
University of Washington Today Midterm grades posted  76. HW 3 due Lab 4 fun! 1.
Last Lab: Writing a shell
Carnegie Mellon 1 Exceptional Control Flow : Introduction to Computer Systems Recitation 9: Monday, October 21 st, 2013 Ian Hartwig Section E.
15-213, Fall 06 Outline Shell Lab Processes Signals.
System Programming Project 2 Due : 4/19...?. Foreground & Background Foreground job Foreground job Only one at moment Only one at moment Having user’s.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
Recitation 8 (Nov. 1) Outline Lab 5 hints Virtual Memory Reminder Shell Lab: Due THIS Thursday TA: Kun Gao Modified from Minglong Shao’s Recitation, Fall.
Process Process: the UNIX abstraction of a stand-along computer that manages resources (memory, CPU, I/O resources) comprising a running program. Processes.
Signals Hua LiSystems ProgrammingCS2690Signals. Topics: Sending Signals -- kill(), raise() Signal Handling -- signal() sig_talk.c -- complete example.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
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
CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
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.
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
Introduction to Processes CS Intoduction to Operating Systems.
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla.
The process concept (section 3.1, 3.3 and demos)  Process: An entity capable of requesting and using computer resources (memory, CPU cycles, files, etc).
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Agenda  Working with Processes: Purpose Running Programs within same process (execl, execlp, execle, execv, execvp, execve) “Spawning” other process (fork,
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.
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
Operating Systems Process Creation
1 Signals (continued) CS 241 April 9, 2012 University of Illinois.
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.
Signals. Introduction r A signal is a mechanism for notifying a process that an event has occurred. m When a signal is sent to a process is normal execution.
Recitation: Signaling S04, Recitation, Section A Debug Multiple Processes using GDB Debug Multiple Processes using GDB Dup2 Dup2 Signaling Signaling.
Carnegie Mellon 1 Processes, Signals, I/O, Shell Lab : Introduction to Computer Systems Recitation 9: Monday, Oct. 21, 2013 Marjorie Carlson Section.
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)
1 Exceptional Control Flow III. 2 Outline Loading and Running Programs Multitasking, shells Suggested reading 8.4.
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.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
G.Jyostna.
Recitation 7 (Oct. 25) Outline Minglong Shao Office hours: Reminders
CS 3214 Introduction to Computer Systems
Task Control: Signals and Alarms Chapter 7 and 8
Unix Process Management
UNIX PROCESSES.
Exceptional Control Flow Part II
Exceptional Control Flow
Tarek Abdelzaher Vikram Adve Marco Caccamo
Exceptional Control Flow
Exceptional Control Flow Part II
Exceptional Control Flow
LINUX System Programming with Processes (additional)
Exceptional Control Flow
Processes Prof. Ikjun Yeom TA – Mugyo
Exceptional Control Flow
Exceptional Control Flow
Signals.
Exceptional Control Flow
Exceptional Control Flow
System Programming: Process Management
Presentation transcript:

Recitation 8: 10/28/02 Outline Processes Signals –Racing Hazard –Reaping Children Annie Luo Office Hours: Thursday 6:00 – 7:00 Wean 8402 Reminder L5 due: Halloween night, 11:59pm

How Programmers Play with Processes Process: executing copy of program Basic functions –fork() spawns new process group –exit() terminates own process –wait() and waitpid() wait for and reap terminated children –execl() and execve() run a new program in an existing process –kill() send arbitrary signal to process or process group kill(-pid, SIGINT) sends SIGINT to every process in pg “pid”

Process IDs and Process Groups Each process has its own, unique process ID –pid_t getpid(); Each process belongs to exactly one process group –pid_t getpgid(); Which process group does a new process belong to? –Its parent’s process group –pid_t getppid(); A process can make a process group for itself and its children –setpgid(pid, pgid); –Use setpgid(0,0) to put the child in a new pgroup other than the shell

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 Backgroud process group 40 pid=20 pgid=20 pid=32 pgid=32 pid=40 pgid=40 pid=21 pgid=20 pid=22 pgid=20 Process Groups

Signals Section 8.5 in text book –Read at least twice, really! Used to reap background jobs –waitpid() alone is not enough –Background jobs become zombies A signal tells our process that some event has occurred in the system Can we use signal to count events? –No

Important Signals SIGINT –Interrupt signal from keyboard (ctrl-c), terminates the current foreground job SIGTSTP –Stop signal from keyboard (ctrl-z), suspends current job until next SIGCONT SIGCHLD –A child process has terminated (becomes a zombie) or stopped Look at Figure 8.23 for a complete list of Linux signals

Signals – Sending A signal is sent through the kernel to a process When does the kernel send a signal? –The kernel detects a system event, e.g.: Divide-by-zero (SIGFPE) Termination of a child process (SIGCHLD) –Another process invokes a system call, e.g.: kill(pid_t pid, int SIGINT) alarm(unsigned int secs)

Signals – Receiving A process receives a signal when the kernel forces it to react to the signal Three default ways to react to a signal –The process ignores the signal –The process terminates (and dump core) –The process stops until next SIGCONT Can modify the default action (except SIGSTOP and SIGKILL) by executing signal handler functions –signal(SIGINT, sigint_handler)

Signal Handling Issues Subtle when deal with multiple signals Pending signals are blocked and not queued –Same type of signal currently being processed by handler –Not received until after the current handler returns –At most ONE pending signal of the same type at any time pending bit vector: bit k is set when signal type k is delivered, clear when signal received blocked bit vector: can be set by the program using the sigprocmask function

Synchronizing Parent and Children Preemptive scheduler run multiple programs “concurrently” by time slicing –How does time slicing work? –The scheduler can stop a program at any point –Signal handler code can run at any point, too Program behaviors depend on how the scheduler interleaves the execution of processes Racing condition between parent and child! –Why?

sigchld_handler() { pid = waitpid(…); deletejob(pid); } void eval() { pid = fork(); if(pid == 0){ /* child */ execve(…); } /* parent */ /* signal handler might run BEFORE addjob() */ addjob(…); } Parent & Children Racing Hazard

An Okay Schedule Shell Signal HandlerChild fork() addjob() execve() exit() sigchld_handler() deletejobs() time

A Problematic Schedule Shell Signal HandlerChild fork() execve() exit() sigchld_handler() deletejobs() time addjob() Job added to job list after the signal handler tried to delete it!

sigchld_handler() { pid = waitpid(…); deletejob(pid); } void 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, …); } Solution to P&C Racing Hazard More details see section (p.633)

Waiting for Foreground Child Process What’s the parent’s behavior for foreground child? It is blocked: –In eval() the parent uses a busy loop checking foreground child pid –Parent pauses if fg child processes is still alive

void eval() { … /* parent */ addjob(…); sigprocmask(SIG_UNBLOCK, …); while (fg process still alive) pause(); } sigchld_handler() { pid = waitpid(…); deletejob(pid); } Busy Loop: Pause vs. Sleep? pause() causes the invoking process to sleep until a signal is received. What’s the problem here? If signal is handled before pause is called, then it will not return when fg process sends SIGCHLD

void eval() { … /* parent */ addjob(…); sigprocmask(SIG_UNBLOCK, …); while (fg process still alive) sleep(1); } sigchld_handler() { pid = waitpid(…); deletejob(pid); } Busy Loop: Pause vs. Sleep? Use sleep instead

Reaping Child Process Child process becomes zombie when terminates –Still consume system resources –Parent performs reaping on terminated child –Where to wait children processes to terminate? Suggested solution: –In sigchld_handler() use waitpid(), detecting any terminated or stopped jobs and reaping them

Waitpid Called in signal handler of SIGCHLD –Reaps all available zombie children –Does not wait for any other currently running children Waitpid in detail: pid_t waitpid(pid_t pid, int *status, int options) pid: child process ID being waited to terminate pid = -1: wait for any child process status : tells why child terminated options : WNOHANG : return immediately if no children has exited (zombied) WUNTRACED : return for stopped children (status not reported)

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 number of the term. signal –WIFSTOPPED(status): child is currently stopped –WSTOPSIG(status): gives the number of the stop signal

Summary Process provides applications with the illusions of: –Exclusively use of the processor and the main memory At the interface with OS, applications can: –Creating child processes –Run new programs –Catch signals from other processes Use man if anything is not clear! Coding style issues