Interlude: Process API
5.1 fork()
Create a new process
Each process has a name -- process ID (PID)
Child process comes to life after the fork itself
Has its own copy of the address space (i.e. its own private memory), its own register, PC and so forth. The value it returns to the caller of fork() is different.
Creator: parent
Newly created process: child
5.2 wait()
Make the above example deterministic (parent waits for the child)
5.3 exec()
When you want to run a program that is different from the calling program
Given the name of an executable, and some arguments, it loads code from executable and overwrites its current code segment (and current static data) with it; the heap and stack and other parts of the memory space of the program are re-initialized.
It does not create a new process! Rather, it transforms the currently running program into a different running program.
A successful call to exec() never returns.
Allows a child to break free from its similarity to its parent and execute an entirely new program
5.4 Motivating the API
Shell: prompt, and waits you to type something into it
type something, the shell figures out where in FS the exe is, calls fork() to create a new child process to run the command, calls some variant of exec() to run the command, and then waits for the command to complete by calling wait()
Completes, returns from wait(), and prints out a prompt again
Separation of exec() and fork() enables features like input/output redirection, pipes and so on, without changing anything about the program being run.
E.x. wc p3.c > newfile.txt
Child created, before calling exec(), closes std output and opens the file newfile.txt
pipe: output of one process is connected to a pipe (queue), and the input of another process is connected to the same pipe.
5.5 Process Control and Users
kill(): send signals to a process, including directives to pause, die, and other useful imperatives
signal() to "catch" various signals
Process control is available in form of signals, which can cause jobs to stop, continue, or even terminate
Who can send and who cannot?
Strong conception of the notion of user.
Generally they can only control their own processes
It is the job of the OS to parcel out resources (CPU, memory, disk) to each user (and their processes) to meet overall system goals.
Superuser: control all processes
5.6 Useful Tools
man pages
top: processes of the system and how much CPU and other resources they are eating up
kill / killall
Last updated