Overview
In this lecture, we finish our discussion of the primary process control system calls, then discuss inter-process communication, including termination notification and stream communication.
Full lecture notes — Textbook readings
Primary process control system calls
- What sub-tasks are required for process coordination?
- Create a process:
fork
- Run a program:
exec
family - Exit a process:
_exit
- Stop a process:
kill
- Await completion:
waitpid
fork
vs. execvp
fork
creates a new process- Cloned program image
- New identity
- Cloned environment view
- Shared underlying environment
execvp
starts a new program in the current process- New program image
- Unchanged identity
- Unchanged environment view
- Shared underlying environment
Putting them together: processex
fork
: Which runs first?
The uniq
utility
uniq
searches for consecutive duplicate linesExample 1 Example 2 Example 2 Example 3 Example 2
uniq
: Print only one of each set of duplicatesuniq -c
: Precede each line with a count of duplicatesuniq -u
: Only print non-repeated linesuniq -d
: Only print repeated lines
Again: fork
: Which runs first?
Voluntary termination: Exit
exit
(=return
frommain
)- Process transcends the earthly sphere, leaving with its last breath a message for its parent
exit(STATUS)
library function performs cleanup actions, such as flushing stdio files_exit(STATUS)
system call exits without cleanup
- The
STATUS
is the process’s ‘last words’- Remembered by kernel
- Can be collected by the process’s parent
- Status convention
0
(EXIT_SUCCESS
) means success, non-zero (1-255) means failure
Speed of exit notification
- Exiting allows a process to communicate a single byte’s worth of data to its parent!
- How fast is this inter-process communication channel?
storage1/r-exitbyte
🤡🤪
Involuntary termination
- A process can transcend involuntarily (segmentation fault; being killed)
- This is called termination by signal
- The exit status indicates the reason for transcendence (exit vs. signal)
Collecting a termination notification
waitpid(pid, &status, options)
- Complex system call! Read the manual page! Check the return values!
options == 0
means blocking,options == WNOHANG
means nonblockingpid == -1
means wait for any child process,pid > 0
means wait for that specific process- Returns the terminated process’s ID (or 0 or -1)
- Can only wait for children
- On success,
int status
filled in with the statusWIFEXITED(status)
≠ 0 iff process terminated by exit- If
WIFEXITED
, thenWEXITSTATUS(status)
is the exit status
Question
- Fill in the
???
s with the most complete assertion you can relating thep
s! (Assumefork
does not fail.)
pid_t p1 = getpid();
pid_t p2 = getppid();
pid_t p3 = fork();
pid_t p4 = getpid();
pid_t p5 = getppid();
assert(???);
Some answers
assert(p1 > 0 && p2 > 0 && p4 > 0 && p5 > 0)
: all process PIDs are >0assert(p3 >= 0)
:fork
did not fail (it’s >0 in parent, 0 in child)assert(p1 != p2 && p4 != p5)
: a process’s PID ≠ its PPIDassert(p4 != p2)
: new PID ≠ original PPIDassert(p1 != p3 && p2 != p3)
: child PID ≠ parent or grandparent PIDassert(p3 != 0 ? p1 == p4 : p1 == p5)
- In parent (
p3 != 0
), original PID == new PID - In child (
p3 == 0
), original PID == new PPID
- In parent (
assert(p3 != 0 ? p2 == p5 : p2 != p5)
- In parent (
p3 != 0
), original PPID == new PPID - In child (
p3 == 0
), original PPID ≠ new PPID
- In parent (