You are on page 1of 33

CS837: ADV.

OPERATING SYSTEMS
PROCESS DESCRIPTION AND CONTROL

Dr. Mian M.Hamayun


mian.hamayun@seecs.edu.pk
http://seecs.nust.edu.pk/faculty/mianhamayun.html
Process 

• Definition: 
– “A process is a program in execution” 
 
• Process is defined as the unit of resource 
ownership and protection 
 
Process 
• Protection: 
– Protected access to processors, other processes, 
files, I/O 
– Operating system protects process to prevent 
unwanted interference between processes 
• Resource ownership: 
– Processes own address space (Virtual memory 
space to hold the process image) 
– Processes own a set of resources (I/O devices, I/O 
channels, files, main memory) 
Process 
• Processes are a fundamental concept of an 
operating system 
– Unit of execution 
• enables the concurrent execution of multiple programs on a 
system 
• CPU switches between processes 
– A program appears to “own” a computer system 
• In reality, due to frequent context switches, it shares a 
computer system with many other programs 
• All the running software on a computer system, 
including the operating system itself, is organised 
as a set of processes 
Process Representation 
• Three basic components 
– Executable program code 
– Data related to the program 
– Execution context 
• Process ID, group ID, user ID 
• Stack pointer, program counter, CPU registers 
• File descriptors, locks, network sockets 
• Execution context essential for process 
switching 
 
Process Memory Layout 
max 
• Process Image  Stack  Stack segment 
– Layout of process in memory 
• Segments 
– Stack segment 
• Used for function calls 
– Data segment  Data segment 
• Static variables, constants 
• Dynamic allocation of memory  Heap 
from the heap 
– Text segment  Data 
• Contains the program code, 
shared between processes  Program code  Text segment

Kernel  protected 

Virtualisation  max 
max  Stack 
max  Stack
max 
• Each program invoked  Stack 
Stack 
results in the creation 
of a separate process, 
each with its own 
process image 
• Each process image  Heap 
Heap 
Heap 
appears to “own” the Heap 
Data 
Data 
complete address space  Data 
Data 
Program code 
Program code 
Program code 
Program code  Kernel 
0  Kernel 
0  Kernel 
0  Kernel 

Virtual Address Space 
• Process image appears to be a huge contiguous 
memory area 
– Starts at address 0, up to the maximum possible memory 
address 
– Each of the segments starts at a particular address 
– Each process image starts at address 0 – how can this be? 
• Virtual address space 
– Abstraction of physical memory 
– Independent of the actual size of physical memory 
• Mechanisms needed to map addresses in the process 
image to actual physical memory locations 
Process Control 
Process Control Block 
Process Identifier  A unique identifier associated with the process 

Process State  The state of the process, when it was interrupted 

Priority  Priority level relative to other processes 

Program counter  Address of next instruction to be executed 

Memory pointers  Pointers to program code and data 

Context data  Register content 

I/O status information  Outstanding I/O requests, assigned I/O devices, list of open files 

Accounting information  Used processor time, time limits etc. 

. . . 

• The Process Control Block (PCB) contains all the 
information necessary to suspend a process and 
later resume its execution 
Process Control Block 
• Process identification 
– Process ID 
– Identifier of creating process (parent process), user ID, group ID 
• Processor state information 
– CPU registers: user­visible registers, control and status registers, stack 
pointers, program counter 
• Process control information 
– CPU scheduling information: process state, priority, scheduling­related 
information 
– Process privileges: access privileges to memory, allowed instructions 
– Memory management information: information about the virtual 
memory assigned to this process (segment, paging tables, various 
control registers) 
– Accounting information: time limits, accumulated CPU time etc. 
– I/O status information: resource ownership, open files, allocated I/O 
devices 
Process Table 
• Process Table 
– holds one entry per 
process 
– Process Control Block 
Process Control 
• Operating system controls process execution 
– Process state representation and transition 
– Process creation, allocation of memory, CPU and 
other resources 
– Dispatching: Queuing of processes to dispatch 
them for execution by a CPU 
– Scheduling: select the “best” process for dispatch 
– Interruption, signalling, process termination, 
protection 
Process States 
• At any time, a process will be in a particular 
state 
– New 
– Ready to be executed, with all resources assigned 
– Currently executing 
– Waiting for I/O, blocked 
– Swapped out 
– Terminated 
Five­State Process Model 

