Transcript Outline

1
Chapter 18 – Process Management
Outline
18.1
18.2
18.3
18.4
18.5
18.6
18.7
Introduction
os.fork Function
os.system Function and os.exec Family of Functions
Controlling Process Input and Output
Interprocess Communication
Signal Handling
Sending Signals
 2002 Prentice Hall. All rights reserved.
2
18.1 Introduction
• Implement concurrency by having each task (or
process) operate in a separate memory space
• Time slicing on single-processor systems divides
processor time among many processes
• Operating system allocates a short execution time
called a quantum to a process
• Operating system performs context switching to
move processes and their dependent data in and
out of memory
 2002 Prentice Hall. All rights reserved.
3
18.1 Introduction
• Operating systems provides shells – programs that
execute system commands on user’s behalf
• Some operating systems have built-in system
commands that enable programmers to create and
manage processes (e.g. Portable Operating System
Interface for UNIX (POSIX) standard defines
these system commands for UNIX operating
systems)
 2002 Prentice Hall. All rights reserved.
4
18.2 os.fork Function
• Function os.fork, available only on POSIXcompliant systems, creates a new process
• Parent process invokes os.fork
• Parent process forks (creates) a child process
• Each process has a unique identifying process id
number (pid)
• Function os.fork’s return value
– 0 for child process
– Child’s pid for parent
 2002 Prentice Hall. All rights reserved.
5
18.2 os.fork Function
1
Parent (original)
process executes
2
Parent process calls
os.fork and assigns
return value to forkPID
Parent process (forkPID
is Child’s pid)
Parent process
3
forkPID = os.fork()
Parent and child processes
execute same program
simultaneously
Child process
(forkPID is 0)
Fig. 18.1 os.fork creates a new process.
 2002 Prentice Hall. All rights reserved.
6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Fig. 18.2: fig18_02.py
# Using fork to create child processes.
import os
import sys
import time
Indicates current process is parent
processName = "parent"
Outline
fig18_02.py
# only the parent is running now
Returns pid
print "Program executing\n\tpid: %d, processName: %s" \
% ( os.getpid(), processName )
Creates
duplicate of current process
# attempt to fork child
process
Function os.fork raises OSError exception
try:
forkPID = os.fork() # create child process
except OSError:
sys.exit( "Unable to create new process." )
if new process cannot be created
Parent process
if forkPID != 0: # am I parent process?
print "Parent executing\n" + \
"\tpid: %d, forkPID: %d, processName: %s" \
Child process
has pid
of 0
% ( os.getpid(),
forkPID,
processName
)
child process
elif forkPID == 0: Indicates
# am I child
process?
processName = "child"
print "Child executing\n" + \
"\tpid: %d, forkPID: %d, processName: %s" \
% ( os.getpid(), forkPID, processName )
print "Process finishing\n\tpid: %d, processName: %s" \
% ( os.getpid(), processName )
 2002 Prentice Hall.
All rights reserved.
7
Program executing
pid: 5428,
Parent executing
pid: 5428,
Process finishing
pid: 5428,
Child executing
pid: 5429,
Process finishing
pid: 5429,
Program executing
pid: 5430,
Child executing
pid: 5431,
Process finishing
pid: 5431,
Parent executing
pid: 5430,
Process finishing
pid: 5430,
Program executing
pid: 5888,
Child executing
Parent executing
pid: 5888,
Process finishing
pid: 5888,
pid: 5889,
Process finishing
pid: 5889,
processName: parent
Outline
forkPID: 5429, processName: parent
processName: parent
forkPID: 0, processName: child
processName: child
processName: parent
forkPID: 0, processName: child
processName: child
forkPID: 5431, processName: parent
processName: parent
processName: parent
forkPID: 5889, processName: parent
processName: parent
forkPID: 0, processName: child
processName: child
 2002 Prentice Hall.
