Overview
This lecture introduces the process control unit. We discuss the goals of process control and the basic system calls used to create and manage processes.
About process control
- Advantages of processes
- Strong modularity
- Isolation
- Coordinate multiple processes to accomplish complex tasks
- As opposed to one enormous program
- Take advantage of more computing resources
- When one process is paused (e.g., reading from storage), run another
- Reusable components
- Requires one process coordinate the operation of others
- Prototypical coordination program: the shell
Shell question 1
- Produce a lowercase version of
file.txt- “Hello! hello hello” ⟶ “hello! hello hello”
- “Hello hi there HI” ⟶ “hello hi there hi”
- Program toolbox
cat FILE: Write contents ofFILEto standard outputsleep N: Exit afterNsecondssort: Sort linestr A-Z a-z: Change uppercase characters to lowercasetr -cs a-z "\n": Change non-lowercase characters to newlineuniq: Remove duplicate adjacent lineswc -l: Count lines
- Coordination toolbox
PROG < FILE: Read standard input ofPROGfromFILEPROG > FILE: Write standard output ofPROGtoFILEPROG1 | PROG2: Send standard output ofPROG1to standard input ofPROG2
Shell question 2
- How many different ‘words’ are used in
file.txt?- “Hello! hello hello” ⟶ 1
- “Hello hi there HI” ⟶ 3
- Program toolbox
cat FILE: Write contents ofFILEto standard outputsleep N: Exit afterNsecondssort: Sort linestr A-Z a-z: Change uppercase characters to lowercasetr -cs a-z "\n": Change non-lowercase characters to newlineuniq: Remove duplicate adjacent lineswc -l: Count lines
- Coordination toolbox
PROG < FILE: Read standard input ofPROGfromFILEPROG > FILE: Write standard output ofPROGtoFILEPROG1 | PROG2: Send standard output ofPROG1to standard input ofPROG2
Process control system calls
- What are the tasks of a shell?
- Create a process:
fork - Run a program:
execfamily - Exit a process:
_exit - Stop a process:
kill - Await completion:
waitpid - Shell code depends on system call API
Process = program image + identity + environment
- Program image: unprivileged, contents of memory
- Code, data, (initially empty) stack and heap
- Command line arguments (
argc,argv)
- Identity: kernel, managed by kernel
- Process ID
- Process relationships (parent process ID)
- Ownership, timing, etc.
- Environment: kernel, managed by system calls
- File descriptor table: open file descriptors, file positions
Process hierarchy
- Every process has a parent process
getpidsystem call: Return current process IDgetppidsystem call: Return parent process IDforkcreates a new child process
- Root of process hierarchy is process with ID 1
Question
- Fill in the
???s with the most complete assertion you can relating theps! (Assumeforkdoes 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):forkdid 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 (
fork: Which runs first?
fork vs. exec
fork- Cloned program image
- New identity
- Cloned environment
exec(e.g.,execvp)- New program image
- Unchanged identity
- Unchanged environment