You are on page 1of 42

Lecture-4

Process
Management

Presented By –
M.M. Rakibul Hasan
Faculty, CSE, IUBAT
Content of this Lecture
• Process Management Introduction • Modes of Process Execution
• Process Elements • Process Switching
• Example of Process Execution • Mode Switching
• Process Creation and termination
• Two-State & Five-State Model
• Suspended Process Reference Reading-
Operating Systems -
• Process System Control Structures
William Stalling 6th
• Typical Elements of a Process Image edition (Chapter-03)

• Process Location and attributes

M.M. Rakibul Hasan CSC-307[Operating Systems] 2


Process Management Introduction
 Process –
• A program in execution.
• When we execute a program then it’s a process.
• Process execution must progress in sequential fashion.
• A process doesn’t indicate separate CPU.
 Process Management-
After initiation of a program we demand-
• Program should be completed as early as possible.
• Or at least the CPU should start execution of my program
as early as possible.
M.M. Rakibul Hasan CSC-307[Operating Systems] 3
Process Elements
Each process is represented in the OS by a Process Control Block (PCB) –
also called a task control block.
 Identifier: Unique process ID
 State: new, ready, running, waiting, halted and so on
 Priority: Process priority
 Program counter: indicates the address of the next instruction to
be executed for this process
 Memory pointers: includes pointer to the program code and data
associated, plus any memory blocks shared with other processes
 Context data: value of CPU registers
 I/O status information: includes outstanding I/O requests,
assigned I/O devices, and a list of files in use and so on
 Accounting information: amount of processor time & clock time
used, time limits, account numbers, and so on
M.M. Rakibul Hasan CSC-307[Operating Systems] 4
Example of Process Execution
 Trace
• The behavior of an individual process can be characterized
by listing the sequence of instructions that execute for that
process
• The behavior of the processor can be characterized by
showing how the traces of the various processes are
interleaved

 Dispatcher
• A small program that switches the processor from one
process to another
M.M. Rakibul Hasan CSC-307[Operating Systems] 5
Example of Process Execution (Cont’)

M.M. Rakibul Hasan CSC-307[Operating Systems] 6


Example of Process Execution (Cont’)

Timeou
I/O
t

Figure: Memory-layout of Three


processes

M.M. Rakibul Hasan CSC-307[Operating Systems] 7


Process Creation
A process may create several
new process during the
execution time.
The creating process is called
a parent process, and the new
processes are called the
children of that process.
Each of the new processes
may in turn create other
processes, forming a tree of
Processes Tree on a UNIX System
processes.
M.M. Rakibul Hasan CSC-307[Operating Systems] 8
Process Creation (Cont’)
 Resource sharing
•Parent and children share all resources.
•Children share subset of parent’s resources.
•Parent and child share no resources.
 Execution
•Parent and children execute concurrently.
•Parent waits until children terminate.
 Address space
•Child duplicate of parent.
•Child has a program loaded into it.
M.M. Rakibul Hasan CSC-307[Operating Systems] 9
Reasons for Process Creation

M.M. Rakibul Hasan CSC-307[Operating Systems] 10


Process Termination
 Process executes last statement and asks the operating system to
decide it exit:
•Output data from child to parent (via wait).
•Process’ resources are deallocated by operating system.

 Parent may terminate execution of children processes (abort):


•Child has exceeded allocated resources.
•Task assigned to child is no longer required.
 Parent is exiting:
Operating system does not allow child to continue if its parent
terminates. This phenomenon referred to as cascading termination.
M.M. Rakibul Hasan CSC-307[Operating Systems] 11
Reasons for Process Termination

M.M. Rakibul Hasan CSC-307[Operating Systems] 12


Reasons for Process Termination (Cont’)

M.M. Rakibul Hasan CSC-307[Operating Systems] 13


Two-State Model
• Process may be in one of two
states
• Running
• Not-running
• Problems of this model
– Not-running
• ready to execute
– Blocked
• Waiting for I/O operation to complete
– Dispatcher could not just select the process at oldest end
of the queue
– Rather, Dispatcher would have to scan the list looking for
the process that is not blocked and that has been in the
queue the longest
M.M. Rakibul Hasan CSC-307[Operating Systems] 14
Five-State Model

 As a process executes, it changes state:


 new: The process is being created.
 ready: The process is waiting to be assigned to a process.
 running: Instructions are being executed.
 waiting: The process is waiting for some event to occur.
 terminated: The process has finished execution.