All rights reserved.
8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Fig. 18.3: fig18_03.py
# Demonstrates the wait function.
import
import
import
import
os
sys
time
random
Outline
fig18_03.py
Generate random sleep times for child processes
# generate random sleep times for child processes
sleepTime1 = random.randrange( 1, 6 )
sleepTime2 = random.randrange( 1, 6 )
# parent ready to forkCreate
firstfirst
child
process
child
process
try:
forkPID1 = os.fork() # create first child process
except OSError:
sys.exit( "Unable to create first child. " )
if forkPID1 != 0:
# am I parent process?
Create
second
child
process
# parent ready to fork
second
child
process
try:
forkPID2 = os.fork() # create second child process
except OSError:
sys.exit( "Unable to create second child." )
if forkPID2 != 0: # am I parent process?
print "Parent waiting for child processes...\n" + \
"\tpid: %d, forkPID1: %d, forkPID2: %d" \
% ( os.getpid(), forkPID1, forkPID2 )
 2002 Prentice Hall.
All rights reserved.
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
os.wait
execution
forFunction
any child
processcauses parent to wait for child process to complete
Outline
# wait
try:
child1 = os.wait()[ 0 ] # wait returns one child’s pid
except OSError:
sys.exit( "No more child processes." )
fig18_03.py
print "Parent: Child %d finished first, one child left." \
% child1
Function os.wait returns finished child’s pid
# wait for another child process
try:
child2 = os.wait()[ 0 ] # wait returns other child’s pid
except OSError:
sys.exit( "No more child processes." )
print "Parent: Child %d finished second, no children left." \
% child2
elif forkPID2 == 0: # am I second child process?
print """Child2 sleeping for %d seconds...
\tpid: %d, forkPID1: %d, forkPID2: %d""" \
% ( sleepTime2, os.getpid(), forkPID1, forkPID2 )
time.sleep( sleepTime2 ) # sleep to simulate some work
elif forkPID1 == 0: # am I first child process?
print """Child1 sleeping for %d seconds...
\tpid: %d, forkPID1: %d""" \
% ( sleepTime1, os.getpid(), forkPID1 )
time.sleep( sleepTime1 ) # sleep to simulate some work
 2002 Prentice Hall.
All rights reserved.
9
10
Child2 sleeping for 4 seconds...
pid: 9578, forkPID1: 9577, forkPID2: 0
Child1 sleeping for 5 seconds...
pid: 9577, forkPID1: 0
Parent waiting for child processes...
pid: 9576, forkPID1: 9577, forkPID2: 9578
Parent: Child 9578 finished first, one child left.
Parent: Child 9577 finished second, no children left.
Outline
Parent waiting for child processes...
pid: 9579, forkPID1: 9580, forkPID2: 9581
Child1 sleeping for 1 seconds...
pid: 9580, forkPID1: 0
Child2 sleeping for 5 seconds...
pid: 9581, forkPID1: 9580, forkPID2: 0
Parent: Child 9580 finished first, one child left.
Parent: Child 9581 finished second, no children left.
Parent waiting for child processes...
Child1 sleeping for 4 seconds...
pid: 9583, forkPID1: 0
Child2 sleeping for 3 seconds...
pid: 9584, forkPID1: 9583, forkPID2: 0
pid: 9582, forkPID1: 9583, forkPID2: 9584
Parent: Child 9584 finished first, one child left.
Parent: Child 9583 finished second, no children left.
 2002 Prentice Hall.
All rights reserved.
11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Outline
# Fig. 18.4: fig18_04.py
# Demonstrates the waitpid function.
import os
import sys
import time
fig18_04.py
# parent about to fork
first
process
Create
first child
child process
try:
forkPID1 = os.fork() # create first child process
except OSError:
sys.exit( "Unable to create first child. " )
if forkPID1 != 0:
# am I parent process?
Create second child process
# parent about to fork second child process
try:
forkPID2 = os.fork() # create second child process
except OSError:
sys.exit( "Unable to create second child." )
if forkPID2 > 0: # am I parent process?
print "Parent waiting for child processes...\n" + \
"\tpid: %d, forkPID1: %d, forkPID2: %d" \
% ( os.getpid(), forkPID1, forkPID2 )
# wait for second childWait
process
explicitly
explicitly
for second child by specifying
try:
child2 = os.waitpid( forkPID2, 0 )[ 0 ] # child’s pid
except OSError:
sys.exit( "No child process with pid %d." % ( forkPID2 ) )
print "Parent: Child %d finished." \
% child2
its pid
 2002 Prentice Hall.