Dispatch 

Admit  Release 
New  Ready  Running  Exit 
Timeout 

Wait­occurred 
Wait­for­event 

Blocked 
Five­State Process Model 
• Process states 
– New: process is created, but not yet admitted to the pool 
of executable processes by the operating system 
• Process Control Block created 
• Program may not be loaded yet into main memory 
– Ready: process is prepared for execution and waits to be 
assigned to a processor 
– Running: process is executing 
– Blocked: a process waits for some event to occur (such as 
the reception of a signal or the completion of an I/O 
operation) 
– Exit: process is released from pool of executable processes, 
because it halted or aborted 
 
Process Creation 
• Principle Events that lead to the creation of processes 
– System boot 
• When a system is initialised, several processes are started 
• Under Unix, these are background processes, called “daemons”, such
as sched (pid 0) and init (pid 1), and others such as email server, logon 
server, inetd etc. 
– An existing process spawns a child process 
• A process invokes a system call for creating a new process 
• e.g.: a server process (web server) may spawn a child process for each 
request handled 
– User request to create a new process 
• User types command in shell or selects widget in GUI to start a 
program, this creates a new process with the shell as the parent 
– A batch system takes on the next job in line (e.g. Jobs in a 
printer queue) 
Process Creation 
• New: process is created, but not yet admitted to the 
pool of executable processes by the operating system 
– Process Control Block created 
– Program may not be loaded yet into main memory 
• Assign unique process identifier to the new process 
• Allocate space for the process 
– Allocate memory for process image (program, data, stack) 
• Initialise the Process Control Block 
• Add process to the Ready queue 
Process Execution 
Process Execution 
Dispatch 
2. 
1. Process blocked for I/O 
Ready  Running  2. Dispatcher schedules another 
Timeout  3.  process 
3. Dispatcher interrupts process 
Wait­occurred  1.  because its time slice expired 
4.  Wait­for­event  4. Input becomes available, blocked 
process made ready 
Blocked 

• We can distinguish three basic process states during 
execution 
– Running: actually using the CPU 
– Ready: being runnable, temporarily stopped (time­out) to 
let another process execute 
– Blocked/Waiting: unable to run until some external event 
happens, such as I/O completion 
Context Switch 
• Switch running process into ready or blocked 
state 
• Switch next ready process into running state and 
allocate CPU 
• This is called “context switch”: 
– The current state of the CPU (all its registers) are 
stored in the PCB of the process currently executing 
– Another process is selected from the ready queue – 
this requires a scheduling decision 
– The state of this process, held the process PCB, is 
loaded into the CPU 
 
Context Switch 
Dispatch Events 
• When does a context switch occur? 
• Clock interrupt, occurs after a specified time 
interval, usually 3­10ms 
– Execution of processes interrupted, control goes 
back to operating system 
– Process added to the ready queue 
– Frequency of such an interrupt important system 
parameter 
• Balance overhead of context switch vs. responsiveness 
 
Dispatch Events 
• I/O Interrupt 
– I/O interrupt occurs, when I/O action has occurred and data is loaded into 
memory 
– Currently executing process (Running state) is interrupted 
– All blocked processes waiting for this I/O action to be completed are moved 
into the Ready queue 
– Dispatcher must decide whether to continue execution of the process 
currently in Running state (has been interrupted) 
• Memory fault / Page fault 
– Executing process refers to a virtual memory address that is not allocated a 
physical memory location (data still on hard disk) 
– Currently executing process (Running state) is interrupted 
– I/O request for bringing in data from secondary storage is issued 
– Currently executing process in Running state is switched to blocked 
– Switch to another process of the Ready queue 
– When I/O completed, blocked process moved back to Ready queue 
 
