0% found this document useful (0 votes)
55 views40 pages

Lecture 2 - Process Management

This document discusses the fundamentals of process management in operating systems. It describes what a process is, the different states a process can be in during its lifecycle (new, ready, running, blocked, terminated), and how the CPU switches between running processes using a process scheduler. It also explains the process control block (PCB) that the OS uses to store information and control each process like its ID, program counter, registers, and more.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views40 pages

Lecture 2 - Process Management

This document discusses the fundamentals of process management in operating systems. It describes what a process is, the different states a process can be in during its lifecycle (new, ready, running, blocked, terminated), and how the CPU switches between running processes using a process scheduler. It also explains the process control block (PCB) that the OS uses to store information and control each process like its ID, program counter, registers, and more.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

CS 334: Principles of Operating

System

Lecture 2: Fundamentals of Process


Management
Outline
• Overview of processes
• Process environment
• Process life cycle
• Process control block
• Process switching
• Process schedulers
• Operations on processes
Introduction
• A process is an instance of an executing program
• It is an abstraction of a running program
• Processes provide the ability to conduct concurrent
operations even if there is a single processor (CPU)
• A CPU runs one process at a time, but in a
multiprogramming system, the CPU switches
between processes, running each for a few
milliseconds.
• Operating systems use processes to manage
multiple running programs and create the illusion
of parallelism.
• A process consists of the current values of the
program counter, registers, and variables
Introduction
• A process is different from a program
• A program is a set of instructions stored
somewhere in the disk.
• A program is a passive entity and continues to
reside in the disk until launched.
• When a program is launched and is ready for
execution, it becomes known as a process.
• A process is an active entity with a limited time
span as compared to a program.
• The term task is used interchangeably with the
term process.
• The term job is used interchangeably with the
term program.
Process Environment
• The process address space is the set of logical addresses
that a process references in its code
• The address space contains all of the memory allocated
to the process.
• It consists of a code section, a data section, a stack and
a process control block (PCB)
• The code section stores the program code.
• The data (also called heap) section consists of global
variables.
• The stack stores local variables, parameters in a module
call, and return addresses
• The PCB is used for storing process attributes such as ID
and state
Process Environment
• The collection of the items in the process address space
is also called the process image
Process Lifecycle
• From the time of its creation to its termination,
a process passes through various states.
• The states of processes are used by the OS to
maintain a multiprogramming environment
Process Lifecycle
A Two state process model
A process may be in one of the two states
i. Running
ii. Not Running

A process is moved to the CPU and back to the queue until


the task is completed
Process Lifecycle
A two-state process model
• In a two-state model, the dispatcher has to scan the entire
queue looking for a process that is not blocked and that
has been in the queue the longest.

• To simplify the scanning process, the Not Running state is


split into two states:
i. Ready (processes in the Not Running state which are
ready to execute)
ii. Blocked (processes in the Not Running state waiting for
an I/O operation to complete).
Process Lifecycle

Five state process model


• When a process is first
created, it is put into a job
queue and given a NEW
state.
• The process is still in the
secondary storage as a
program and not admitted
to the main memory.
• The control information
regarding the new process
is created and maintained
in the memory.
Process States

Five state process model


• When a program in the job
queue is scheduled and
brought to the ready
queue, the state of the
process is changed to
READY.
• The process in ready state
is ready for execution but
not yet executing
• The ADMIT event fetches a
job from the job pool to the
ready queue in main
memory.
Process States

Five state process model


• When a process in the
ready queue is selected by
the scheduling mechanism
for execution and
dispatched to the CPU it
becomes a RUNNING
process.
• The CPU executes the
instructions in the code of
the process.
• A process runs for some
time before either being
interrupted or finishing the
last instruction.
Process States

Five state process model


• The DISPATCH event sends
a process to the CPU for
execution after selection.
• The selection of a process
for dispatching depends on
the system scheduling
algorithm
• The INTERRUPT event stops
the execution of a process
and changes its state to the
ready state. This can be due
to the expiry of a time slot
or high priority process
Process States

Five state process model • An executing process may


reach an instruction where
it has to wait for an I/O
device or other event.
• While waiting for the I/O or
event, the process will be
put in a BLOCKED state to
free the processor for
executing other ready
processes
• The blocked process waits
for the I/O or event in the
main memory but in a
separate queue known as
the blocked queue.
Process States

Five state process model • When the I/O access or the


other event is completed,
the process is moved back
to the ready queue to be
scheduled for its turn to
execute.
• A WAIT event causes a
change of state from
RUNNING to BLOCKED
• A WAIT COMPLETION
event moves a process back
to the READY queue
Process States

Five state process model • A process executed


completely till its end
becomes a TERMINATED
process.
• Other reasons for
terminating a process
include unrecoverable
errors or being aborted by
a user/other process.
• The terminated process will
release all the resources
that have been allotted to
it.
Process States

Five state process model • The EXIT event changes a


process state from running
to terminated
Process States

Five state process model


• Two queues are required in the five-state model,
ready queue and blocked queue
• When an event occurs, the dispatcher has to cycle
through the entire queue to see which process was
waiting for that event. This can cause a huge
overhead when there are many processes
Process States
Suspended Processes
• In a situation where there is not enough memory to
bring in new processes, space can be created by
swapping out some blocked processes to disk.
Process States
Suspended Processes
• A swapped-out process is known as a suspended
process and the queue where it waits in the secondary
storage is called a suspended queue.
• The state of the process is changed from blocked to
BLOCKED-SUSPENDED
• Another state, READY-SUSPENDED, is introduced for the
suspended process whose I/O or event waiting is
completed
• The suspended state introduces the 7-state process
model
Process Control Block
• The operating system maintains process attributes to
manage and control them.
• The attributes are stored in a data structure called a
process control block (PCB).
Process Control Block
Process ID (PID)
It is a unique identification number of the process.

Program Counter (PC)


The address of the next instruction to be executed by the
processor.

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

The termination operation has the following steps.


i. When a process reaches the execution of its last statement, a system
call is executed to tell the OS that the process is terminating.
ii. The OS releases the resources held by the process
Next in Process Management
v Process Scheduling
v Process Synchronization
v Deadlock
v Multithreading
Home Work
1. Form a group of 10 students
2. Go to http://www.teach-sim.com/
3. Download and Install the simulator
4. Download Operating Systems Tutorials
5. In your group, perform HW 1: Investigating Process States and
Memory Management (LO1 and LO2)
6. Upload the results (1 per group) in the LMS
7. LMS course enrolment key cs3342023

You might also like