You are on page 1of 34

Chapter-2

Process Management
Process Concept
When the OS runs a program (i.e., a binary executable), this program is
loaded into memory and the control is transferred to this programs first
instruction. Then, the program starts to run.
A process is a program in execution.
A process is more than a program, because the former has a program
counter, stack, data section and so on (i.e., the runtime stuffs).
Moreover, multiple processes may be associated with one program (e.g.,
runs the same program, a web browser, twice).
An operating system executes a variety of programs:
Batch system jobs
Time-shared systems user programs or tasks

(Process in Memory)

Process State
At any moment, a process can be in one of the five states: new, running, waiting,
ready and terminated.
1. New: The process is being created
2. Running: The process is being executed
3. Waiting: The process is waiting for some event to occur (e.g., waiting for I/O
completion)
4. Ready: The process is waiting to be assigned to a processor.
5. Terminated: The process has finished execution.

(Process State Diagram)

Process Control Block

Information associated with each process


Process state: The state may be new, ready running, waiting, halted, and so
on.
Program counter: The counter indicates the address of the next instruction
to be executed for this process.
CPU registers: The registers vary in number and type, depending on the
computer architecture. They include accumulators, index registers, stack
pointers, and general-purpose registers, plus any condition-code information.
Along with the program counter, this state information must be saved when
an interrupt occurs, to allow the process to be continued correctly afterward
CPU scheduling information: This information includes a process priority,
pointers to scheduling queues, and any other scheduling parameters.
Memory-management information: This information may include such
information as the value of the base and limit registers, the page tables, or
the segment tables, depending on the memory system used by the operating
system
Accounting information: This information includes the amount of CPU and
real time used, time limits, account numbers, job or process numbers, and so
on.
I/O status information: This information includes the list of I/O devices
allocated to the process, a list of open files, and so on.

Threads
A process is a program that performs a single thread of execution.

Process Scheduling
Since the number of processes is always larger than the number of CPUs, the
OS must maintain maximum CPU utilization.
To determine which process can do what, processes are chained into a
number of
Scheduling queues.
For example, in addition to the ready queue, each event may have its own
scheduling queue (i.e., waiting queue).
Process Scheduling Queues
Job queue set of all processes in the system
Ready queue set of all processes residing in main memory, ready and
waiting to execute
Device queues set of processes waiting for an I/O device
Processes migrate among the various queues
Ready Queue And Various I/O Device Queues

Queuing Diagram for Scheduling

Schedulers
There are three types of schedulers
1. Long-Term (Job) Scheduler:
Selects which processes should be brought into the ready queue.
Invoked very infrequently (seconds, minutes); may be slow.
controls the degree of multiprogramming
2. Short-Term (CPU) scheduler:
Selects which process should execute next and allocates CPU.
invoked very frequently (milliseconds) - must be very fast
3. Medium-Term Scheduler:
swaps out process temporarily
balances load for better throughput

Addition of Medium Term Scheduling

Short-term scheduler is invoked very frequently (milliseconds) (must be


fast)
Long-term scheduler is invoked very infrequently (seconds, minutes)
(may be slow)
The long-term scheduler controls the degree of multiprogramming
Processes can be described as either:
o I/O-bound process spends more time doing I/O than computations,
many short CPU bursts
o CPU-bound process spends more time doing computations; few
very long CPU bursts
Context Switch
What is a process context?
The context of a process includes the values of CPU registers, the process
state, the program counter, and other memory/file management information.
What is a context switch?
After the CPU scheduler selects a process (from the ready queue) and before
allocates CPU to it, the CPU scheduler must
save the context of the currently running process,
put it into a queue,
load the context of the selected process, and
Let it run.

When CPU switches to another process, the system must save the state of the
old process and load the saved state for the new process
Context-switch time is overhead; the system does no useful work while
switching
Time dependent on hardware support

Operations on Processes
There are three commonly seen operations:
Process Creation: Create a new process. The newly created is the child of the
original. Use fork() or vfork() in Unix to create new processes.
Process Termination: Terminate the execution of a process. Under Unix, use
exit().
Process Join: Wait for the completion of a child process. Under Unix use
wait().
fork(), vfork(), exit() and wait() are system calls.
Use ps aux to see all running processes
Process Creation
Parent process create children processes, which, in turn create other
processes, forming a tree of processes

