Recitation08 SectionA

Information about Recitation08 SectionA

Published on November 5, 2007

Author: Gallard

Source: authorstream.com

Content

Recitation 8: Signals & Shells:  Recitation 8: Signals & Shells Andrew Faulring 15213 Section A 28 October 2002 Andrew Faulring:  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:  Today’s Plan Process IDs & Process Groups Process Control Signals Preemptive Scheduler Race hazards Reaping Child Processes Lab 5: Shell:  Lab 5: Shell tshref Use as a guide for output You shell should have same behavior How Programmers Play with Processes:  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 Process IDs & Process Groups:  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); Slide7:  Fore- ground job Back- ground job #1 Back- ground job #2 Shell Child 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 Signals:  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 Important Signals:  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 Sending a Signal:  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) Receiving a Signal:  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 Receiving a Signal :  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 Synchronizing Processes:  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? Race Hazard:  Race Hazard Different behaviors of program depending upon how the schedule interleaves the execution of code. Parent & Child Race Hazard:  Parent & Child Race Hazard sigchld_handler() { pid = waitpid(…); deletejob(pid); } eval() { pid = fork(); if(pid == 0) { /* child */ execve(…); } /* parent */ /* signal handler might run BEFORE addjob() */ addjob(…); } An Okay Schedule:  Shell Signal Handler Child fork() addjob() execve() exit() sigchld_handler() deletejobs() time An Okay Schedule A Problematic Schedule:  Shell Signal Handler Child fork() execve() exit() sigchld_handler() deletejobs() time addjob() A Problematic Schedule Solution to Race Hazard:  Solution to Race Hazard sigchld_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) Reaping Child Process:  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? Busy Wait:  Busy Wait void eval() { … /* parent */ addjob(…); while(fg process still alive){ ; } } sigchld_handler() { pid = waitpid(…); deletejob(pid); } Pause:  Pause void 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 Sleep:  Sleep void eval() { … /* parent */ addjob(…); while(fg process still alive){ sleep(1); } } sigchld_handler() { pid = waitpid(…); deletejob(pid); } waitpid:  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 waitpid’s status:  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 Summary:  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!

Related presentations


Other presentations created by Gallard

www MEGALOL net must audi
03. 10. 2007
0 views

www MEGALOL net must audi

winds of change
28. 09. 2007
0 views

winds of change

S1 01 LaFazia
01. 12. 2007
0 views

S1 01 LaFazia

Berkeley Nov 8
10. 12. 2007
0 views

Berkeley Nov 8

ICMCI Moscow UN NGO 1
27. 09. 2007
0 views

ICMCI Moscow UN NGO 1

P105 017
29. 10. 2007
0 views

P105 017

d104
02. 11. 2007
0 views

d104

Business Forecast 2007
02. 11. 2007
0 views

Business Forecast 2007

dias de muertos
05. 11. 2007
0 views

dias de muertos

Goat Herd Health
19. 11. 2007
0 views

Goat Herd Health

Fireworks Presentation0086
05. 11. 2007
0 views

Fireworks Presentation0086

feb1917
26. 11. 2007
0 views

feb1917

llp
18. 12. 2007
0 views

llp

coldwarbegins
21. 12. 2007
0 views

coldwarbegins

A C 20 US CHAPTER
28. 12. 2007
0 views

A C 20 US CHAPTER

wetland symbol
04. 01. 2008
0 views

wetland symbol

Assessing Problem Based Learning
05. 01. 2008
0 views

Assessing Problem Based Learning

symantec
02. 01. 2008
0 views

symantec

MT05 Riishojgaard
14. 11. 2007
0 views

MT05 Riishojgaard

trends
24. 12. 2007
0 views

trends

Hu Prieger Presentation07
19. 02. 2008
0 views

Hu Prieger Presentation07

Singerman presentation Jan 08
24. 02. 2008
0 views

Singerman presentation Jan 08

2003 Cluster ESMR 1st WG intro
27. 02. 2008
0 views

2003 Cluster ESMR 1st WG intro

gender stigma violence laura
31. 12. 2007
0 views

gender stigma violence laura

grenzeback freight trends
29. 02. 2008
0 views

grenzeback freight trends

18ESB GWON Slides
05. 03. 2008
0 views

18ESB GWON Slides

371outline
13. 12. 2007
0 views

371outline

26
14. 03. 2008
0 views

26

eng 420 lecture3n4
27. 03. 2008
0 views

eng 420 lecture3n4

W4G SNOMEDCT
02. 10. 2007
0 views

W4G SNOMEDCT

NarcolepsyMidwestSma ll
28. 11. 2007
0 views

NarcolepsyMidwestSma ll

Doc5 IDSC
30. 03. 2008
0 views

Doc5 IDSC

investing101
13. 04. 2008
0 views

investing101

204 010 06 Seidler PPT
29. 12. 2007
0 views

204 010 06 Seidler PPT

Nico Olie General Assessment
23. 11. 2007
0 views

Nico Olie General Assessment

solomon16 web
05. 11. 2007
0 views

solomon16 web

ASTRA Presentation
16. 11. 2007
0 views

ASTRA Presentation

Unit09a reactions
04. 01. 2008
0 views

Unit09a reactions

Blogosfera
09. 10. 2007
0 views

Blogosfera

explore advocacy
19. 12. 2007
0 views

explore advocacy

Dennis Martell Changing Focus
05. 11. 2007
0 views

Dennis Martell Changing Focus

ksu 1
04. 10. 2007
0 views

ksu 1

nm ms rvsphr2
30. 12. 2007
0 views

nm ms rvsphr2

Peter Dexter JCOMM
15. 11. 2007
0 views

Peter Dexter JCOMM

6 XQuery
05. 12. 2007
0 views

6 XQuery

webfig ch06
23. 11. 2007
0 views

webfig ch06

IYE proj lighter version
05. 11. 2007
0 views

IYE proj lighter version

inspec ebsco brief
12. 12. 2007
0 views

inspec ebsco brief