All rights reserved.
12
36
37
38
39
40
41
42
43
44
45
elif forkPID2 == 0: # am I second child process?
print "Child2 sleeping for 4 seconds...\n" + \
"\tpid: %d, forkPID1: %d, forkPID2: %d" \
% ( os.getpid(), forkPID1, forkPID2 )
time.sleep( 4 )
Outline
fig18_04.py
elif forkPID1 == 0: # am I first child process?
print "Child1 sleeping for 2 seconds...\n" + \
"\tpid: %d, forkPID1: %d" % ( os.getpid(), forkPID1 )
time.sleep( 2 )
Parent waiting for child processes...
pid: 6092, forkPID1: 6093, forkPID2: 6094
Child1 sleeping for 2 seconds...
pid: 6093, forkPID1: 0
Child2 sleeping for 4 seconds...
pid: 6094, forkPID1: 6093, forkPID2: 0
Parent: Child 6094 finished.
Child1 sleeping for 2 seconds...
pid: 6089, forkPID: 0
Child2 sleeping for 4 seconds...
pid: 6090, forkPID: 6089, forkPID2: 0
Parent waiting for child processes...
pid: 6088, forkPID: 6089, forkPID2: 6090
Parent: Child 6090 finished.
Parent waiting for child processes...
Child1 sleeping for 2 seconds...
pid: 6102, forkPID: 0
pid: 6101, forkPID: 6102, forkPID2: 6103
Child2 sleeping for 4 seconds...
pid: 6103, forkPID: 6102, forkPID2: 0
Parent: Child 6103 finished.
 2002 Prentice Hall.
All rights reserved.
18.3 os.system Function and os.exec
Family of Functions
• Function os.system executes a system
command using a shell and then returns control to
the original process (after the command
completes)
• os.exec family of functions do not return
control to calling process after executing the
specified command
 2002 Prentice Hall. All rights reserved.
13
14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Fig. 18.5: fig18_05.py
# Uses the system function to clear the screen.
import os
import sys
Outline
fig18_05.py
Function os.system executes system-specific clear command
def printInstructions( clearCommand ):
os.system( clearCommand ) # clear display
print """Type the text that you wish to save in this file.
Type clear on a blank line to delete the contents of the file.
Type quit on a blank line when you are finished.\n"""
Clear command depends on operating system
# determine operating system
if os.name == "nt" or os.name == "dos": # Windows system
clearCommand = "cls"
print "You are using a Windows system."
elif os.name == "posix": # UNIX-compatible system
clearCommand = "clear"
print "You are using a UNIX-compatible system."
else:
sys.exit( "Unsupported OS" )
filename = raw_input( "What file would you like to create? " )
# open file
try:
file = open( filename, "w+" )
except IOError, message:
sys.exit( "Error creating file: %s" % message )
printInstructions( clearCommand )
currentLine = ""
 2002 Prentice Hall.
All rights reserved.
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
15
Write
input
to user
fileinput to file
# write
while currentLine != "quit\n":
file.write( currentLine )
Truncate file if user enters
currentLine = sys.stdin.readline()
Outline
clear
fig18_05.py
if currentLine == "clear\n":
# seek to beginning and truncate file
file.seek( 0, 0 )
file.truncate()
Call printInstructions to clear console and display instructions again
currentLine = ""
printInstructions( clearCommand )
file.close()
You are using a Windows system.
What file would you like to create? welcome.txt
You are using a UNIX-compatible system.
What file would you like to create? welcome.txt
Type the text that you wish to save in this file.
Type clear on a blank line to delete the contents of the file.
Type quit on a blank line when you are finished.
This will not be written to the file.
The following line will call clear.
clear
 2002 Prentice Hall.
All rights reserved.
16
Type the text that you wish to save in this file.
Type clear on a blank line to delete the contents of the file.
Type quit on a blank line when you are finished.
Outline
Type the text that you wish to save in this file.
Type clear on a blank line to delete the contents of the file.
Type quit on a blank line when you are finished.
Wilkommen!
Bienvenue!
Welcome!
quit
Wilkommen!
Bienvenue!
Welcome!
 2002 Prentice Hall.