Resource sharing
o Parent and children share all resources
o Children share subset of parents resources
o Parent and child share no resources
Execution
o Parent and children execute concurrently
o Parent waits until children terminate
Address space
o Child duplicate of parent
o Child has a program loaded into it
UNIX examples
o fork system call creates new process
o exec system call used after a fork to replace the process memory
space with a new program

(Process creation using fork() system call.)

C Program Forking Separate Process


int main()
{
pid_t pid;
/* fork another process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0) { /* child process */
execlp("/bin/ls", "ls", NULL);
}
else { /* parent process */
/* parent will wait for the child to complete */
wait (NULL);
printf ("Child Complete");
exit(0);
}
}
Process Termination
Process executes last statement and asks the operating system to delete 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
If parent is exiting
Some operating system do not allow child to continue if its
parent terminates
All children terminated - cascading termination

Interprocess Communications (IPC)


Cooperating Processes
Independent process cannot affect or be affected by the execution of another
process
Cooperating process can affect or be affected by the execution of another
process
Advantages of process cooperation
o Information sharing
o Computation speed-up
o Modularity
o Convenience
Cooperating Processes Importance
Information sharing: Multiple processes can use the same set of
information (e.g., files).
Computation Speedup: One may split a process into multiple
processes to use multiple processors.
Modularity: A system can be divided into separate processes. Use the
ps command to see how many processes are not yours!
Convenience: A user may have multiple tasks to work at the same
time (e.g., editing, browsing, printing).
However, handling cooperating processes is difficult.
Cooperating processes must communicate to get the job done.
There are two types of communications:
a) Processes that share the same memory: locks, semaphores, monitors,
and others.(Shared Systems)
b) Processes that are running in a distributed environment: message
passing, sockets, remote procedure calls (RPC).(Message Passing)
Mechanism for processes to communicate and to synchronize their actions
Message system processes communicate with each other without resorting
to shared variables
IPC facility provides two operations:

a) send(message) message size fixed or variable


b) receive(message)
If P and Q wish to communicate, they need to:
a) establish a communication link between them
b) exchange messages via send/receive
Implementation of communication link
a) physical (e.g., shared memory, hardware bus)
b) logical (e.g., logical properties)

(a) Message passing

Shared Memory System

(b) Shared System

Message passing system


Message Passing system are divided into three types:
Naming
Direct Communication
o Processes must name each other explicitly:
send (P, message) send a message to process P
receive(Q, message) receive a message from process Q
o Properties of communication link
Links are established automatically
A link is associated with exactly one pair of
communicating processes
Between each pair there exists exactly one link
The link may be unidirectional, but is usually bidirectional
Indirect Communication
o Messages are directed and received from mailboxes (also referred to
as ports)
Each mailbox has a unique id
Processes can communicate only if they share a mailbox
o Properties of communication link
Link established only if processes share a common mailbox
A link may be associated with many processes
Each pair of processes may share several communication links
Link may be unidirectional or bi-directional
o Operations
create a new mailbox
send and receive messages through mailbox
destroy a mailbox
o Primitives are defined as:

send(A, message) send a message to mailbox A


receive(A, message) receive a message from mailbox A
o Mailbox sharing
P1, P2, and P3 share mailbox A
P1, sends; P2 and P3 receive
Who gets the message?
o Solutions
Allow a link to be associated with at most two processes
Allow only one process at a time to execute a receive operation
Allow the system to select arbitrarily the receiver. Sender is
notified who the receiver was.

Indirect Communication using mailboxes

Synchronization
o Message passing may be either blocking or non-blocking
o Blocking is considered synchronous
Blocking send has the sender block until the message is
received
Blocking receive has the receiver block until a message is
available
o Non-blocking is considered asynchronous
Non-blocking send has the sender send the message and
continue
Non-blocking receive has the receiver receive a valid message
or null

Buffering
Queue of messages attached to the link; implemented in one of three ways
1.
Zero capacity 0 messages Sender must wait for receiver
(rendezvous)
2.
Bounded capacity finite length of n messages Sender must wait if
link full
3.
Unbounded capacity infinite length Sender never waits

Process Scheduling: Basic concepts


Maximum CPU utilization obtained with multiprogramming
CPUI/O Burst Cycle Process execution consists of a cycle of CPU
execution and I/O wait
CPU burst distribution
CPU-I/O Burst Cycle
Alternating Sequence of CPU and I/O Bursts

