Chapter 3
Process Description and Control
Processes -- Introduction
What is a process?
The notion of a process was introduced by the developers of
the Multics operating system in 1965 to denote "the execution of a
program." A program is a static thing, basically just some text.
However, executing the program involves not only its text, but also a
sequence of operations performed based on that text, the memory used
to store the text and perform the operations, etc.
The Multics authors tried to give a precise definition of a process,
but most textbook authors have given more intuitive definitions, all
of which emphasize the dynamic nature of a process, as opposed to the
static nature of a program.
Some of these definitions:
- A sequence of operations carried out one at a time
(Brinch Hansen, 1974).
- A sequence of actions performed by a program
(Coffman and Denning 1973).
- A program with an activation record representing it in the system
(Loren and Deitel 1981).
- A program whose execution has started and not yet terminated
(Maekawa, Oldehoeft and Oldehoeft, 1987).
- A program in execution
(Tanenbaum 1987, Silberschatz and Peterson 1988).
- The activity resulting from the execution of a program with its data by a sequential processor
(Bic and Shaw 1988).
- The execution of an individual program
(Stallings 1992).
- A program whose execution has started but is not yet complete
(Singhal and Shivaratri 1994).
- A running program and all the state associated with it
(Lewis and Berg 1996).
- An operating system concept that captures the idea of a program in
execution
(Crowley 1997)
The important points are:
A process is an execution stream in a particular context.
- The context is everything that can affect, or be affected by, the
process: code, particular data values, open files, etc.
- The execution stream is a sequence of instructions performed
using that context.
Processes are sequential: only one thing happens at a time.
Process States
The Two-State Model
Figure 3.4 goes here.
The Dispatcher
The Dispatcher is the inner-most portion of the O/S; it selects and runs
processes:
- Run a process for a while.
- Save the process state.
- Load state of another process.
- Run it ...
The Two-State Model Is Inadequate
The two-state model is not realistic. Not all processes are always ready to execute.
Some processes in the Not-Running state are ready to run; others are blocked
while waiting for an I/O to complete.
Do we want to keep all these processes on a single queue? No. The Dispatcher
would have to scan the Not-Running queue looking for a process that is not
blocked. We'd rather have the Dispatcher just take the first process on the
queue. Less overhead.
It's more natural to separate the Not-Running state into two states: Ready and Blocked.
The Five-State Model
A process is in one of five states:
- Running: Currently being executed on the CPU.
- Ready: Prepared to execute when given the opportunity.
- Blocked: Cannot execute again until some event occurs,
e.g. completion of an I/O operation.
- New: Created but not yet added to the pool of executable processes.
- Exited: Released by the O/S from the pool of executable processes:
halted, aborted, etc.
Figure 3.5 goes here.
Figure 3.6 goes here.
Process Control
Introduction
- How can several processes share one CPU? O/S must make sure
that processes don't interfere with each other. This means:
- Making sure each gets a chance to run (fair scheduling).
- Making sure they don't modify each other's state (protection).
The Process Control Block
First, the O/S has to keep track of all the processes.
For each process, the process control block (PCB) holds:
- Process identification information.
- Processor state information.
- Process control information
Process Identification Information
- Identity of the process.
- Identity of the parent process.
- Identity of the user.
Processor State Information
- User-visible registers.
- Control and status registers.
- Stack pointers.
Process Control Information
- Scheduling and state information: process state, priority,
amount of time process has been waiting, event being waited for
(if any), etc.
- Data structuring: links to other processes in system data structures
(like ready queue), links to parent and children.
- Interprocess communication: information about flags, signals, messages
used for communication with other processes (may not all be kept in PCB).
- Process privileges.
- Memory management: pointers to segment and/or page tables.
- Resource ownership and utilization: open files, amount of CPU time consumed
so far and/or other accounting information.
The Process table is a collection of all process control blocks for
all processes. In Unix the process table is a fixed-size array.
Process Switching
CPU can only be doing one thing at a time: if user process is executing,
Dispatcher isn't: O/S has lost control! How does O/S regain control
of processor?
- Internal events (things occurring within user process).
These are also called traps. They all cause a state switch
into the O/S.
- System call;
- Error (illegal instruction, addressing violation, etc.);
- Page fault.
- External events (things occurring outside the control of the
user process).
These are usually called interrupts. They all
cause a state switch into the O/S.
- Character typed at terminal;
- Completion of disk operation (controller is ready for more work);
- Timer: to make sure O/S eventually gets control.
Saving the Process State
When a process isn't running,
its state must be saved in its PCB.
What gets saved? Everything that next process could damage:
- Program counter;
- Processor status word (condition codes, etc.);
- General purpose registers;
- Floating-point registers;
- Entire address space?
Saving the state of user process is tricky because the the O/S
needs some state to execute the state saving and restoring code.
All machines provide some special hardware support for
saving and restoring state.
Mode Switching
What does the CPU do when a trap or interrupt occurs?
- Save the current processor state.
- Set the program counter to the beginning of an interrupt
handler routine.
- Switch from user mode to kernel (supervisor, privileged) mode.
Why? Interrupt handler may include privileged instructions.
- Begin processing interrupt handler routine.
Interrupt Handler
What does the interrupt handler do? It depends.
- External interrupt (character typed, completion of I/O, etc.):
process interrupt, restore state of interrupted process, switch to user mode,
resume interrupted process.
- Page fault: Call memory manager to fetch new page, restore state of
interrupted process, switch to user mode, resume interrupted process.
- I/O request: Start I/O, call Dispatcher.
- Timer interrupt: call Dispatcher.
Context Switching
When the Dispatcher must select a new process to run on the CPU
(timer interrupt, current process halts, current process blocked for
I/O), a context switch occurs. Since the current process will
not resume at the end of interrupt processing, the Dispatcher must
do more:
- Save the processor state in PCB.
- Update PCB (state of process, accounting information).
- Move PCB to appropriate queue.
- Select another process to run (more about this later in the semester).
- Update the PCB of the selected process.
- Update memory-management data structures (more about this in
about a month).
- Restore the previous context of selected process (program counter,
PSW, registers).
- Resume execution of selected process.
Creating Processes
New Processes
What must the O/S do to create a new process from scratch?
- Assign a unique process identifier (PID) to new process;
- Allocate space for the process:
- Load code and data into memory;
- Create an (empty) call stack;
- Create and initialize process control block.
- Make process known to Dispatcher.
The Unix Fork
The Unix fork makes a new copy of an existing process.
For example, the shell makes a copy of itself for each command.
One waits around; the other goes off and executes the command.
- Make sure process to be copied isn't running and has
all state saved.
- Make a copy of code, data, stack.
- Copy PCB of source into new process.
- Make process known to Dispatcher.