All rights reserved.
17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Outline
# Fig. 18.7: fig18_07.py
# Opens a Web page in a system-specific editor.
import os
import sys
import urllib
fig18_07.py
Program requires 2 command-line arguments: URL name and filename
if len( sys.argv ) != 3:
sys.exit( "Incorrect number of arguments." )
Determine operating system and set appropriate editor command
# determine operating system and set editor command
if os.name == "nt" or os.name == "dos":
editor = "notepad.exe"
elif os.name == "posix":
editor = "vi"
else:
sys.exit( "Unsupported OS" )
Retrieve Web page and store its contents in the specified file
# obtain Web page and store in file
urllib.urlretrieve( sys.argv[ 1 ], sys.argv[ 2 ] )
Replaces current process with text editor application
# editor expects to receive itself as an argument
os.execvp( editor, ( editor, sys.argv[ 2 ] ) )
Function
os.execvp
print "This line
never
executes." takes
two arguments: command name and its arguments
fig18_07.py http://www.deitel.com/test/test.txt test.txt
This is a test file to illustrate
downloading text from a file on a
web server using an HTTP connection
to the server.
~
~
~
 2002 Prentice Hall.
All rights reserved.
18
Outline
 2002 Prentice Hall.
All rights reserved.
18.3 os.system Function and os.exec
Family of Functions
Func tio n na m e
De sc rip tio n
execl( path, arg0, arg1, ... )
Executes the program path with the given
arguments.
execle( path, arg0, arg1, ...,
Same as execl, but takes an additional
environment )
argument—the environment in which the
program executes. The environment is a
dictionary with keys that are environment
variables and with values that are environment
variable values.
Same as execl, but searches for executable
execlp( path, arg0, arg1, ... )
path in os.environ[ "PATH" ].
execv( path, arguments )
Same as execl, but arguments are contained
in a single tuple or list.
excve( path, arguments, environment Same as execle, but arguments are contained
in a single tuple or list.
)
Same as execlp, but arguments are contained
execvp( path, arguments )
in a single tuple or list.
execvpe( path, arguments,
Same as execvp, but takes an additional
environment )
argument, environment, in which the
program executes.
Fig. 18.8 exec family of functions.
 2002 Prentice Hall. All rights reserved.
19
20
18.4 Controlling Process Input and Output
• UNIX-system users can use multiple programs
together to achieve a particular result (e.g., ls |
sort produces a sorted list of a directory’s
contents)
 2002 Prentice Hall. All rights reserved.
21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Outline
# Fig. 18.9: fig18_09.py
# Demonstrating popen and popen2.
import os
fig18_09.py
Determine operating system to set system-specific directory-listing and reverse-sort commands
determine operating system, then set directory-listing and
#
# reverse-sort commands
if os.name == "nt" or os.name == "dos": # Windows system
fileList = "dir /B"
sortReverse = "sort /R"
elif os.name == "posix": # UNIX-compatible system
fileList = "ls -1"
sortReverse = "sort -r"
else:
sys.exit( "OS not supported by this program." )
Executes command and obtains stdout stream
# obtain stdout of directory-listing command
dirOut = os.popen( fileList,
"r"
)
Executes
command
and obtains
stdout and stdin streams
# obtain stdin, stdout of reverse-sort command
Returns
directory-listing
command’s
sortIn, sortOut = os.popen2(
sortReverse
)
filenames = dirOut.read()
output
# output from directory-listing command
# display output from directory-listing command
print "Before sending to sort"
print "(OutputSends
from directory-listing
’%s’):" % fileList
command’s output
print filenames
to reverse-sort command’s stdin stream
stdout
command
sortIn.write( Close
filenames
) #stream
send of
to directory-listing
stdin of sort command
Closing stdin stream of sort command sends EOF
dirOut.close()
sortIn.close()
# close stdout of directory-listing command
# close stdin of sort command -- sends EOF
 2002 Prentice Hall.