Dispatcher 
• Central part of the operating system 
• Dispatcher is the OS function that allocates 
CPU to processes, switches CPU from one 
process to the next 
– Processes are “dispatched” for execution by the
operating system 
• Performs context switch 
• Manages a “Ready” queue, dispatches next
process from this queue 
Dispatcher 
Ready Queue  Release 
Admit  Dispatch 
Processor

Timeout or Yield 

Blocked Queue 
Event   Event Wait 
Occurs 

• Implementation: 
– Using one “Ready” and one “Blocked” queue 
– Weakness: 
• When a particular event occurs, ALL processes waiting for this event have to 
be transferred from the “Blocked” queue to the “Ready” queue 
• Operating system has to look through all the entries in the Blocked queue to 
select the right processes for transfer 
Process Scheduling / Dispatch Queues 
• Operating system maintains a set of queues to 
manage process scheduling 
– Job queue: set of all processes in the system 
– Ready queue: set of all processes residing in memory 
and ready for execution 
• Memory allocated for process image 
– Queues for waiting processes 
• Either one Blocked queue or different queues for processes 
waiting for particular events 
– E.g.: Device queue: processes that wait for an I/O device to 
become available 
• Processes migrate between these queues 
Process Management 
• Multiple Event Queues 
Ready Queue  Release 
Admit  Dispatch 
Processor

Timeout or Yield 

Event 1 Queue 
Event 1   Event 1 Wait 
Occurs 
Event 2 Queue 
Event 2   Event 2 Wait 
Occurs 

Event n Queue 
Event n   Event n Wait 
Occurs 
Swapping 
• Virtualisation 
– Computer system appears to allow an “unlimited”
number of processes to execute concurrently 
• Not all of them may fit into physical memory 
• Swapping 
– Move a process to secondary memory 
• We need two extra process states 
– “ready­suspended” 
– “blocked­suspended” 
Process States with Suspend 

New 
Admit  Suspend 
Admit 
Dispatch 

Activate 
Ready,  Release 
Ready  Running  Exit 
Suspend 
Timeout 
Suspend 
Wait­for­event 
Wait­occurred  Wait­occurred 

Activate 
Blocked, 
Blocked 
Suspend 

Suspend 
Process Termination 
• Normal termination 
– Program ends itself 
• Abnormal termination 
– OS intervenes, user sends kill signal (CTRL­C) 
– Access to memory locations that are forbidden 
– Time out 
– I/O errors 
– Not enough memory, stack overflow 
– Parent process terminated 
– Etc. 
Process Termination 
• Events 
– Normal exit (voluntary) / Error exit (voluntary) 
• Regular completion of a process, with or without error code 
• Process voluntarily executes the  exit(errNo) system call to 
indicate to the operating system that it has finished. 
– Fatal error (involuntary) 
• Uncatchable or uncaught 
• Service errors, such as: no memory left for allocation, I/O error, 
etc. 
• Total time limit exceeded 
• Arithmetic error, out­of­bounds memory access, etc. 
– Killed by another process via the kernel (involuntary) 
• The process receives a  SIGKILL signal 
• In some systems, the parent takes down all its children with it 
Process of the Operating System 
Kernel
• There are different forms of kernels 
– Kernel operations separate from user operations 
• Only user programs are executing as processes 
• OS code executes in privileged mode, OS is a supervisor 
– All OS software is virtually executed within a user 
process, only Dispatcher outside user programs 
(process switching functionality 
• OS is a collection of routines that are called by user 
programs 
• Mode switch occurs when user calls system routines 
– OS runs as a collection of system processes, 
dispatcher (process switching) is a small separate part

You might also like