Lecture 2 - Process Management
Lecture 2 - Process Management
System
Registers
• During process execution, data registers, address
registers, control, and status registers are used.
• The register's information is saved when the CPU is
taken away from the process so that it may resume its
execution when its next turn comes.
Process Control Block
State
The current state of a process, can be running, ready,
blocked etc
Priority
• A number assigned to a process to give it preference
over others.
• The lowest number means the highest priority, for
example, the process with Priority 1 will have the
highest priority over all other processes.
Event information
Information on the event for which a blocked process is
waiting.
Process Control Block
CPU scheduling information- priorities, scheduling queue pointers
Memory-management information – memory allocated to the
process
Accounting information – CPU used, clock time elapsed since
start, time limits
I/O status information – I/O devices allocated to process, list of
open files
Pointer to parent/child process
Pointer to address space of the process
Process Implementation
• The OS manages and controls the resources using tables e.g. memory
tables, I/O tables, file tables, and process tables.
• Processes are implemented utilizing process tables.
• The process tables store the ID of every process and, a corresponding
pointer to its PCB
Process Implementation
• Queues are implemented using linked list data structures
• The main queues are
i. The ready queue for storing processes with the ready state
ii. The blocked queue for storing processes waiting for a resource
iii. The suspended queue for storing the blocked processes that have
been suspended
iv. The free-process queue for the information of empty space in the
memory where a new PCB can be created
• Processes' PCBs are stored in the queues
• As queues are implemented as linked lists, each PCB has a
pointer that points to the next PCB.
• When the state of a process is changed, its PCB is moved to its
appropriate queue
Process Implementation
Queue headers
Process Implementation
• The OS keeps a table with headers for each type of queue.
• The header stores the information about the first and the last
PCBs in that queue.
Note: The running process header gives the address of only one
process because there is a single running process in the CPU
• After changing the state of a running process due to interrupt or
I/O wait, the PCB of this process is saved so that the process
can resume its execution from where it left off
• The saving of the status of the running process in its PCB is
called context saving.
• The loading of a new process in the CPU after a previous
process is stopped is called context/process switching
Process Switching
• Process switching occurs when a running process is
interrupted, and the OS assigns another process to the CPU.
• Types of interrupts:
i. System Calls. System calls are used by processes to request the
kernel for some action. For example, reading or writing an I/O
device.
ii. Exceptional Conditions. Some instructions in the process can
cause some exceptions e.g. arithmetic exceptions, reference to an
illegal address etc
iii. I/O Completion. When an I/O device completes the read/write
operation with a process, an interrupt is generated so that the
process waiting for that device may use it.
iv. External. Other external units connected with the system may also
generate an interrupt. For example, timer clock on completion of a
time slice may send an interrupt
Process Switching
The following actions are performed to handle an interrupt
i. The context of the current running process is saved onto stack. This
includes PC, PSW and registers
ii. The PC is loaded with the address of the ISR (Interrupt Service
Routine) that can handle the interrupt
iii. System control is transferred to the ISR in the kernel
iv. The ISR changes the preceding process status and saves its context
from the stack to the PCB
v. The ISR processes the interrupt by invoking the corresponding
event-handling function
vi. The OS schedules a process to be executed next
vii. The OS dispatches the scheduled process into the CPU
Process Switching
Schedulers
• The OS has several scheduler modules that are used to schedule the next
process to be executed.
• The classification of schedulers is based on the frequency of their use in the
system.
• Long-term scheduler are used rarely and after a long period of time while
short-term schedulers are used more frequently.
Schedulers
Long-term Scheduler
• This scheduler is invoked when there is a need to select a job
from the job pool to the ready queue.
• This type of scheduling does not happen very frequently
because a process waits for some time in the job pool before
getting a slot in the ready queue.
• The long-term schedulers are common in batch processing and
absent in most time-sharing systems.
Schedulers
Short-term Scheduler
• This scheduler is invoked when there is a need to select a
process from the ready queue for dispatching to the CPU.
• Whenever there is an interrupt, the short-term scheduler is
invoked to select another process for execution.
Medium-term Schedular
• It is available in models with the suspended state
• It is used to select next process moving from blocked to
blocked-suspended, blocked-suspended to ready-suspended and
ready-suspended to ready
Process Operations
The OS manages processes through creation, dispatching, termination etc
Creation
Processes are created in the following scenarios
i. The OS creates a process for a running user program
ii. The OS creates a process to provide a service e.g. printing process is
created when a user program wants to print
iii. An existing process creates another process
Parent process create children processes, which, in turn, create other
processes, forming a tree of processes
Resource sharing options
i. Parent and children share all resources
ii. Children share subset of parentʼs resources
iii. Parent and child share no resources
Execution options
i. Parent and children execute concurrently
ii. Parent waits until children terminate
Process Operations
Dispatching
The dispatching operation has the following steps.
i. The scheduler selects the next process to be executed from the ready
queue.
ii. The process code is loaded in the memory, data and stack are
initialized.
iii. The PCB is located and the saved fields (if the process has executed
partially before) are loaded in the processor’s registers and
initialized.
iv. The CPU executes the process
Process Operations
Blocking
The blocking operation has the following steps.
i. First, the process PCB is saved in the blocked queue.
ii. Event information field in the PCB stores the I/O device or the
resource for which the blocked process is waiting.
iii. When a resource or an I/O device is released in the system, the OS
scans the event information field of all the blocked processes in the
blocked queue.
iv. If a process matches, it is sent to the ready queue.
Process Operations
Termination
A process terminates in the following scenarios
i. Executes the last line in its code
ii. A user terminates the process
iii. A user logs off the system
iv. The process encounters unrecoverable error