recitation 8: signals & shells
DESCRIPTION
Recitation 8: Signals & Shells. Andrew Faulring 15213 Section A 28 October 2002. Andrew Faulring. [email protected] Office hours: NSH 2504 (lab) / 2507 (conference room) Thursday 5-6 Lab 5 due Thursday, 31 Oct @ 11:59pm Halloween Night … happy reaping!. Today’s Plan. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/1.jpg)
Recitation 8:Signals & Shells
Andrew Faulring15213 Section A28 October 2002
![Page 2: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/2.jpg)
Andrew Faulring• [email protected]• Office hours:
– NSH 2504 (lab) / 2507 (conference room)– Thursday 5-6
• Lab 5– due Thursday, 31 Oct @ 11:59pm
• Halloween Night … happy reaping!
![Page 3: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/3.jpg)
Today’s Plan• Process IDs & Process Groups• Process Control• Signals• Preemptive Scheduler
– Race hazards• Reaping Child Processes
![Page 4: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/4.jpg)
Lab 5: Shell• tshref
– Use as a guide for output– You shell should have same behavior
![Page 5: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/5.jpg)
How Programmers Play with Processes
• Process: executing copy of program• Basic functions
– fork() spawns new process– exit() terminates calling process– wait() and waitpid() wait for and
reap terminated children– execl() and execve() run a new
program in an existing process
![Page 6: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/6.jpg)
Process IDs & Process Groups
• Each process has its own, unique process ID– pid_t getpid();
• Each process belongs to exactly one process group– pid_t getpgid();
• To which process group does a new process initially belong?– Its parent’s process group
• A process can make a process group for itself and its children– setpgid(0, 0);
![Page 7: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/7.jpg)
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 8: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/8.jpg)
Signals• Section 8.5 in text
– Read at least twice … really!• A signal tells our program that some
event has occurred– For instance, a child process has
terminated• Can we use signals to count events?
– No
![Page 9: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/9.jpg)
Important Signals• SIGINT
– Interrupt signal from keyboard (ctrl-c)• SIGTSTP
– Stop signal from keyboard (ctrl-z)• SIGCHLD
– A child process has stopped or terminated
Look at Figure 8.23 for a complete list of Linux signals
![Page 10: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/10.jpg)
Sending a Signal• Send a signal
– Sent by either the kernel– Or another process
• Why is a signal sent?– The kernel detects a system event.
• Divide-by-zero (SIGFPE)• Termination of a child process (SIGCHLD)
– Another process invokes a system call.• kill(pid_t pid, int SIGINT)
– kill(1500, SIGINT)» Send SIGINT to process 1500
– kill(-1500, SIGINT)» Send SIGINT to progress group 1500
• alarm(unsigned int secs)
![Page 11: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/11.jpg)
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 the default action with the signal function– Additional action: “Handle the signal”
• void sigint_handler(int sig);• signal(SIGINT, sigint_handler);
– Cannot modify action for SIGSTOP and SIGKILL
![Page 12: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/12.jpg)
Receiving a Signal• pending: bit vector: bit k is set when
signal type k is delivered, clear when signal received
• blocked: bit vector of signals that should not be received
• Only receive non-blocked, pending signals– pending & ~blocked
![Page 13: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/13.jpg)
Synchronizing Processes• 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?
![Page 14: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/14.jpg)
Race Hazard• Different behaviors of program
depending upon how the schedule interleaves the execution of code.
![Page 15: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/15.jpg)
Parent & Child Race Hazardsigchld_handler() { pid = waitpid(…); deletejob(pid);}
eval() { pid = fork(); if(pid == 0) { /* child */ execve(…); } /* parent */ /* signal handler might run BEFORE addjob() */ addjob(…);}
![Page 16: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/16.jpg)
Shell Signal Handler Childfork()addjob()
execve()exit()
sigchld_handler()deletejobs()
time
An Okay Schedule
![Page 17: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/17.jpg)
Shell Signal Handler Childfork()
execve()exit()
sigchld_handler()deletejobs()
time
addjob()
Job added to job list after the signal handler tried to delete it!
A Problematic Schedule
![Page 18: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/18.jpg)
Solution to Race Hazardsigchld_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 19: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/19.jpg)
Reaping Child Process• Child process becomes zombie when terminates
– Still consume system resources– Parent performs reaping on terminated child
• Using either wait or waitpid syscall• Where to wait children processes to terminate?
– Two waits• sigchld_handler• eval: for foreground processes
– One wait• sigchld_handler• But what about foreground processes?
![Page 20: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/20.jpg)
Busy Waitvoid eval() { … /* parent */ addjob(…); while(fg process still alive){ ; }}
sigchld_handler() { pid = waitpid(…); deletejob(pid);}
![Page 21: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/21.jpg)
Pausevoid eval() { … /* parent */ addjob(…); while(fg process still alive){ pause(); }}
sigchld_handler() { pid = waitpid(…); deletejob(pid);}
If signal handled (SIGCHLD) before call to pause, then pause will not return
![Page 22: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/22.jpg)
Sleepvoid eval() { … /* parent */ addjob(…); while(fg process still alive){ sleep(1); }}
sigchld_handler() { pid = waitpid(…); deletejob(pid);}
![Page 23: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/23.jpg)
waitpid• Used for reaping zombied child
processes• 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 have exited (zombied)– waitpid returns -1
• WUNTRACED: report status of stopped children too
![Page 24: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/24.jpg)
waitpid’s status• int status;waitpid(pid,&status, NULL)
• WIFEXITED(status): child exited normally– WEXITSTATUS(status): return code when child exits
• WIFSIGNALED(status): child exited because a signal was not caught– WTERMSIG(status): gives the number of the
terminating signal
• WIFSTOPPED(status): child is stopped– WSTOPSIG(status): gives the number of the stop signal
![Page 25: Recitation 8: Signals & Shells](https://reader036.vdocument.in/reader036/viewer/2022062814/56816827550346895dddb887/html5/thumbnails/25.jpg)
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!