Process execution repeats the CPU burst and I/O burst cycle.
When a process begins an I/O burst, another process can use the CPU for a
CPU burst.

Histogram of CPU-burst durations.

The durations of CPU bursts have been measured extensively. Although they
vary greatly from process to process and from computer to computer they tend to
have a frequency curve similar to that shown in the above Figure. The curve is
generally characterized as exponential or hyper exponential, with a large number of
short CPU bursts and a small number of long CPU bursts. An I/O-bound program
typically has many short CPU bursts. A CPU-bound program might have a few
long CPU bursts. This distribution can be important in the selection of an
appropriate CPU-scheduling algorithm.
CPU-bound and I/O-bound
A process is CPU-bound if it generates I/O requests infrequently, using more
of its time doing computation.
A process is I/O-bound if it spends more of its time to do I/O than it spends
doing computation.
A CPU-bound process might have a few very long CPU bursts.
An I/O-bound process typically has many short CPU bursts.

Levels of Scheduling
High Level Scheduling or Job Scheduling
o Selects jobs allowed to compete for CPU and other system resources.
Intermediate Level Scheduling or Medium Term Scheduling
o Selects which jobs to temporarily suspend/resume to smooth
fluctuations in system load.
Low Level (CPU) Scheduling or Dispatching
o Selects the ready process that will be assigned the CPU
o Ready Queue contains PCBs of processes.

CPU Scheduler
What does a CPU scheduler do?
When the CPU is idle, the OS must select another process to run.
This selection process is carried out by the short-term scheduler (or CPU
scheduler).
The CPU scheduler selects a process from the ready queue, and allocates
the CPU to it.
The ready queue does not have to be a FIFO one. There are many ways to
organize the ready queue.
Selects from among the processes in memory that are ready to execute, and
allocates the CPU to one of them.
Non-preemptive Scheduling
o Once CPU has been allocated to a process, the process keeps the CPU
until
Process exits OR

Process switches to waiting state


Preemptive Scheduling
o Process can be interrupted and must release the CPU.
Need to coordinate access to shared data
CPU scheduling decisions may take place when a process:
switches from running state to waiting state
switches from running state to ready state
switches from waiting to ready
terminates
Scheduling under 1 and 4 is non-preemptive.
All other scheduling is preemptive.

Preemptive vs. Non-preemptive

Non-preemptive scheduling: scheduling occurs when a process voluntarily


enters the wait state (case 1) or terminates (case 4).
o Simple, but very inefficient
Preemptive scheduling: scheduling occurs in all possible cases.
o What if the kernel is in its critical section modifying some important
data? Mutual exclusion may be violated.
o The kernel must pay special attention to this situation and, hence, is
more complex.
Dispatcher
Scheduling Flow and Dispatcher

The dispatcher is the last step in scheduling. It


o Switches context
o Switches to user mode
o Braches to the stored program counter to resume the programs
execution.
Dispatcher module gives control of the CPU to the process selected by the
short-term scheduler
Dispatch Latency:
o time it takes for the dispatcher to stop one process and start another
running. Or the time to switch two processes.
o Dispatcher must be fast.

Scheduling Criteria
There are many criteria for comparing different scheduling algorithms. Here are
five common ones:
1.
2.
3.
4.
5.

CPU Utilization
Throughput
Turnaround Time
Waiting Time
Response Time

CPU Utilization:
o Keep the CPU and other resources as busy as possible
o CPU utilization ranges from 0 to 100 percent.
o Normally 40% is lightly loaded and 90% or higher is heavily loaded.
Throughput:
o Number of processes that complete their execution per time unit.
o Higher throughput means more jobs get done.
o However, for long processes, this rate may be one job per hour, and, for
short (student) jobs, this rate may be 10 per minute.
Turnaround time:
o Amount of time to execute a particular process from its entry time.
o From a users point of view, turnaround time is more important than CPU
utilization and throughput.
o Turnaround time is the sum of
waiting time before entering the system
waiting time in the ready queue
waiting time in all other events (e.g., I/O)
time the process actually running on the CPU

Waiting time :
Amount of time a process has been waiting in the ready queue.
o Why only ready queue?

CPU scheduling algorithms do not affect the amount of time


