January 25, 2006
Coverage: [USP] Chapters 3 and
4;
[CPL] pp. 154-155; 254
Process Wrap-up
- fork
- called once, returns twice
- initial value of
- child's data is parent's data at time of fork
- child's PC is instruction immediately following fork
- child and parent have same
- user id
- group id
- process group id
- root directory
- current working directory
- signal handling settings
- file mode creation mask
- they differ in
- process id
- parent process id
- child and parent share
- data
- open files
- typically parent closes file after fork to avoid sharing file
- though can be used as a communication mechanism
- do parent and child share program text in main memory?
- they do not share
- file descriptors
- child has its own copies of parent's file descriptors
- when is it convenient to have exact copies of parent process in memory?
- consider a network server process; might copy itself to handle a request
- how is fork executed?
- how is execlp executed?
- UNIX init, login, and command shell processes
- background processes
- call setsid to prevent the process from receiving any
signals as a result of a ctrl-c from a controlling terminal
- interesting questions
- (courtesy Matt Mize)
since parent does not wait for a background process, does
the init process wait for it?
- (courtesy Matt Mize)
does running a process in the background change the parent pid of the
background process?
- can we pass an & to the execl family
of functions to run the swapped in process in the background?
Variable Argument Lists in C
- use ellipses in prototype/header
- e.g., int printf (char *fmt, ...)
- means number and type may vary across calls
- ellipses must come at the end
- to step through arguments
- declare variable of type va_list in function
- initialize ap with va_start
- then call va_arg with ap and a datatype
to retrieve a value of that type
- call va_end with ap
after all arguments have been processed, but
before the function returns
- if variable types are used, use a switch
to control the particular call made to va_arg
- typically pass the
number of variable arguments as a parameter to the function
- necessary macro, datatypes, and functions are
declared in stdarg.h
- another use: operator
node in a parse tree (with a variable number of operands)
- other things discussed
Introduction to I/O in UNIX
- file descriptors
- 0 - stdin
- 1 - stdout
- 2 - stderr
- develop iterative versions of UNIX cat
- using only standard i/o and library functions
getchar/putchar
- using only standard i/o and library
functions fread/fwrite with
a buffer of size 1
- using the system calls read/write with
a buffer of size 1
- compile and time these programs (using time) on
a large input stream redirecting the output to the system's trashcan
(/dev/null)
- self-experimentation
- modify versions 2 and 3 to so that they take the buffer size
as a command-line argument
- increase the buffer size
- what do you notice?
- at what buffer size does the performance begin to remain constant?
- now time the UNIX cat command. what can you infer?