Unit 2
Unit 2
PROCESS STATE
The state of a process is defined in part by the current activity of that process.
Each process may be in one of the following states:
The operating system groups all information that it needs about a particular
process into a data structure called a Process Control Block (PCB). It simply serves
as the storage for any information for processes. When a process is created, the
operating system creates a corresponding PCB and when it terminates, its PCB is
released to the pool of free memory locations from which new PCBs are drawn. A
process is eligible to compete for system resources only when it has an active PCB
associated with it. A PCB is implemented as a record containing many pieces of
information associated with a specific process, including:
• Process state:
The state may be new, ready, running, waiting or terminated.
• Program counter:
The counter indicates the address of the next instruction to be executed for
this process.
• CPU registers:
They include accumulator, general purpose registers, index registers etc.
Whenever a processor switches over from one process to another process,
information about current status of the old process is saved in the register
along with the program counter so that the process be allowed to continue
correctly afterwards.
• 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 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:
The information includes the list of I/O devices allocated to this process, a
list of open files and so on.
PROCESSOR SCHEDULING
Scheduling is a fundamental operating system function. All computer resources are
scheduled before use. Since CPU is one of the primary computer resources, its
scheduling is central to operating system design.
Scheduling refers to a set of policies and mechanisms supported by operating
system that controls the order in which the work to be done is completed. A
scheduler is an operating system program (module) that selects the next job to
be admitted for execution. The main objective of scheduling is to increase CPU
utilization and higher throughput. Throughput is the amount of work
accomplished in a given time interval. CPU scheduling is the basis of operating
system which supports multiprogramming concepts. This mechanism improves
the overall efficiency of the computer system by getting more work done in less
time.
Types of Schedulers
This is also called job scheduler. This determines which job shall be admitted for
immediate processing. There are always more processes than it can be executed
by the CPU. These processes are kept in large storage devices like disk for later
processing. The long term scheduler selects processes from this pool and loads
them into memory. In memory these processes belong to a ready queue. The
short term scheduler (also called the CPU scheduler) selects from among the
processes in memory which are ready to execute and assigns the CPU to one of
them. The long term scheduler executes less frequently. The long-term scheduler
controls the degree of multiprogramming – the number of processes in memory. If
the degree of multiprogramming is stable, then the average rate of process
creation must be equal to the average departure rate of processes leaving the
system. Thus, the long-term scheduler may need to be invoked only when a
process leaves the system. Because of longer time taken by CPU during execution,
the long term scheduler can afford to take more time to decide which process
should be selected for execution.
It may also be very important that the long-term scheduler should take a careful
selection of processes, i.e. processes should be a combination of CPU and I/O
bound types. Generally, most processes can be put into any of the two categories:
CPU bound or I/O bound. If all processes are I/O bound, the ready queue will
always be empty and the short-term scheduler will have nothing to do. If all
processes are CPU bound, no process will be waiting for I/O operation and again
the system will be unbalanced. Therefore, the long-term scheduler provides good
performance by selecting a combination of CPU bound and I/O bound processes.
Medium-term schedulers:
Most of the processes require some I/O operation. In that case, it may become
suspended for I/O operation after running a while. It is beneficial to remove these
process (suspended) from main memory to hard disk to make room for other
processes. At some later time these process can be reloaded into memory and
continued where from it was left earlier. Saving of the suspended process is said
to be swapped out or rolled out. The process is swapped in and swapped out by
the medium term scheduler.
The medium term scheduler has nothing to do with the suspended processes. But
the moment the suspending condition is fulfilled, the medium term scheduler get
activated to allocate the memory and swapped in the process and make it ready
for execution. The medium-term scheduler is also helpful for reducing the degree
of multiprogramming, when the long term-term scheduler is absent or minimal.
Short-term scheduler:
This is also called CPU scheduler. When ever the CPU becomes idle, the operating
system must select one of the processes in the ready queue to be executed. The
selection process is carried out by the short-term scheduler. It allocates processes
belonging to ready queue to CPU for immediate processing. It’s main objective is
to maximize CPU utilization. Compared to the other two schedulers this is more
frequent. It must select a new process for execution quite often because a CPU
executes a process only for few milliseconds before it goes for I/O operation. Often
the short term scheduler executes at least once in every 10 milliseconds. If it
takes 1 millisecond to decide to execute a process for 10 milliseconds, the
1/(10+1) = 9% of the CPU is being wasted simply for scheduling the work.
Therefore. it must be very fast.
Scheduling and Performance Criteria:
Different CPU-scheduling algorithms have different properties and may favor one
class of processes over another. In choosing which algorithm to use in a particular
situation, we must consider the properties of the various algorithms. The following
criteria have been suggested for comparing CPU-scheduling algorithms.
• CPU utilization
• Throughput:
• Turnaround time
• Waiting time
• Response time
CPU utilization:
The key idea is that if the CPU is busy all the time, the utilization factor of all the
components of the system will be also high. CPU utilization may range from 0 to
100 percent.
Throughput:
It refers to the amount of work completed in a unit of time. One way to measure
throughput is by means of the number of processes that are completed in a unit of
time. The higher the number of processes, the more work apparently being done
by the system. But this approach is not very useful for comparison because this is
dependent on the characteristics and resource requirement of the process being
executed. Therefore to compare throughput of several scheduling algorithms it
should be fed the process with similar requirements.
Turnaround time:
From the point of view of a particular process, the important criterion is how long
it takes to execute that process. Turnaround time may be defined as the interval
from the time of submission of a process to the time of its completion. It is the
sum of the periods spent waiting to get into memory, waiting in the ready queue,
executing on the CPU and I/O operations.
Waiting time:
Response time:
It is most frequently considered in time sharing and real time operating systems.
However it’s characteristics differs in the two systems. In time sharing system it
may be defined as interval from the time the last character of a command line of a
program or transaction is entered to the time the last result appears on the
terminal. In real time system it may be defined as interval from the time an
internal or external event is signaled to the time the first instruction of the
respective service routine is executed.
One of the problems in designing schedulers and selecting a set of its performance
criteria is that they often conflict with each other. For example, the fastest
response time in time sharing and real time system may result in low CPU
utilization. Throughput and CPU utilization may be increased by executing large
number of processes, but then response time may suffer. Therefore, the design of
a scheduler usually requires a balance of all the different requirements and
constraints.
Scheduling Algorithms
CPU scheduling deals with the problem of deciding which of the processes in the
ready queue is to be allocated the CPU. There are several scheduling algorithms
exists. A major division among scheduling algorithms is that whether they support
pre-emptive or non-preemptive scheduling discipline. A scheduling discipline
is non-preemptive if once a process has been given the CPU, the CPU cannot be
taken away from that process. A scheduling discipline is pre-emptive if the CPU
can be taken away. Preemptive scheduling is more useful in high priority process
which requires immediate response. In non-preemptive systems, jobs are made to
wait by longer jobs, but the treatment of all processes is fairer. The decision
whether to schedule preemptive or not depends on the environment and the type
of application most likely to be supported by a given operating system.
First-Come-First-Served Scheduling:
This is one of the simplest scheduling algorithm. With this scheme, the process
that requests the CPU first is allocated the CPU first. The implementation of the
FCFS policy is easily managed with a FIFO queue. When a process enters the
ready queue, its PCB is linked onto the tail of the queue. When the CPU is free, it
is allocated to the process at the head of the queue. The running process is then
removed from the queue.
P1 P2
0 20 24
If both processes P1 and P2 arrive in order P1 -P2 in quick succession, the
turnaround times are 20 and 24 units of time respectively (P2 must wait for P1 to
complete) thus giving an average of 22 (20+24)/2) units of time. The
corresponding waiting times are 0 and 20 units of time with average of 1 0 time
units.
P2 P1
0 4 24
However, when the same processes arrive in P2-P 1 order, the turn around times
are 4 and 24 units of time respectively giving an average of 14 (4+24)/2) units of
time and the average waiting time is(0+4)/2)=2. This is a substantial reduction.
This shows how short jobs may be delayed in FCFS scheduling algorithm.
The FCFS scheduling algorithm is non preemptive. Once the CPU has been
allocated to a process, that process keeps the CPU until it releases the CPU, either
by terminating or by requesting I/O. The FCFS algorithm is particularly
troublesome for time-sharing, where each user needs to get a share of the CPU at
regular intervals.
Shortest-Job-First Scheduling:
0 3 8 16 26
The SJF scheduling algorithm is provably optimal, in that it gives the minimum
average waiting time for a given set of processes. By moving a short process
before a long one, the waiting time of the short process decreases more than it
increases the waiting time of the long process. Consequently, the average waiting
time decreases.
P1 0 8
P2 1 4
P3 2 9
P4 3 5
P1 P2 P4 P1 P3
0 1 5 10 17 26
Process P1 is started at time 0, since it is the only process in the queue. Process
P2 arrives at time 1. The remaining time for process P1 is larger than the time
required by P2, so P1 is preempted, and P2 is scheduled. The average waiting time
for this example is
Priority Scheduling:
A priority is associated with each process and the scheduler always picks up the
highest priority process for execution from the ready queue. Equal priority
processes are scheduled in FCFS order. An SJF algorithm is simply a priority
algorithm where the priority is the inverse of the next CPU burst. The larger the
CPU burst, the lower the priority, and vice versa. The level of priority may be
determined on the basis of resource requirements, processes characteristics and
its run time behavior.
This is one of the oldest, simplest and widely used algorithm. The round-robin
scheduling algorithm is primarily used in a time-sharing and a multi-user system
where the primary requirement is to provide reasonably good response times and
in general to share the system fairly among all system users. It is similar to FCFS
scheduling, but preemption is added to switch between processes. A small unit of
time, called a time quantum (or time slice), is defined. A time quantum is
generally from 10 to 100 milliseconds. The ready queue is treated as a circular
queue. The CPU scheduler goes around the ready queue, allocating the CPU to
each process for a time interval of up to 1 time quantum.
Completed job
CPU
X Z Y X
Preemption
If we use a time-slice of 5 units of time, then P1 gets the first 5 units of time.
Since it requires another 20 units of time, it is pre-empted after the first time slice
and the CPU is given to the next process i.e. P2. Since P2 just needs 5 units of
time, it terminates as time-slice expires. The CPU is then given to the next process
P3. Once each process has received one time slice, the CPU is returned to P1 for
an additional time-slice. Thus the resulting round robin schedule is:
P1 P2 P3 P1 P1 P1 P1
0 5 10 15 20 25 30 35
In MLQ, processes are classified into different groups. For example, interactive
processes (foreground) and batch processes (background) could be considered as
two types of processes because of their different response time requirements,
scheduling needs and priorities.
System Process
High priority
Scheduler
Interactive Process
Round Robin Switch
CPU
Scheduler
First Cum
Batch Process
First Serve
In addition, there must be scheduling among the queues. There are different
possibilities to manage queues. One possibility is to assign a time slice to each
queue, which it can schedule among different processes in its queue. Foreground
processes can be assigned 80% of CPU whereas background processes are given
20% of the CPU time.
The second possibility is to execute the high priority queue first. For example, no
process in the batch queue could run unless the queue for system processes and
interactive processes were all empty. If an interactive process entered the ready
queue while a batch process is running, the batch process would be pre-empted.
Interprocess Communication
• Convenience: Even an individual user may work on may tasks at the same
time. For instance, a user may be editing, printing, and compiling in parallel.
Cooperating processes require an interprocess communication (IPC) mechanism
that will allow them to exchange data and information. Inter Process
Communication is a capability supported by operating system that allows one
process to communicate with another process. The processes can be running on
the same computer or on different computers connected through a network. IPC
enables one application to control another application, and for several applications
to share the same data without interfering with one another.
• Critical Resource
• Critical Section
• Mutual Exclusion
• Shared Memory
• Message Passing
• send(message)
• receive(message)
Direct Communication
Each process that wants to communicate must explicitly name the recipient or
sender of the communication. In this scheme the send and receive primitives are:
Symmetry in addressing
This scheme shows the symmetry in addressing; that is both the sender and the
receiver processes must name the other to communicate.
Asymmetry in addressing
Only the sender names the recipient; the recipient is not required to name the
sender.
Indirect Communication
With indirect communication, the message are sent to and receive from a mailbox.
It is an object into which messages may be placed and from which messages may
be removed by processes. Each mailbox owns a unique identification. A process
may communicate with some other process by a number of different mailboxes.
If the mailbox is owned by a process, then the owner who can only receive from
this mailbox and the user who can only send message to the mailbox are to be
distinguished. When a process that owns a mailbox terminates, its mailbox
disappears.
It has an existence of its own, i.e., it is independent and not attached to any
particular process.
• destroy a mailbox
Buffering:
• Zero capacity
• Bounded capacity
• Unbounded capacity
Zero capacity
The queue has maximum length 0; thus, the link cannot have any messages
waiting in it. In this case, the sender must block until the recipient receives the
message. The zero-capacity link is referred to as a message-passing system with
no buffering.
Bounded capacity
The queue has finite length n; thus, at most n messages can reside in it. If a new
message is sent, and the queue is not full, it is placed in the queue either by
copying the message or by keeping a pointer to the message and the sender can
continue execution without waiting. If the link is full, the sender must block until
space is available in the queue.
Unbounded capacity
The queue has potentially infinite length; thus, any number of messages can wait
in it. The sender never blocks. Bounded and Unbounded capacity link is referred to
as message-passing system with automatic buffering.