during which a process is waiting for I/O and other events.
However, CPU scheduling algorithms do affect the time that a
process stays in the ready queue.
Response Time (in a time-sharing environment)
o amount of time it takes from when a request was submitted until the first
response is produced, NOT output.
o For example: in front of your workstation, you perhaps care more about the
time between hitting the Return key and getting your first output than the
time from hitting the Return key to the completion of your program (e.g.,
turnaround time).
Optimization Criteria
1.
2.
3.
4.
5.

Max CPU utilization


Max throughput
Min turnaround time
Min waiting time
Min response time

Scheduling Algorithms
We will discuss a number of scheduling algorithms:
1. First-Come, First-Served (FCFS)
2. Shortest-Job-First (SJF)
3. Priority
4. Round-Robin
5. Multilevel Queue
6. Multilevel Feedback Queue

1. First-Come, First-Served:
Policy: Process that requests the CPU FIRST is allocated the CPU FIRST.
o FCFS is a non-preemptive algorithm.
Implementation - using FIFO queues
o Incoming process is added to the tail of the queue.
o Process selected for execution is taken from head of queue.
Performance metric - Average waiting time in queue.
FCFS is not preemptive. Once a process has the CPU, it will occupy the
CPU until the process completes or voluntarily enters the wait state.
Example
Process Burst Time
P1

24

P2

P3

Suppose that the processes arrive in the order: P1 , P2 , P3


The Gantt Chart for the schedule is:

P1

24

P2

27

P3

30

Waiting time for P1 = 0; P2 = 24; P3 = 27


Average waiting time: (0 + 24 + 27)/3 = 17

Example 2
Suppose that the processes arrive in the order
P2 , P3 , P1
Then Gantt chart for the schedule is:
0

P2

P3

P1

30

Waiting time for P1 = 6; P2 = 0; P3 = 3


Average waiting time: (6 + 0 + 3)/3 = 3
Much better than previous case
Convoy effect short process behind long process

FCFS: Problems
It is easy to have the convoy effect: all the processes wait for the one
big process to get off the CPU. CPU utilization may be low. Consider
a CPU-bound process running with many I/O-bound process.
It is in favor of long processes and may not be fair to those short ones.
What if your 1-minute job is behind a 10-hour job?
It is troublesome for time-sharing systems, where each user needs to
get a share of the CPU at regular intervals.
Shortest-Job-First (SJF) Scheduling
Each process in the ready queue is associated with the length of its next CPU
burst.

When a process must be selected from the ready queue, the process with the
smallest next CPU burst is selected.
Thus, the processes in the ready queue are sorted in CPU burst length.
Two schemes:
o non preemptive once CPU given to the process it cannot be
preempted until completes its CPU burst
o Preemptive if a new process arrives with CPU burst length less than
remaining time of current executing process, preempt. This scheme is
known as the Shortest-Remaining-Time-First (SRTF)
SJF is optimal gives minimum average waiting time for a given set of
processes
Example of Non-Preemptive SJF
Process

Arrival Time

Burst Time

P1

0.0

P2

2.0

P3

4.0

P4

5.0

SJF (non-preemptive)
0

P1
3

P3
7 8

P2

12

P4

16

Average waiting time = (0 + 6 + 3 + 7)/4 = 4


Example for Preemptive SJF
If the process arrive in this order:

Proces Arrival
s
Time
A
B
C
D
How the Scheduling done:

Gang chart

0
1
2
3

Burs
t
Time
8
4
9
5

Avg wait =(9+0+15+2)/4 =26/4 =6.5


Other way: (10- 1) + (1 - 1) + (17- 2) +(5-3)]/ 4 = 26/4 = 6.5

How do we know the next CPU burst?


o Without a good answer to this question, SJF cannot be used for CPU
scheduling.qWe try to predict the next CPU burst!
o Let tn be the length of the nth CPU burst and pn+1 be the prediction of the
next CPU burst where a is a weight value in [0,1].
o If a = 0, then pn+1 = pn and recent history has no effect. If a = 1, only the
last burst matters. If a is , the actual burst and predict values are equally
important.
Estimating the next burst: Example:
o Initially, we have to guess the value of p1 because we have no history value.
o The following is an example with a = .

Define

SJF Problems:
It is difficult to estimate the next burst time value accurately.
SJF is in favor of short jobs. As a result, some long jobs may not have a
chance to run at all. This is starvation.
The preemptive version is usually referred to as shortest-remaining-timefirst scheduling, because scheduling is based on the remaining time of a
process.
Priority Scheduling
A priority number (integer) is associated with each process. Can be based on
o Cost to user
o Importance to user
o Aging
o %CPU time used in last X hours.

