Lecture 3, Week 1 September 29, 2016
- Interrupts halt the execution of a process and transfer control (execution) to the operating system
- Interrupts are ranked in terms of priority:
- Higher priority (lower rank) interrupts are handled first.
Interrupts can spawn while the OS is executing
- Interrupts are used by devices to have the OS do stuff
- Polling: The CPU loops until the OS is done.
The process is the OS abstraction for allowing programs to use the CPU (execution)
- The unit of execution and scheduling (what runs what doesn’t run)
- The dynamic execution context of a program
Equivalent terms: job, task, sequential process
A sequential process is program in execution; in other words, a process is a running program.
We must load a program into a process to run the program.
The process contains all state for a program in execution.
A process has a unique identifier, called a process ID (PID)
Basic Process Address Space¶
Everything the process needs is stored in its address space.
The address space is partioned into a few components, some are defined below:
- Text segment
- Static Data
- Data Segment
- Dynamic Memory Alloc
Function calls, etc.; the stack pointer “points” to the beginning of the stack (first in, last out).
This is dynamic as well!
Each process has an execution state that indicates what it is currently doing:
- Execution instruction on the CPU (scheduled on the CPU)
- Waiting to be assigned to the CPU (scheduled)
Waiting for an event; e.g., I/O completion
The process can not continue or make progress until the event completes.
As a process executes, it moves from state to state
STAT/S column indicates execution state
A system can only support a finite number of processes – this number is set by the system adminstrator. The system can also hit this limit if it runs out of space to allocate to another process.
Process Data Structures¶
The OS has a data stucture, called the Process Control Block (PCB) that stores all the processes.
The PCB contains all of the info about a process and hardware execution state (PC, SP, regs, etc.) when the process is not running:
- This state is everything that is needed to restore the hardware to the same configuration it was in when the process was switched out of the hardware.
PCB Data Structure¶
Reffered to as a heavyweight (i.e., a lot is stored inside of it) abstraction.
Keep this is mind for next week’s lecture on threads, as processes are large.
PCBs and Hardware State¶
- When the process is running, its hardware state (PC, SP, regs, etc.) is in the CPU.
- The hardware registers contain the current value.
When the OS stops running a process, it saves the current values of the registers into the process’ PCB
When the OS is ready to start executing a new process, it loads the hardware registers from the values stored in that process’ PCB.
- Context Switch
- The process of changing the CPU hardware state from one process to another
This can happen 100 or 1000 times a second
The OS maintains a collection of queues that represent the state of all processes in the system
- Typically, the OS has one queue for each state
- Ready, waiting, etc.
Each PCB is queued on a state queue according to its current state
As a process changes state, its PCB is unlinked from one queue and linked into another.
There may be many wait queues, one for each type of wait (disk, etc.)
PCBs and State Queues¶
PCBs are data structures dynamically created in OS address space
A process is created by another process:
- Parent Process
- The creator
- Child Process
- Created by parent (Unix:
The first process is a special kind:
init. All other process are
either create by
init or children of
A system call invokes the creation of a new process.
* on Windows:
* on Unix:
Process Creation: Unix¶
Unlike Window’s equivalent
fork takes no arguments.
It initializes a new process by copying (not sharing) the address space of the parent.
- It returns twice:
- Returns the child’s PID to the parent, and
0to the child.
- Returns the child’s PID to the parent, and
When it comes to sheduling, there is no way to know which process will run first.
- It is very useful when the child...:
- is cooperating with the parent
- relies upon the parent’s data to accomplish its task
To start running a new program, Unix provides the
- What does
- Stops the current process
- Loads the program
proginto the process’ address space
- Initializes hardware context and args for the new program
- Places the PCB onto the ready queue
It does not create a new process
exec() changes the program that is running inside a process shell.
What is difference between
fork() is used to create a new process, exec is used to load
a program into the address space.
fork() will return an error if it runs out of processes.
exec() returns an error when given a program that doesn’t exist.
- To terminate a process on...
- Essentially, free resources and terminates:
- Terminates all threads (next lecture)
- Close open files, network connections
- Allocated memory (and VM pages out on disk)
- Remove PCB from kernel data structures, delete
- Often it is convenient to pause until a child process has finished.
- Think of executing commands in a shell
wait() returns the status code of the child process that
it returned by invoking
Every process must be “reaped” by a parent. If a child process exits, and no ancestor can reap it, the process is known as a zombie process