Unit 2-Process Mnagement
Unit 2-Process Mnagement
Unit 2-Process Mnagement
Chapter – Two
PROCESS MANAGEMENT
2.1 INTRODUCTION TO PROCESS
Sequential Processes: Execution progresses in a sequential fashion, i.e. one after the other.
At any point in time, at most one process is being executed.
PROCESS STATES:
During its lifetime, a process passes through a number of states. The most important states
are, New, Ready, Running, Blocked (waiting) and terminate.
New:
• A process that has just been created but has not yet been admitted to the pool of
executable processes by the operating system.
• Information concerning the process is already maintained in memory but the code is
not loaded and no space has been allocated for the process.
Ready:
• A process that is not currently executing but that is ready to be executed as soon as
the operating system dispatches it.
• The process is in main memory and is available for execution.
Running:
• A process that is currently being executed.
• The process is currently being executed
Blocked (Waiting):
• A process that is waiting for the completion of some event, such as and I/O
operation.
• The process is in main memory and is waiting for an event
Exit (Terminated):
• The process has finished execution
Each process is represented in the operating system by a process control block (PCB)-also
called a task control block. A PCB is shown in Figure-2.2. It contains many pieces of
information associated with a specific process, including these
• Process state: The state may be new, ready, running and 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.
THREADS
A thread is a dispatchable unit of work (lightweight process) that has independent context,
state and stack. A process is a collection of one or more threads and associated system
resources. Traditional operating systems are single-threaded systems.
• User threads are supported above the kernel and are implemented by a thread library
at the user level.
• Kernel threads are supported directly by the operating system; the kernel performs
thread creation, scheduling, and management in kernel space.
Multithreading
The benefits of multithreaded programming can be broken down into four major categories:
• Responsiveness: Multithreading an interactive application may allow a program to
continue running even if part of it is blocked or is performing a lengthy operation,
thereby increasing responsiveness to the user. For instance, a multithreaded web
browser could still allow user interaction in one thread while an image is being
loaded in another thread.
• Resource sharing: By default, threads share the memory and the resources of the
process to which they belong. The benefit of code sharing is that it allows an
application to have several different threads of activity all within the same address
space.
• Economy: Allocating memory and resources for process creation is costly.
Alternatively, because threads share resources of the process to which they belong, it
is more economical to create and context switch threads. It can be difficult to gauge
empirically the difference in overhead for creating and maintaining a process rather
than a thread, but in general it is much more time consuming to create and manage
processes than threads.
The list of processes waiting for a particular I/O device is called a device queue.
Two types of queues are present, the ready queue and a set of device queues.
A new process is initially put in the ready queue. It waits in the ready queue until it is
selected for execution (or dispatched). Once the process is assigned to the CPU and is
executing, one of several events could occur.
The process could issue an I/O request, and then be placed in an I/O queue.
The process could create a new sub-process and wait for its termination.
The process could be removed forcibly from the CPU, as a result of an interrupt, and be put
back in the ready queue.
The processes in the system can execute concurrently, and they must be created and deleted
dynamically. Thus, the operating system must provide a mechanism (or facility) for process
creation and termination.
• If the operating system is to manage processes and resources, it must have information
about the current status of each process and resources.
• The operating system constructs and maintains tables of information, called control
tables.
Process Creation
A process may create several new processes, via a create-process system call, during the
course of execution. The creating process is called a parent process, whereas the new
processes are called the children of that process. Each of these new processes may in turn
create other processes, forming a tree of processes.
When a process creates a new process, two possibilities exist in terms of execution:
1. The parent continues to execute concurrently with its children.
2. The parent waits until some or all of its children have terminated.
There are also two possibilities in terms of the address space of the new process:
1. The child process is a duplicate of the parent process.
2. The child process has a program loaded into it.
Process Termination
A process terminates when it finishes executing its final statement and asks the operating
system to delete it by using the exit system call. At that point, the process may return data
(output) to its parent process (via the wait system call).
All the resources of the process-including physical and virtual memory, open files, and I/O
buffers-are de-allocated by the operating system.
A parent may terminate the execution of one of its children for a variety of reasons, such as
these.
The child has exceeded its usage of some of the resources that it has been allocated. This
requires the parent to have a mechanism to inspect the state of its children.
The task assigned to the child is no longer required.
The parent is exiting, and the operating system does not allow a child to continue if its
parent terminates. On such systems, if a process terminates (either normally or abnormally),
then all its children must also be terminated. This phenomenon, referred to as cascading
termination, is normally initiated by the operating system.
Cooperating Processes
The concurrent processes executing in the operating system may be either independent
processes or cooperating processes. A process is independent if it cannot affect or be
affected by the other processes executing in the system. Clearly, any process that does not
share any data (temporary or persistent) with any other process is independent. On the other
Process Management Page: 6
hand, a process is cooperating if it can affect or be affected by the other processes executing
in the system. Clearly, any process that shares data with other processes is a cooperating
process.
CPU scheduling is the basis of multiprogrammed operating systems. By switching the CPU
among processes, the objective of multiprogramming is to have some process running at all
times, in order to maximize CPU utilization.
CPU Scheduler
Selects from among the processes in memory that are ready to execute, and allocates the
CPU to one of them.
CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state.
2. Switches from running to ready state.
3. Switches from waiting to ready.
4. Terminates.
Scheduling under 1 and 4 is non-preemptive.
All other scheduling is preemptive
Preemptive Scheduling
CPU scheduling decisions may take place under the following four circumstances:
1. When a process switches from the running state to the waiting state (for example, I/O
request, or invocation of wait for the termination of one of the child processes)
2. When a process switches from the running state to the ready state (for example, when an
interrupt occurs).
Dispatcher
Another component involved in the CPU scheduling function is the dispatcher. The
dispatcher is the module that gives control of the CPU to the process selected by the short-
term scheduler. This function involves:
• Switching context.
• Switching to user mode.
• Jumping to the proper location in the user program to restart that program.
Scheduling Criteria
Different CPU-scheduling algorithms have different properties and may favor one class of
processes over another.
• Throughput: If the CPU is busy executing processes, then work is being done. One
measure of work is the number of processes completed per time unit, called
throughput. For long processes, this rate may be 1 process per hour; for short
transactions, throughput might be 10 processes per second.
• Turnaround time: From the point of view of a particular process, the important
criterion is how long it takes to execute that process. The interval from the time of
submission of a process to the time of completion is the turnaround time. Turnaround
time is the sum of the periods spent waiting to get into memory, waiting in the ready
queue, executing on the CPU, and doing I/O.
• Waiting time: The CPU-scheduling algorithm does not affect the amount of time
during which a process executes or does I/O; it affects only the amount of time that a
process spends waiting in the ready queue. Waiting time is the sum of the periods
spent waiting in the ready queue.
• Response time: In an interactive system, turnaround time may not be the best
criterion. Often, a process can produce some output fairly early, and can continue
computing new results while previous results are being output to the user. Thus,
another measure is the time from the submission of a request until the first response is
produced. This measure, called response time, is the amount of time it takes to start
responding, but not the time that it takes to output that response. The turnaround time
is generally limited by the speed of the output device.
1. FIRST-COME FIRST-SERVE:
Consider the following set of processes that arrive at time 0, with the length of the CPU
burst given in milliseconds:
If the processes arrive in the order P1, P2, P3, and are served in FCFS order, we get the
result shown in the following Gantt chart:
The waiting time is 0 milliseconds for process P1, 24 milliseconds for process P2, and 27
milliseconds for process P3. Thus, the average waiting time is (0 + 24 + 27)/3 = 17
milliseconds.
If the processes arrive in the order P2, P3, P1, however, the results will be as shown in the
following Gantt chart:
2. SHORTEST-JOB-FIRST SCHEDULING:
Using SJF scheduling, we would schedule these processes according to the following Gantt
chart:
The waiting time is 3 milliseconds for process P1, 16 milliseconds for process P2, 9
milliseconds for process P3, and 0 milliseconds for process P4. Thus, the average waiting
time is (3 + 16 + 9 + 0)/4 - 7 milliseconds. By comparison, if we were using the FCFS
scheduling scheme, the average waiting time would be 10.25 milliseconds.
The SJF scheduling algorithm is provably optimal, in that it gives the minimum average
waiting time for a given set of processes. Moving a short process before long one decrease
the waiting time of the short process more than it increases the waiting time of the long
process. Consequently, the average waiting time decreases.
A preemptive SJF algorithm will preempt the currently executing process, whereas a non-
preemptive SJF algorithm will allow the currently running process to finish its CPU burst.
A preemptive mode is called Shortest Remaining Time First (SRTF) or Shortest Remaining
Time Next (SRTN).
Example:
A priority is associated with each process, and the CPU is allocated to the process with the
highest priority. Equal-priority processes are scheduled in FCFS order. An SJF algorithm is
simply a priority algorithm where the priority (p) is the inverse of the (predicted) next CPU
burst. The larger the CPU burst, the lower the priority, and vice versa.
we discuss scheduling in terms of high priority and low priority In this text, we assume that
low numbers represent high priority.
As an example, consider the following set of processes, assumed to have arrived at time 0, in
the order P1, P2, • • -, P5, with the length of the CPU burst given in milliseconds:
Using priority scheduling, we would schedule these processes according to the following
Gantt chart:
4. ROUND ROBIN:
To implement RR scheduling, we keep the ready queue as a FIFO queue of processes. New
processes are added to the tail of the ready queue. The CPU scheduler picks the first process
from the ready queue, sets a timer to interrupt after 1 time quantum, and dispatches the
process.
One of two things will then happen. The process may have a CPU burst of less than 1 time
quantum. In this case, the process itself will release the CPU voluntarily. The scheduler will
then proceed to the next process in the ready queue. Otherwise, if the CPU burst of the
currently running process is longer than 1 time quantum, the timer will go off and will cause
an interrupt to the operating system. A context switch will be executed, and the process will
be put at the tail of the ready queue. The CPU scheduler will then select the next process in
the ready queue.
The average waiting time under the RR policy is often long. Consider the following set of
processes that arrive at time 0, with the length of the CPU burst given in milliseconds:
If we use a time quantum of 4 milliseconds, then process P1 gets the first 4 milliseconds.
Since it requires another 20 milliseconds, it is preempted after the first time quantum, and
the CPU is given to the next process in the queue, process P2. Since process P2 does not
need 4 milliseconds, it quits before its time quantum expires. The CPU is then given to the
next process, process P3. Once each process has received 1 time quantum, the CPU is
returned to process P1 for an additional time quantum.
If a process's CPU burst exceeds 1 time quantum, that process is preempted and is put back
in the ready queue. The RR scheduling algorithm is thus preemptive.
Another class of scheduling algorithms has been created for situations in which processes
are easily classified into different groups. For example, a common division is made between
foreground (interactive) processes and background (batch) processes. These two types of
processes have different response-time requirements and so may have different scheduling
needs. In addition, foreground processes may have priority (externally defined) over
background processes.
A multilevel queue scheduling algorithm partitions the ready queue into several separate
queues. The processes are permanently assigned to one queue, generally based on some
property of the process, such as memory size, process priority, or process type. Each queue
has its own scheduling algorithm. For example, separate queues might be used for
foreground and background processes. The foreground queue might be scheduled by an RR
algorithm, while the background queue is scheduled by an FCFS algorithm.
Let's look at an example of a multilevel queue scheduling algorithm with five queues, listed
below in order of priority:
2. Interactive processes
4. Batch processes
5. Student processes
When the multilevel queue scheduling algorithm is used, processes are permanently
assigned to a queue when they enter the system. If there are separate queues for foreground
and background processes, for example, processes do not move from one queue to the other,
since processes do not change their foreground or background nature. This setup has the
advantage of low scheduling overhead, but it is inflexible.
A process entering the ready queue is put in queue 0. A process in queue 0 is given a time
quantum of 8 milliseconds. If it does not finish within this time, it is moved to the tail of
queue 1. If queue 0 is empty, the process at the head of queue 1 is given a quantum of 16
milliseconds. If it does not complete, it is preempted and is put into queue 2. Processes in
queue 2 are run on an FCFS basis but are run only when queues 0 and 1 are empty.
This scheduling algorithm gives highest priority to any process with a CPU burst of 8
milliseconds or less. Such a process will quickly get the CPU, finish its CPU burst, and go
off to its next I/O burst. Processes that need more than 8 but less than 24 milliseconds are
also served quickly, although with lower priority than shorter processes. Long processes
automatically sink to queue 2 and are served in FCFS order with any CPU cycles left over
from queues 0 and 1.
Where two or more processes are reading or writing some shared data and final result
depends on who runs precisely are called race conditions.
Conditions required to avoid race condition
1. No two processes may be simultaneously inside their critical regions.
2. No assumptions may be made about speeds or the number of CPUs.
3. No process running outside its critical region may block other processes.
4. No process should have to wait forever to enter its critical region.
CRITICAL SECTION:
• n processes all competing to use some shared data