Processes
Last updated
Last updated
A process is a dynamic execution context of an executing program.
Several processes may run from the same program, but each has its own state with a different PID.
A process executes one instruction at a time sequentially and the EIP
is pointing to next instruction.
Code running the program
Static data for the running program
Space of dynamic data (Heap with a HP)
Instruction pointer indicating next instruction
Execution of stack with the program's call chain (Stack with SP)
Values of CPU registers
A set of OS resources in use such as open files
Process execution state (ready, running, waiting, terminating)
new: OS is setting up the process state
running: Executing instructions on the CPU
waiting: Ready to run, but waiting for the CPU
terminated: OS is destroying this process
As the program executes, it moves from state to state, as a result of the program actions (syscalls), OS actions (scheduling), and external actions (interrupts).
The OS manages multiple active processes using state queues.
PCB OS data structure to keep track of all processes.
The PCB tracks the execution state and location of each process
The OS allocates a new PCB on the creation of each process and places it on a state queue
The OS deallocates the PCB when the process terminates
Process state (running, waiting, etc.)
Process number
Program Counter
Stack Pointer
General Purpose Registers
Memory Management Information
Username of owner
List of open files
Queue pointers for state queues
Scheduling information (e.g., priority)
I/O status
The difference between PCB and PEB:
The Process Environment Block (PEB) contains data that is relevant to the process itself, and hence can be read by applications.
The process control block (PCB) contains data that is only useful to the kernel, such as the preferred CPU for this process.
The OS maintains the PCBs of all the processes in state queues.
The OS places the PCBs of all the processes in the same execution state in the same queue.
When the OS changes the state of a process, the PCB is unlinked from its current queue and moved to its new state queue.
The OS can use different policies to manage each queue.
Each I/O device has its own wait queue.
Starting and stopping processes is called a context switch, and is a relatively expensive operation.
The OS starts executing a ready process by loading hardware registers (EIP, SP, etc...) from its PCB.
While a process is running, the CPU modifies the Program Counter (PC), Stack Pointer (SP), registers, etc...
When the OS stops a process, it saves the current values of the registers, (PC, SP, etc.) into its PCB.
This process of switching the CPU from one process to another (stopping one and starting the next) is the context switch.
Time sharing systems may do 100 to 1000 context switches a second.
The cost of a context switch and the time between switches are closely related.
One process can create other processes to do work.
The creator is called the parent and the new process is the child.
The parent defines (or donates) resources and privileges to its children.
A parent can either wait for the child to complete, or continue in parallel.
In Unix, the fork
system call called is used to create child processes.
Fork copies variables and registers from the parent to the child.
The only difference between the child and the parent is the value returned by fork.
In the parent process, fork returns the process id of the child.
In the child process, the return value is 0.
The parent can wait for the child to terminate by executing the wait
system call or continue execution.
The child often starts a new and different program within itself, via a call to exec
system call.
When a parent is killed, its child processes are called orphans and they go under the init (pid1)
process.
Normally when a child process is created, the parent process suspends until child is finished and returns a value, but if nothing is waiting on the child and parent doesn't suspend and wait for the child return value, the child process exits and becomes a zombie process
When you log in to a machine running Unix, you create a shell process.
Every command you type into the shell is a child of your shell process and is an implicit fork and exec pair.
For example, you type emacs
, the OS forks
a new process and then exec
(executes) emacs.
If you type an &
after the command, unix will run the process in parallel with your shell, otherwise, your next shell command must wait until the first one completes.
fork()
: Forks a new child process that is a copy of the parent.
execlp()
: Replaces the program of the current process with the named program.
sleep()
: Suspends execution for at least the specified time.
waitpid()
: Waits for the named process to finish execution.
gets()
: Reads a line from a file.
getpid()
: Gets the process ID.
Functions sleep()
and wait()
are only useful in old operating systems with a single core, if you want concurrency just call fork()
alone.
On process termination, the OS reclaims all resources assigned to the process, in unix-like systems a process can terminate itself using exit
or kill
system call.
Any two process are either independent or cooperating.
Cooperating processes work with each other to accomplish a single task.
Cooperating processes can
Improve performance by overlapping activities or performing work in parallel.
Enable an application to achieve a better program structure as a set of cooperating processes, where each is smaller than a single monolithic program.
Easily share information between tasks.
Distributed and parallel processing is the wave of the future. To program these machines, we must cooperate and coordinate between separate processes.
Producer is a process which is able to produce data/item.
Consumer is a Process that is able to consume the data/item produced by the Producer.
Both Producer and Consumer share a common memory buffer. This buffer is a space of a certain size in the memory of the system which is used for storage.
Producers and consumers can communicate using message passing or shared memory.
Distributed systems typically communicate using message passing
Each process needs to be able to name the other process.
The consumer is assumed to have an infinite buffer size.
A bounded buffer would require the tests in the previous slide, and communication of the in and out variables (in from producer to consumer, out from consumer to producer).
OS keeps track of messages (copies them, notifies receiving process, etc...).
Establish a mapping between the process's address space to a named memory object that may be shared across processes.
The mmap()
system call performs this function.
Fork processes that need to share the data structure.
A process is the unit of execution.
Processes are represented as Process Control Blocks in the OS
PCBs contain process state, scheduling and memory management information, etc...
A process is either New, Ready, Waiting, Running, or Terminated.
On a uniprocessor, there is at most one running process at a time.
The program currently executing on the CPU is changed by performing a context switch.
Processes communicate either with message passing or shared memory.