M.M. Rakibul Hasan CSC-307[Operating Systems] 15
Five-State Model Transitions
• Null  New: Process is created
• New  Ready: OS is ready to handle
another process
• Ready  Running: Select another
process to run
• Running  Exit: Process has terminated
• Running  Ready: End of time slice or
higher-priority process is ready
• Running  Blocked: Process is waiting for
an event
• Blocked  Ready: The event a process is
waiting for has occurred, can continue
• Ready  Exit: Process terminated by OS
or parent
• Blocked  Exit: Same reasons

M.M. Rakibul Hasan CSC-307[Operating Systems] 16


Suspended Processes
• Processor is faster than I/O so all processes could be waiting for
I/O
• The OS may need to suspend some processes, i.e.:
– Swap these processes to disk to free up more memory
– When swapped to disk, blocked state becomes suspend
state
•Two independent concept here:
 Whether a process is waiting on an event (blocked or not)
 Whether a process is swapped of main memory (suspended or not)
• Two new states added
• Blocked/Suspend
• The process is in secondary memory and awaiting an
event
• Ready/Suspend
• The process is in secondary memory but is available
for execution as soon as it is loaded into main
memory
M.M. Rakibul Hasan CSC-307[Operating Systems] 17
New State Transitions
• Blocked --> Blocked Suspend
• When all processes are blocked, the OS will
make room to bring a ready process in
memory

• Blocked Suspend --> Ready Suspend


• When the event for which it as been waiting
occurs (state info is available to OS)

• Ready Suspend --> Ready


• When no more ready process in main
memory

• Ready--> Ready Suspend (unlikely)


• When there are no blocked processes and
must free memory for adequate
performance
M.M. Rakibul Hasan CSC-307[Operating Systems] 18
Reasons for Process Suspension

M.M. Rakibul Hasan CSC-307[Operating Systems] 19


Process Description
• The OS controls events within the computer system
• Fundamentally, the OS is that entity that manages the use of
system resources by processes

• What Information does the OS need to control processes and


manage resources for them?
M.M. Rakibul Hasan CSC-307[Operating Systems] 20
Process System Control Structures
• OS must have information about the current status of each
process and resource
• OS constructs and maintains tables of information about each
entity that it is managing

• So, what control tables does an OS have?

M.M. Rakibul Hasan CSC-307[Operating Systems] 21


General Structure of OS Control Tables
• An OS maintains the following
tables:
• Memory tables
• I/O tables
• File tables
• Process tables

M.M. Rakibul Hasan CSC-307[Operating Systems] 22


Memory Tables
• Used to keep track of both main (real) and secondary (virtual)
memory
• Some of main memory is reserved for used by the OS; the remainder
is available for use by processes
• Processes are maintained on secondary memory using some sort of
Virtual memory or simple swapping mechanism
• Memory table must include the following information
• Allocation of main memory to processes
• Allocation of secondary memory to processes
• Any protection attributes of blocks of main/virtual memory, such as, which
processes may access to shared memory regions
• Any information needed to manage virtual memory
M.M. Rakibul Hasan CSC-307[Operating Systems] 23
I/O Tables
• Used by OS to manage the I/O devices and channels of the
computer system
• At any given time, an I/O device may be available or assigned
to a particular process

• If an I/O operation is in progress, the OS needs to know:


• The status of the I/O operation and
• The location in main memory being used as the source or destination of
the I/O transfer

M.M. Rakibul Hasan CSC-307[Operating Systems] 24


File Tables
• These tables provide information about:
• existence of files,
• their location on secondary memory,
• their current status, and
• other attributes

• These information may be maintained and used by a file


management system
• In which case, OS has little or no knowledge of files

• In other OSs, much of the detail of file management is


managed by the OS itself
M.M. Rakibul Hasan CSC-307[Operating Systems] 25
Process Tables
• OS must maintain Process tables to manage processes
• Before proceeding to the discussion of an examination of the
required process tables, two additional points needs to make
• First, There must be some reference to the resources (such as memory,
I/O, and files) directly or indirectly, in process tables
 The tables themselves must be accessible by the OS and therefore are
subject to memory management
• Second, how does the OS know to create the tables in the first place?
 OS must have some knowledge of the basic environment
 This is an issue of configuration (mostly done by auto-configuration
software)

M.M. Rakibul Hasan CSC-307[Operating Systems] 26


Process Control Structures
• To manage and control a process the OS must know
• Where the process is located?
• The attributes of the process that are necessary for its management

M.M. Rakibul Hasan CSC-307[Operating Systems] 27


Typical Elements of a Process Image