The CPU is allocated to the process with the highest priority (smallest
integer highest priority)
o Preemptive
o nonpreemptive
SJF is a priority scheduling where priority is the predicted next CPU burst
time
Problem Starvation low priority processes may never execute
Solution Aging as time progresses increase the priority of the process

The scheduler always picks the process (in ready queue) with the highest
priority to run.

Priority Scheduling: Example


Four jobs A, B, C and D come into the system in this order at about
the same time. Subscripts are priority. Smaller means higher.

Then:

Average wait time = (0+7+17+23)/4 = 47/4 = 11.75


Average turnaround time= (7+17+23+28)/4 = 75/4 = 18.75
Priority Scheduling: Starvation

Priority scheduling can be non-preemptive or preemptive.


With preemptive priority scheduling, if the newly arrived process has
a higher priority than the running one, the latter is preempted.
Indefinite block (or starvation) may occur: a low priority process may
never have a chance to run.
Priority Scheduling: Aging
Aging is a technique to overcome the starvation problem.
Aging: gradually increases the priority of processes that wait in the system
for a long time.
Example:
o If 0 is the highest (resp., lowest) priority, then we could decrease
(resp., increase) the priority of a waiting process by 1 every fixed
period (e.g., every minute).
Round-Robin (RR) Scheduling
RR is similar to FCFS, except that each process is assigned a time quantum.
All processes in the ready queue is a FIFO list.
When the CPU is free, the scheduler picks the first and lets it run for one
time quantum.
If that process uses CPU for less than one time quantum, it is moved to the
tail of the list.
Otherwise, when one time quantum is up, that process is preempted by the
scheduler and moved to the tail of the list.
Example of RR with Time Quantum = 20
Process
P1
P2
P3
P4
The Gantt chart is:

Burst Time
53
17
68
24

P1
0

P2
20

P3
37

P4
57

P1
77

P3
97

P4
117

P1
121

P3
134

P3
154

Typically, higher average turnaround than SJF, but better response

Other Examples:

162

Thir
d example:

Time Quantum and Context Switch Time

Turnaround Time Varies With The Time Quantum

RR Scheduling: Some Issues


If time quantum is too large, RR reduces to FCFS
If time quantum is too small, RR becomes processor sharing
Context switching may affect RRs performance
o Shorter time quantum means more context switches
Turnaround time also depends on the size of time quantum.
In general, 80% of the CPU bursts should be shorter than the time quantum
Multilevel Queue Scheduling

Ready queue is partitioned into separate queues:


foreground (interactive)
background (batch)
Each queue has its own scheduling algorithm
o Foreground RR
o Background FCFS
Scheduling must be done between the queues
o Fixed priority scheduling; (i.e., serve all from foreground then from
background). Possibility of starvation.
o Time slice each queue gets a certain amount of CPU time which it
can schedule amongst its processes; i.e., 80% to foreground in RR
o 20% to background in FCFS
A priority is assigned to each queue. A higher priority process may preempt
a lower priority process.

A process P can run only if all queues above the queue that contains P are
empty.
When a process is running and a process in a higher priority queue comes in,
the running process is preempted.

Multilevel Feedback Queue

Multilevel queue with feedback scheduling is similar to multilevel queue;


however, it allows processes to move between queues.
If a process uses more (resp., less) CPU time, it is moved to a queue of
lower (resp., higher) priority.
As a result, I/O-bound (resp., CPU-bound) processes will be in higher (resp.,
lower) priority queues.
Multilevel-feedback-queue scheduler defined by the following parameters:
o number of queues
o scheduling algorithms for each queue
o method used to determine when to upgrade a process
o method used to determine when to demote a process
o method used to determine which queue a process will enter when that
process needs service
Multilevel Feedback Queues

Example of Multilevel Feedback Queue


Three queues:
o Q0 RR with time quantum 8 milliseconds
o Q1 RR time quantum 16 milliseconds
o Q2 FCFS

Scheduling
o A new job enters queue Q0 which is served FCFS. When it gains CPU,
job receives 8 milliseconds. If it does not finish in 8 milliseconds, job
is moved to queue Q1.
o At Q1 job is again served FCFS and receives 16 additional
milliseconds. If it still does not complete, it is preempted and moved
to queue Q2.

You might also like