All rights reserved.
22
35
36
37
38
39
40
# display output from sort command
print "After sending
todirectory
sort" list sorted in reverse
Retrieve
print "(Output from ’%s’):" % sortReverse
print sortOut.read()
# output from
sort stream
command
Close sort command’s
stdout
sortOut.close()
order
Outline
fig18_09.py
# close stdout of sort command
Before sending to sort
(Output from 'ls -1'):
fig18_02.py
fig18_03.py
fig18_04.py
fig18_05.py
fig18_07.py
fig18_09.py
fig18_10.py
fig18_14.py
fig18_15.py
After sending to sort
(Output from 'sort -r'):
fig18_15.py
fig18_14.py
fig18_10.py
fig18_09.py
fig18_07.py
fig18_05.py
fig18_04.py
fig18_03.py
fig18_02.py
 2002 Prentice Hall.
All rights reserved.
23
Before sending to sort
(Output from 'dir /B'):
fig18_02.py
fig18_03.py
fig18_04.py
fig18_05.py
fig18_07.py
fig18_09.py
fig18_10.py
fig18_14.py
fig18_15.py
Outline
After sending to sort
(Output from 'sort /R'):
fig18_15.py
fig18_14.py
fig18_10.py
fig18_09.py
fig18_07.py
fig18_05.py
fig18_04.py
fig18_03.py
fig18_02.py
 2002 Prentice Hall.
All rights reserved.
24
18.5 Interprocess Communication
• Module os provides interface to many
interprocess communication (IPC) mechanisms
• Pipe, IPC mechanism, is a file-like object that
provides a one-way communication channel
• Function os.pipe creates a pipe and returns a
tuple containing two file descriptors which
provide read and write access to the pipe
• Functions os.read and os.write read and
write the files associated with the file descriptors
 2002 Prentice Hall. All rights reserved.
25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Fig. 18.10: fig18_10.py
# Using os.pipe to communicate with a child process.
import os
import sys
Outline
fig18_10.py
Open parent and child read/write pipes
# open parent and child read/write pipes
fromParent, toChild = os.pipe()
fromChild, toParent = os.pipe()
# parent about to fork child process
try:
pid = os.fork() # create child process
except OSError:
sys.exit( "Unable to create child process." )
if pid != 0:
# am I parent process?
Close unnecessary pipe ends for parent
# close unnecessary pipe ends
os.close( toParent )
os.close( fromParent )
# write values from 1-10 to parent’s write pipe and
# read 10 values
pipe
Write from
valueschild’s
1-10 to read
parent’s
write pipe
for i in range( 1, 11 ):
os.write( toChild, str( i ) )
print "Parent:
% from
i,
Read%d,"
values
child’s read pipe
print "Child: %s" % \
os.read( fromChild, 64 )
Close remaining pipes
# close pipes
os.close( toChild )
os.close( fromChild )
 2002 Prentice Hall.