M.M. Rakibul Hasan CSC-307[Operating Systems] 28


Process Location
• Where are the processes located?
• In simplest case, the process image is maintained as a contiguous, or
continuous, block of memory
• Secondary memory, usually disk
• To manage and execute a process, at least a small portion of its image must be
maintained in main memory
• To execute process, entire process image must be loaded in main memory or at
least virtual memory
• Modern OSs presume paging hardware that allows noncontiguous
physical memory to support partially resident processes
• At any given time, a portion of a process image may be in main memory , with
remainder in secondary memory
• OS must know the location of each page of each process image, achieved by
process tables
M.M. Rakibul Hasan CSC-307[Operating Systems] 29
Process Attributes
• A sophisticated multiprogramming system requires a great deal
of information about each process
• This information can be considered to reside in a process control
block
• The process control block information can be grouped into three
categories:
• Process identification
• Process state information
• Process control information

M.M. Rakibul Hasan CSC-307[Operating Systems] 30


Typical Elements of a Process Control Block

M.M. Rakibul Hasan CSC-307[Operating Systems] 31


Typical Elements of a Process Control Block

M.M. Rakibul Hasan CSC-307[Operating Systems] 32


Rules of Process Control Block
• PCB is the most important data structure in an OS
• Contains all of the information about a process that is needed by the OS
• Blocks are read and/or modified by virtually every module in the OS
• PCB defines the state of the OS

• Difficulty is not access, but protection


• A bug in a single routine, such as interrupt handler, could damage PCBs, which could
destroy the system’s ability to manage the affected processes
• A design change in the structure of PCB could affect a number of modules in the OS

• These problems can be addressed by requiring all


routines in OS to go through a handler routine which
• will protect PCBs, and
• is the sole arbiter for reading and writing these blocks
M.M. Rakibul Hasan CSC-307[Operating Systems] 33
Process Control
Modes of Execution
• User Mode
• Less-privileged mode
• User programs typically execute in this mode

• System Mode
• More-privileged mode
• Also referred to as control mode or kernel mode
• Kernel mode refers to kernel of OS that encompasses the
important system functions

M.M. Rakibul Hasan CSC-307[Operating Systems] 34


Typical Functions of an OS Kernel

M.M. Rakibul Hasan CSC-307[Operating Systems] 35


Process Creation
• Once the OS decides to create a new process it
can proceed as follows:

M.M. Rakibul Hasan CSC-307[Operating Systems] 36


Process Switching
• A process switch may occurred any time that OS has gained control
from the currently running process

• Two types of system interrupts


• Interrupt
• Trap

M.M. Rakibul Hasan CSC-307[Operating Systems] 37


• Interrupt
System Interrupts
• Due to some sort of event that is external to and independent of the
currently running process
 clock interrupt
Time slice: the maximum amount of time that a process can
execute before being interrupted
 I/O interrupt
 memory fault
• Trap
• An error or exception condition generated within the currently
running process
• OS determines if the error or exception condition is fatal
 If so, currently running process is moved to the Exit state and a process
switch occurs
 If not, action of the OS will depend on the nature of the error and the
design of the OS
• Attempt some recovery procedure or simply notify the user
M.M. Rakibul Hasan CSC-307[Operating Systems] 38
Mode Switching
• If no interrupts are pending the processor:
• Proceeds to the fetch stage and fetches the next instruction of the current program
in the current process

• If an interrupt is pending the processor:


• Sets the program counter to the starting address of an interrupt handler program
• Switches from user mode to kernel mode so that the interrupt processing code may
include privileged instructions

M.M. Rakibul Hasan CSC-307[Operating Systems] 39


Change of Process State
If the currently running process is to be moved to another state (Ready, Blocked, etc.),
then the OS must make substantial changes in its environment
• The steps in a full process switch are:

M.M. Rakibul Hasan CSC-307[Operating Systems] 40


Exercise
1. Define Process with its elements.
2. What are the common reasons for process termination?
3. Draw two and five state model diagram and mention their limitation.
4. Draw seven state model diagram? List down the reasons for Process Suspension.
5. For what types of entities does the OS maintain tables of information for management purposes?
6. Describe about process image.
7. List three general categories of information in a process control block.
8. What are the typical functions an OS Kernel needs to perform?
9. What are the steps performed by an OS to create a new process? List down the common reasons
for process creation.
10. What is the difference between an interrupt and a trap?
11. What is the difference between a mode switch and a process switch?
M.M. Rakibul Hasan CSC-307[Operating Systems] 41
Every end is
a new beginning!!!

You might also like