All rights reserved.
26
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
elif pid == 0:
Parent:
Parent:
Parent:
Parent:
Parent:
Parent:
Parent:
Parent:
Parent:
Parent:
# am I child process?
Close pipe ends unnecessary for child process
# close unnecessary pipe ends
os.close( toChild )
os.close( fromChild )
Outline
fig18_10.py
Read value from parent pipe
# read value from parent pipe
currentNumber = os.read( fromParent, 64 )
# if we receive number from parent,
# write number to child write pipe
while currentNumber:
Write numberRead
to child
write pipe
more
newNumber = int( currentNumber
) * data
20 from
os.write( toParent, str( newNumber ) )
currentNumber = os.read( fromParent, 64 )
Close remaining pipes
# close pipes
os.close( toParent )
Terminates) child process without
os.close( fromParent
os._exit( 0 ) # terminate child process
parent read pipe
any cleanup
1, Child: 20
2, Child: 40
3, Child: 60
4, Child: 80
5, Child: 100
6, Child: 120
7, Child: 140
8, Child: 160
9, Child: 180
10, Child: 200
 2002 Prentice Hall.
All rights reserved.
27
18.5 Interprocess Communication
Read end
Write end
Parent pipe
Original process can
read from either
parent or child pipe
Original process can
write to either parent
or child pipe
Original process
Child pipe
Read end
Write end
Fig. 18.11 Initial stage: Original process can read and write to both pipes.
 2002 Prentice Hall. All rights reserved.
28
18.5 Interprocess Communication
Patent process can read from parent
and child pipe
Patent process can write to parent
and child pipe
Parent process
Read end
Write end
Parent pipe
Child process can
read from parent and
child pipe
Child process
Child process can
write to parent and
child pipe
Child pipe
Read end
Write end
Fig. 18.12 Intermediate stage: Parent and child processes can read and write to both pipes.
 2002 Prentice Hall. All rights reserved.
29
18.5 Interprocess Communication
Parent process
Read end
Write end
Parent pipe
Parent process
reads from child pipe
and writes to parent
pipe
Child process
reads from
parent pipe and
writes to child
pipe
Child process
Child pipe
Read end
Write end
Fig. 18.13 Final stage: Parent and child processes close unneeded ends of the pipes.
 2002 Prentice Hall. All rights reserved.
30
18.6 Signal Handling
• Processes can communicate with signals, which
are messages that operating systems deliver to
programs asynchronously
• Signal processing: program receives a signal and
performs an action based on that signal
• Signal handlers execute in response to signals
• Every Python program has default signal handlers
 2002 Prentice Hall. All rights reserved.
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Fig. 18.14: fig18_14.py
# Defining our own signal handler.
Outline
Module signal provides signal-handling capabilities
import time
import signal
fig18_14.py
Signal handlers take two arguments: signal and current stack frame
def stop( signalNumber, frame ):
global keepRunning
keepRunning -= 1
print "Ctrl-C pressed; keepRunning is", keepRunning
keepRunning = 3
Registers
signal signal.SIGINT with function stop
# set the handler for SIGINT to be function stop
signal.signal( signal.SIGINT, stop )
while keepRunning:
print "Executing..."
time.sleep( 1 )
print "Program terminating..."
Executing...
Executing...
Ctrl-C pressed; keepRunning is 2
Executing...
Executing...
Ctrl-C pressed; keepRunning is 1
Executing...
Executing...
Ctrl-C pressed; keepRunning is 0
Program terminating...
 2002 Prentice Hall.
All rights reserved.
32
18.7 Sending Signals
• Executing programs can send signals to other
processes
• Function os.kill sends a signal to a particular
process identified by its pid
 2002 Prentice Hall. All rights reserved.
33
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Outline
# Fig. 18.15: fig18_15.py
# Sending signals to child processes using kill
import
import
import
import
os
signal
time
sys
fig18_15.py
Signal handler enables parent process to handle interrupt signal
# handles both SIGALRM and SIGINT signals
def parentInterruptHandler( signum, frame ):
global pid
global parentKeepRunning
Sends kill signal signal.SIGKILL to process with specified pid
# send kill signal to child process and exit
os.kill( pid, signal.SIGKILL ) # send kill signal
print "Interrupt received. Child process killed."
# allow parent process to terminate normally
parentKeepRunning = 0
Register parent’s handler for signal.SIGINT
# set parent's handler for SIGINT
signal.signal( signal.SIGINT, parentInterruptHandler )
# keep parent running until child process is killed
parentKeepRunning = 1
# parent ready to fork child process
try:
pid = os.fork() # create child process
except OSError:
sys.exit( "Unable to create child process." )
if pid != 0:
# am I parent process?
 2002 Prentice Hall.
All rights reserved.
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
while parentKeepRunning:
print "Parent running. Press Ctrl-C to terminate child."
time.sleep( 1 )
elif pid == 0:
# am I child process?
Outline
fig18_15.py
Ignore interrupt signal in child process
# ignore interrupt in child process
signal.signal( signal.SIGINT, signal.SIG_IGN )
while 1:
print "Child still executing."
time.sleep( 1 )
print "Parent terminated child process."
print "Parent terminating normally."
Parent running. Press Ctrl-C to terminate child.
Child still executing.
Parent running. Press Ctrl-C to terminate child.
Child still executing.
Child still executing.
Parent running. Press Ctrl-C to terminate child.
Interrupt received. Child process killed.
Parent terminated child process.
Parent terminating normally.
 2002 Prentice Hall.
All rights reserved.