0% found this document useful (0 votes)
4 views

Process

Unit II covers process management, including the concept of processes, process scheduling, and inter-process communication. It discusses various scheduling algorithms, process states, and the role of the Process Control Block (PCB) in managing processes. Additionally, it details process creation and termination, including the use of system calls in UNIX/Linux for managing processes.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Process

Unit II covers process management, including the concept of processes, process scheduling, and inter-process communication. It discusses various scheduling algorithms, process states, and the role of the Process Control Block (PCB) in managing processes. Additionally, it details process creation and termination, including the use of system calls in UNIX/Linux for managing processes.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 64

Unit II - PROCESS MANAGEMENT

Processes - Process Concept - Process Scheduling - Operations on Processes -


Inter-process Communication; CPU Scheduling - Scheduling criteria - Scheduling
algorithms: Threads -Multithread Models – Threading issues; Process
Synchronization - The Critical-Section problem -Synchronization hardware –
Semaphores – Mutex - Classical problems of synchronization -Monitors; Deadlock
- Methods for handling deadlocks, Deadlock prevention, Deadlock avoidance,
Deadlock detection, Recovery from deadlock.

Process
A process can be thought
of as a program in
execution (or) A process
is the unit of work in a
modern time-sharing
system.
A process will need
certain resources such
as CPU time, memory,
files and I/O devices to
accomplish its task.
These resources are
allocated to the process
either when it is created
or while it is executing
Process
 A process can be thought of as a program in execution (or) A process is the
unit of work in a modern time-sharing system.
 A process will need certain resources such as CPU time, memory, files and
I/O devices to accomplish its task. These resources are allocated to the
process either when it is created or while it is executing.
Process
A process can be thought of as a program in execution (or) A process is the unit of
work in a
modern time-sharing system.
A process will need certain resources such as CPU time, memory, files and I/O
devices to
accomplish its task.
These resources are allocated to the process either when it is created or while it is
executing
Process Concept:
 An operating system executes a variety of programs: A batch system
executes jobs, whereas a time-shared system has user programs, or tasks
 We will use the terms job and process almost interchangeably.
 Process – is a program in execution (informal definition).
 Program is passive entity stored on disk (executable file), process is active
o Program becomes process when executable file loaded into memory.
 Execution of program started via GUI, command line entry of its name, etc.
 One program can be several processes
o Consider multiple users executing the same program.
The below figure shows the structure of process in memory:

 The process contains several sections: Text, Data, Heap and Stack.
 Text Section contains the program code. It also includes the current
activity, as
 represented by the value of the program counter and the contents of
the processor’s registers.
 Process stack contains temporary data such as function parameters,
return addresses and local variables.
 Data section contains global variables.
 Heap is memory that is dynamically allocated during process run
time.
 Difference between Program and Process:
 A program is a passive entity, such as a file containing a list of
instructions stored on disk
 often called an executable file.
 A process is an active entity with a program counter specifying the
next instruction to
 execute and a set of associated resources.
 A program becomes a process when an executable file is loaded into
memory.
 Two common techniques for loading executable files are double-
clicking an icon representing the executable file and entering the name
of the executable file on the command line as in prog.exe or a.out.

Although two processes may be associated with the same program, they are
considered as two separate execution sequences.
For instance, several users may be running different copies of the mail
program or the same user may invoke many copies of the web browser
program. Each of these is considered as a separate process.
Process State
As a process executes, it changes state. The process state defines the current
activity of that process.
A process may be in one of the following states:
 New: The process is being created.
 Ready: The process is waiting to be assigned to a processor.
 Running: Instructions are being executed.
 Waiting: The process is waiting for some event to occur such as an
I/O completion or reception of a signal.
 Terminated: The process has finished execution.

Note: Only one process can be running on any processor at any instant of
time.
Process Control Block
Each process is represented in the operating system by a Process Control
Block (PCB). It is also called a Task Control Block.
PCB serves as the repository for any information that may vary from process
to process.

The PCB contains information related to process such as:

Process state: The state may be new, ready, running, waiting and
terminated.
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 etc. 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 includes the base


and limit registers values, 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 timem 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.
Process Scheduling:

 The process scheduling is the activity of the process manager that


handles the removal of the running process from the CPU and the
selection of another process on the basis of a particular strategy.
 The objective of multiprogramming is to have some process running
at all times, to maximize CPU utilization.
 The objective of time sharing is to switch the CPU among processes
so frequently that users can interact with each program while it is
running.
 To meet these objectives the process scheduler selects an available
process (possibly from a set of several available processes) for
program execution on the CPU.
 For a single-processor system, there will never be more than one
running process. If there are more processes, the rest will have to wait
until the CPU is free and can be rescheduled.
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. Stored as a linked list.

Device queues – set of processes waiting for an I/O device. Processes


migrate among the various queues.

A common representation of process scheduling is a queueing diagram. Two


types of queues are present: the ready queue and a set of device queues.

The circles represent the resources that serve the queues, and the arrows
indicate the flow of processes in the system.

A new process is initially put in the ready queue.


It waits there until it is selected for execution, or dispatched. Once the
process is allocated 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 child process and wait for the child’s
termination.

• The process could be removed forcibly from the CPU, as a result of


an interrupt, and be put back in the ready queue.

Schedulers

 Schedulers are special system software which handle process


scheduling in various ways. Their main task is to select the jobs to be
submitted into the system and to decide which process to run
A process migrates among the various scheduling queues throughout its
lifetime. For scheduling purpose, the operating system must select processes
from these queues.

The selection process is carried out by the Scheduler.

There are three types of Schedulers are used:

1. Long Term Scheduler

2. Short Term Scheduler

3. Medium Term Scheduler

Long Term Scheduler

 It is also called a job scheduler. A long-term scheduler determines


which programs are admitted to the system for processing. It selects
processes from the queue and loads them into memory for execution.
Process loads into the memory for CPU scheduling.
 The primary objective of the job scheduler is to provide a balanced
mix of jobs, such as I/O bound and processor bound. It also controls
the degree of multiprogramming. 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.
 On some systems, the long-term scheduler may not be available or
minimal. Time-sharing operating systems have no long-term
scheduler. When a process changes the state from new to ready, then
there is use of long-term scheduler.
Short Term Scheduler

o It is also called as CPU scheduler. Its main objective is to increase system


performance in accordance with the chosen set of criteria. It is the change of
ready state to running state of the process.
o CPU scheduler selects a process among the processes that are ready to execute
and allocates CPU to one of them.
o Short-term schedulers, also known as dispatchers, make the decision of which
process to execute next. Short-term schedulers are faster than long-term
schedulers.

Medium Term Scheduler

 Medium-term scheduling is a part of swapping. It removes the processes from


the memory. It reduces the degree of multiprogramming.
 The medium-term scheduler is in-charge of handling the swapped out-
processes.
 A running process may become suspended if it makes an I/O request. A
suspended processes cannot make any progress towards completion.
 In this condition, to remove the process from memory and make space for other
processes, the suspended process is moved to the secondary storage.
 This process is called swapping, and the process is said to be swapped out or
rolled out. Swapping may be necessary to improve the process mix.

The processes can be described as two types:

1. I/O bound process is one that spends more of its time doing I/O than it spends
doing computations.

2. CPU Bound process using more of its time doing computations and generates
I/O requests infrequently.

The long-term scheduler selects a good process mix of I/O-bound and CPU-bound
processes.

 If all processes are I/O bound, the ready queue will almost always be empty and
the CPU will remain idle for long time because I/O device processing takes a lot of
time.
 If all processes are CPU bound, the I/O waiting queue will almost always be
empty. I/O devices will be idle and CPU is busy for most of the time.
 Thus if the system maintains the combination of CPU bound and I/O bound
processes then the system performance will be increased.

Note: Time-sharing systems such as UNIX and Microsoft Windows systems often
have no long-term scheduler but simply put every new process in memory for the
short-term scheduler.

Context Switching

 Switching the CPU from one process to another process requires performing a
state save of the current process and a state restore of a different process. This
task is known as a Context Switch.
 The context is represented in the PCB of the process. It includes the value of the
CPU registers, the process state and memory-management information.
 When a context switch occurs, the kernel saves the context of the old process in
its PCB and loads the saved context of the new process scheduled to run.
 Context-switch time is pure overhead, because the system does no useful work
while switching. Context switch time may be in few milliseconds.

Operations on Processes

1. Process Creation

2. Process Termination

Process Creation
 During the execution of a process in its life time, a process may create
several new processes.
 The creating process is called a parent process and the new processes are
called children process.
 Each of these new processes may create other processes forming a tree of
processes.
 Operating system identifies processes according to process identifier (pid).
 Pid provides an unique integer number for each process in the system.
 Pid can be used as an index to access various attributes of a process within
the kernel.
 The below figure shows the process tree for the Linux OS that shows the
name of each process and its pid. In Linux process is called task.

 The init process always has a pid of 1. The init process serves as the root
parent process for all user processes.
 Once the system has booted, the init process can also create various user
processes, such as a web or print server, an ssh server etc.
 kthreadd and sshd are child processes of init.
 The kthreadd process is responsible for creating additional processes that
perform tasks on behalf of the kernel.
 The sshd process is responsible for managing clients that connect to the
system by using secure shell (ssh).

ps command is used to obtain a list of processes:

ps –el

 The command will list complete information for all processes currently
active in the system.
 When a process creates a child process, that child process will need certain
resources such as CPU time, memory, files, I/O devices to accomplish its
task.
 A child process may be able to obtain its resources directly from the
operating system or
 it may be constrained to a subset of the resources of the parent process.
 The parent may have to partition its resources among its children or it may
be able to share some resources such as memory or files among several of its
children.
 Restricting a child process to a subset of the parent’s resources prevents any
process from overloading the system by creating too many child processes.

When a process creates a new process there exist two possibilities for 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 address-space possibilities for the new process:
1. The child process is a duplicate of the parent process (i.e) it has the same
program and

data as the parent.

2. The child process has a new program loaded into it.

Process System calls in Unix/ Linux: fork( ), exec( ), wait( ), exit( ) fork( ):

 In UNIX OS a new process is created by the fork( ) system call.


 The new process consists of a copy of the address space of the original
process. This mechanism allows the parent process to communicate easily
with its child process.
 Both the parent and the child processes continue execution at the instruction
after the
 fork( ).
 For the new child process (i.e. Child Process) the return code for the fork( )
is zero.
 The nonzero process identifier of the child is returned to the parent. exec( ):
After a fork( ) system call, one of the two processes typically uses the
exec( )
 system call to replace the process’s memory space with a new program.
 The exec( ) system call loads a binary file into memory and starts its
execution.
 In this way, the two processes are able to communicate and then go their
separate ways.
 wait( ): The parent can create more children or if the parent has nothing else
to do while
 the child process is running then the parent process can issue a wait( )
system call to move itself out of the Ready Queue until the child process
terminates.
 The call to exec( ) overlays the process’s address space with a new program
or the call to exec( ) does not return control unless an error occurs.

Program for Creating a separate process using the UNIX fork( ) system
call
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main( )
{
pid_t pid;
/* fork a child process */
pid = fork( );
if (pid < 0)
{ /* error occurred */
fprintf(stderr, "Fork Failed");
return 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");
}
return 0;
}
The above C program shows the UNIX system calls fork, exec, wait. Two
different processes are running copies of the same program.
 The only difference is that the value of pid for the child process is
zero, while the value of pid for the parent is an integer value greater
than zero (i.e. the actual pid of the child process).
 The child process inherits privileges and scheduling attributes from
the parent, as well as certain resources such as open files.
 The child process then overlays its address space with the UNIX
command /bin/ls (used to get a directory listing) using the execlp( )
system call (execlp( ) is a version of the exec( ) system call).
 The parent waits for the child process to complete with the wait( )
system call.
 When the child process completes by either implicitly or explicitly
invoking exit( ), the parent process resumes from the call to wait( ),
where it completes using the exit( ) system call.

Process Termination: exit( )


 A process terminates when it finishes executing its final statement and
asks the operating system to delete it by using the exit( ) system call.
 The process may return a status value 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 deallocated by the operating
system.
 A parent may terminate the execution of one of its children for a
variety of reasons such as:
1. The child has exceeded its usage of some of the resources that it has been
allocated.
2. The task assigned to the child is no longer required.
3. The parent is exiting and the operating system does not allow a child to
continue if its parent terminates.
Cascading Termination
If a parent process terminates either normally or abnormally then all its children
must also be terminated is referred as Cascading Termination. It is normally
initiated by operating system.
In Linux and UNIX systems, a process can be terminate by using the exit( )
system call providing an exit status as a parameter:
/* exit with status 1 */
exit(1);
Under normal termination, exit( ) may be called either directly (i.e. exit(1)) or
indirectly (i.e. by a return statement in main( ) ).
A parent process may wait for the termination of a child process by using the
wait( ) system call. The wait( ) system call is passed a parameter that allows the
parent to obtain the exit
status of the child. This system call also returns the process identifier of the
terminated child so that the parent can tell which of its children has terminated:
pid_t pid;
int status;
pid = wait(&status);
Zombie process
A process that has terminated but whose parent has not yet called wait( ) is known
as a zombie process.
When a process terminates, its resources are deallocated by the operating system.
Its entry in the process table must remain there until the parent calls wait( ),
because the process table contains the process’s exit status.
Once the parent calls wait( ), the process identifier of the zombie process and its
entry in the process table are released.
Orphan Processes
If a parent did not invoke wait( ) and instead terminated, thereby leaving its child
processes as orphans are called Orphan processes.
Linux and UNIX address this scenario by assigning the init process as the new
parent to orphan processes.
The init process periodically invokes wait( ), thereby allowing the exit status of
any orphaned process to be collected and releasing the orphan’s process identifier
and process-table entry.

INTERPROCESS COMMUNICATION:
A process is cooperating if it can affect or be affected by the other processes
executing in the system.
There are several reasons for providing an environment that allows process
cooperation:
• Information sharing. Since several users may be interested in the same piece of
information (for instance, a shared file), we must provide an environment to allow
concurrent access to such information.
• Computation speedup. If we want a particular task to run faster, we must break
it into subtasks, each of which will be executing in parallel with the others. Notice
that such a speedup can be achieved only if the computer
has multiple processing cores.
• Modularity. We may want to construct the systemin a modular fashion, dividing
the system functions into separate processes or threads
• Convenience. Even an individual user may work on many tasks at the
same time. For instance, a user may be editing, listening to music, and
compiling in parallel.
Cooperating processes require an interprocess communication (IPC) mechanism
that will allow them to exchange data and information. There are two fundamental
models of interprocess communication: shared memoryand message
passing.
 In the shared-memory model, a region of memory that is shared by cooperating
processes is established. Processes can then exchange information by reading
and writing data to the shared region.
 In the message-passing model, communication takes place by means of
messages exchanged between the cooperating processes.
Shared memory systems:
 If the process wants to initiate the communication and it has some data to
share, then establish the shared memory region in its address space.
 After that, another process wants to communicate and tries to read the
shared data, and must attach itself to the initiating process’s shared address
space.
considering two processes. The diagram is shown below –
 Let the two cooperating processes P1 and P2. Both the processes P1 and P2,
have their different address spaces. Now let us assume, P1 wants to share
some data with P2.

So, P1 and P2 will have to perform the following steps −

 Step 1 − Process P1 has some data to share with process P2. First P1 takes
initiative and establishes a shared memory region in its own address space
and stores the data or information to be shared in its shared memory region.
 Step 2 − Now, P2 requires the information stored in the shared segment of
P1. So, process P2 needs to attach itself to the shared address space of P1.
Now, P2 can read out the data from there.
 Step 3 − The two processes can exchange information by reading and
writing data in the shared segment of the process.

Let’s consider the producer–consumer problem, which is a common


paradigm for cooperating processes.

A producer process produces information that is consumed by a consumer


process.

For example, a compiler may produce assembly code that is consumed by


an assembler. The assembler, in turn, may produce object modules that are
consumed by the loader. The producer–consumer problem

The producer process using shared memory :


item next produced;
while (true) { /* produce an item in next produced
*/
while (((in + 1) % BUFFER SIZE) == out)
; /* do nothing */
buffer[in] = next produced;
in = (in + 1) % BUFFER SIZE;
}
 One solution to the producer–consumer problem uses shared memory.
 To allow producer and consumer processes to run concurrently, we must
have available a buffer of items that can be filled by the producer and
emptied by the consumer.
 This buffer will reside in a region of memory that is shared by the producer
and consumer processes.
 A producer can produce one item while the consumer is consuming another
item.
 The producer and consumer must be synchronized, so that the consumer
does not try to consume an item that has not yet been produced.
Two types of buffers can be used.
 The unbounded buffer places no practical limit on the size of the buffer.
The consumer may have to wait for new items, but the producer can always
produce new items. The bounded buffer assumes a fixed buffer size.
 In this case, the consumer the following variables reside in a of memory
shared by the producer and consumer processes:

#define BUFFER SIZE 10


typedef struct {
...
}item;
item buffer[BUFFER SIZE];
int in = 0;
int out = 0;

The consumer process using shared memory.


item next consumed;
while (true) { while (in == out)
; /* do nothing */
next consumed = buffer[out];
out = (out + 1) % BUFFER SIZE;
/* consume the item in next consumed */
}

Message-Passing Systems
 Message passing provides a mechanism to allow processes to communicate
and to synchronize their actions without sharing the same address space.
 It is particularly useful in a distributed environment, where the
communicating processes may reside on different computers connected by a
network.
 For example, an Internet chat program could be designed so that chat
participants communicate with one another by exchanging messages.

A message-passing facility provides at least two operations:


send(message) receive(message)
Messages sent by a process can be either fixed or variable in size.
Fixed size:
The system-level implementation is straightforward. This restriction, makes the
task of programming more difficult.
variable-sized messages:
variable-sized messages require a more complex system- level implementation,
but the programming task becomes simpler.
If processes P and Q want to communicate, they must send messages to and
receive messages from each other: a communication link must exist between
them.
Here are several methods for logically implementing a link and the
send()/receive()
operations:

• Direct or indirect communication


• Synchronous or asynchronous communication
• Automatic or explicit buffering

Issues related to each of these features:


 Naming
 Synchronization
 Buffering

Naming :
Under 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 defined as:
send(P, message)—Send a message to process P.
receive(Q, message)—Receive a message from process Q.
A communication link in this scheme has the following properties:
 A link is established automatically between every pair of processes that
want to communicate.
 A link is associated with exactly two processes.
 Between each pair of processes, there exists exactly one link

Symmetry in addressing: both the sender process and the receiver process must
name the other to communicate.
Asymmetry: A variant of this scheme employs asymmetry in addressing.
Here, only the sender names the recipient; the recipient is not required to name the
sender.
In this scheme, the send() and receive() primitives are defined as follows:
• send(P, message)—Send a message to process P.
• receive(id, message)—Receive a message from any process. The variable id is set
to the name of the process with which communication has taken place.
Symmetry: eg., E-mail, Asymmetric : Broadcast message or sending parcel (mail
delivery).
Disadvantage of direct communication:

Changing the identifier of a process may necessitate examining all other process
definitions.
Indirect communication:
 In indirect communication, the sender and receiver do not directly know each
other or interact with each other directly.
 Instead, they communicate by sending messages to a shared intermediate
object, often called a mailbox or port
 A process can communicate with another process via a number of different
mailboxes, but two processes can communicate only if they have a shared
mailbox.
 The send() and receive() primitives are defined as follows:
 send(A, message)—Send a message to mailbox A.
 receive(A, message)—Receive a message from mailbox A.

In this scheme, a communication link has the following properties:

• A link is established between a pair of processes only if both members of the


pair have a shared mailbox.

• A link may be associated with more than two processes.

• Between each pair of communicating processes, a number of different links


may exist, with each link corresponding to one mailbox.

 A mailbox may be owned either by a process or by the operating system.


 If the mailbox is owned by a process, then we distinguish between the owner
and the user send messages to the mailbox.
Synchronization

Message passing may be either blocking or nonblocking— also known as


synchronous and asynchronous

Blocking send. The sending process is blocked until the message is received by
the receiving process or by the mailbox.

• Nonblocking send. The sending process sends the message and resumes
operation.

• Blocking receive. The receiver blocks until a message is available.

• Nonblocking receive. The receiver retrieves either a valid message or a null.

Buffering

Whether communication is direct or indirect, messages exchanged by


communicating processes reside in a temporary queue. Basically, such queues
can be implemented in three ways:

Zero capacity: The sender must wait until the receiver picks up the message, as
no waiting messages are allowed.

Bounded capacity: The sender can send messages freely if the queue has space;
otherwise, it must wait.

Unbounded capacity: Messages can be sent indefinitely without the sender ever
needing to wait.

CPU SCHEDULING
 CPU scheduling is the basis of Multi-programmed operating systems. By
switching the CPU among processes, the operating system can make the
computer more productive.
 In a single-processor system, only one process can run at a time. Others must
wait until the CPU is free and can be rescheduled.
 The CPU will sit idle and waiting for a process that needs an I/O operation to
complete. If the I/O operation completes then only the CPU will start executing
the process. A lot of CPU time has been wasted with this procedure.
 The objective of multiprogramming is to have some process running at all times
to maximize CPU utilization.
 When several processes are in main memory, if one processes is waiting for I/O
then the operating system takes the CPU away from that process and gives the
CPU to another process. Hence there will be no wastage of CPU time.
Concepts of CPU Scheduling
1. CPU–I/O Burst Cycle
2. CPU Scheduler
3. Pre-emptive Scheduling
4. Dispatcher
CPU–I/O Burst Cycle
 Process execution consists of a cycle of CPU execution and I/O wait.
 Process execution begins with a CPU burst. That is followed by an I/O
burst. Processes alternate between these two states.
 The final CPU burst ends with a system request to terminate execution.
 Hence the First cycle and Last cycle of execution must be CPU burst.

 Need to add diagram

CPU Scheduler
Whenever 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 or CPU scheduler.

Preemptive Scheduling

CPU-scheduling decisions may take place under the following four cases:

1. When a process switches from the running state to the waiting state.

Example: as the result of an I/O request or an invocation of wait( ) for the


termination of a child process.

2. When a process switches from the running state to the ready state.

Example: when an interrupt occurs

3. When a process switches from the waiting state to the ready state.

Example: at completion of I/O.

4. When a process terminates.

For situations 2 and 4 are considered as Pre-emptive scheduling situations. Mach


OS X, WINDOWS 95 and all subsequent versions of WINDOWS are using
Preemptive scheduling.

Dispatcher

The dispatcher is the module that gives control of the CPU to the process selected
by the short-term scheduler. Dispatcher function involves:

1. Switching context

2. Switching to user mode

3. Jumping to the proper location in the user program to restart that program.
 The dispatcher should be as fast as possible, since it is invoked during every
process switch.
 The time it takes for the dispatcher to stop one process and start another process
running is known as the Dispatch Latency.

SCHEDULING CRITERIA
Different CPU-scheduling algorithms have different properties and the choice of a
particular algorithm may favor one class of processes over another.
Many criteria have been suggested for comparing CPU-scheduling algorithms:
 CPU utilization: CPU must be kept as busy as possible. CPU utilization can
range from 0 to 100 percent. In a real system, it should range from 40 to 90
percent.
 Throughput: The number of processes that are completed per time unit.
 Turn-Around Time: It is the interval from the time of submission of a
process to the time of completion. 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: It is the amount of time that a process spends waiting in the
ready queue.
 Response time: It is the time from the submission of a request until the first
response is produced. Interactive systems use response time as its measure.
Note: It is desirable to maximize CPU utilization and Throughput and to minimize
TurnAround Time, Waiting time and Response time.

CPU SCHEDULING ALGORITHMS


CPU scheduling deals with the problem of deciding which of the processes in the
ready queue is to be allocated the CPU. Different CPU-scheduling algorithms are:

1. First-Come, First-Served Scheduling (FCFS)


2. Shortest-Job-First Scheduling (SJF)
3. Priority Scheduling
4. Round Robin Scheduling
5. Multilevel Queue Scheduling
6. Multilevel Feedback Queue Scheduling
Gantt Chart is a bar chart that is used to illustrates a particular schedule including
the start and finish times of each of the participating processes.
First-Come, First-Served Scheduling (FCFS)
 In FCFS, the process that requests the CPU first is allocated the CPU first.
 FCFS scheduling algorithm is Non-preemptive.
 Once the CPU has been allocated to a process, it keeps the CPU until it
releases the CPU.
 FCFS can be implemented by using FIFO queues.
 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.

Example:1 Consider the following set of processes that arrive at time 0. The
processes are arrived in the order P1, P2, P3, with the length of the CPU burst
given in milliseconds.

Convoy Effect in FCFS


Convoy effect means, when a big process is executing in CPU, all the smaller
processes must have to wait until the big process execution completes. This will
effect the performance of the system.
Example:2 Let us consider same example above but with the processes arrived in
the order P2, P3, P1.
Shortest-Job-First Scheduling (SJF)
 SJF algorithm is defined as “when the CPU is available, it is assigned to the
process that has the smallest next CPU burst”.
 If the next CPU bursts of two processes are the same, FCFS scheduling is used
between two processes.
 SJF is also called as Shortest-Next CPU-Burst algorithm, because scheduling
depends on the length of the next CPU burst of a process, rather than its total
length.
Example 2:
Example:
Priority Scheduling
Round Robin Scheduling
Multilevel Queue Scheduling
 A multi-level 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.
Let us consider an example of a multilevel queue-scheduling algorithm with five
queues:
1. System Processes
2. Interactive Processes
3. Interactive Editing Processes
4. Batch Processes
5. Student Processes
Each queue has absolute priority over lower-priority queues. No process in the
batch queue, for example, could run unless the queues for system processes,
interactive processes, and interactive editing processes were all empty. If an
interactive editing process entered the ready queue while a batch process was
running, the batch process will be pre-empted.
Multilevel Feedback Queue Scheduling
• The multilevel feedback queue scheduling algorithm, in contrast, allows a
process to move between queues. The idea is to separate processes
according to the characteristics of their CPU bursts.
• If a process uses too much CPU time, it will be moved to a lower-priority
queue. This scheme leaves I/O-bound and interactive processes in the
higher-priority queues. In addition, a process that waits too long in a lower-
priority queue may be moved to a higher-priority queue.
• This form of aging prevents starvation.
• For example, consider a multilevel feedback queue scheduler with three
queues, numbered from 0 to 2 (Figure). The scheduler first executes all

• processes in queue 0. Only when queue 0 is empty will it execute processes


in queue 1.
• Similarly, processes in queue 2 will be executed only if queues 0 and 1 are
empty.
• A process that arrives for queue 1 will preempt a process in queue 2. A
process in queue 1 will in turn be preempted by a process arriving for queue
0.
• 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.

Threads:
 A thread is a single sequence stream within a process. Threads are also called
lightweight processes as they possess some of the properties of processes.
 Threads provide a way to improve application performance through
parallelism.
 A thread is the smallest unit of execution within a process.
 A process can have multiple threads that run concurrently, sharing resources
like memory, open files, and global variables. However, each thread has its
own program counter, registers, and stack.
Advantages of Thread
• Threads minimize the context switching time.
• Use of threads provides concurrency within a process.
• Efficient communication.
• It is more economical to create and context switch threads.
• Threads allow utilization of multiprocessor architectures to a greater scale
and efficiency.

Types of Thread

Threads are implemented in following two ways −

 User Level Threads − User managed threads.

 Kernel Level Threads − Operating System managed threads acting on


kernel, an operating system core.

User Level Threads

In this case, the thread management kernel is not aware of the existence of
threads. The thread library contains code for creating and destroying threads,
for passing message and data between threads, for scheduling thread execution
and for saving and restoring thread contexts. The application starts with a single
thread.
Kernel Level Threads

In this case, thread management is done by the Kernel. There is no thread


management code in the application area. Kernel threads are supported directly by
the operating system.

Any application can be programmed to be multithreaded. All of the threads within


an application are supported within a single process.

The Kernel maintains context information for the process as a whole and for
individuals’ threads within the process. Scheduling by the Kernel is done on a
thread basis. The Kernel performs thread creation, scheduling and management in
Kernel space.

Kernel threads are generally slower to create and manage than the user threads.

Multithreading Models

Some operating system provide a combined user level thread and Kernel level
thread facility. Solaris is a good example of this combined approach.

In a combined system, multiple threads within the same application can run in
parallel on multiple processors and a blocking system call need not block the entire
process.
Multithreading models are three types

 Many to many relationship.


 Many to one relationship.
 One to one relationship.

Many to Many Model

The many-to-many model multiplexes any number of user threads onto an equal or
smaller number of kernel threads.

Many to One Model


 Many-to-one model maps many user level threads to one Kernel-level thread.
Thread management is done in user space by the thread library.
 When thread makes a blocking system call, the entire process will be blocked.
Only one thread can access the Kernel at a time, so multiple threads are
unable to run in parallel on multiprocessors.
One to One Model:
There is one-to-one relationship of user-level thread to the kernel-level thread.
This model provides more concurrency than the many-to-one model.
It also allows another thread to run when a thread makes a blocking system call.

Threading Issues
• Some other issue related to the thread is discussed in this section. The issue
includes fork and exec system call, thread cancellation, signal handling, thread
pool etc.

The fork ( ) and exec () system call


• In a multithreaded programming environment, fork and exec system calls are
changed. UNIX operating system uses two versions of fork system calls.

1. Fork call duplicates all threads.


2. In second option, only thread created by fork duplicates.

• Based on the application, system uses fork system calls. Sometimes, duplicating
all the threads are unnecessary. If we call exec immediately after fork then there is
no use of duplicating threads.

• Forking provides a way for an existing process to start a new one, but what about
the case where the new process is not part of the same program as parent process?
This is the case in the shell; when a user starts a command it needs to run in a new
process, but it is unrelated to the shell.

• This is where the exec system call comes into play. An exec will replace the
contents of the currently running process with the information from a program
binary. Thus the process the shell follows when launching a new program is to
firstly fork, creating a new process, and then exec the program binary it is
supposed to run.

Thread Cancellation
• Cancellation allows one thread to terminate another. One reason to cancel a
thread is to save system resources such as CPU time. When your program
determines that the thread's activity is no longer necessary then thread is
terminated.

• Thread cancellation is a task of terminating a thread before it has completed.


• The thread cancellation mechanism allows a thread to terminate the execution of
any other thread in the process in a controlled manner. Each thread maintains its
own cancelability state.
Cancellation may only occur at cancellation points or when the thread is
asynchronously cancelable.

• The target thread can keep cancellation requests pending and can perform
application-specific cleanup when it acts upon the cancellation notice.

A thread's initial cancelability state is enabled. Cancelability state determines


whether a thread can receive a cancellation request. If the cancelability state is
disabled, the thread does not receive any cancellation requests.

• Target thread cancellation occurs in two different situations:


1. Asynchronous cancellation
2. Deferred cancellation

 Asynchronous cancellation: Target thread is immediately terminated. With


the help of another thread, target thread is cancelled. When a thread holds
no locks and has no resources allocated, asynchronous cancellation is a
valid option.
• Deferred cancellation: When a thread has enabled cancellation and is using
deferred cancellation, time can elapse between the time it's asked to cancel
itself exes and the time it's actually terminated.

Signal Handling
Signal is used to notify a process that a particular event has occurred. Signal may
be synchronous or asynchronous. All types of signals are based on the following
pattern:
1. At a specific event, signal is generated.
2. Generated signal is sent to a process/thread.

3. Signal handler is used for handling the delivered signal.

• Synchronous signals: An illegal memory access, division by zero is the example


of synchronous signals. These signals are delivered to the same process which
performed the operation for generating the signal.
• Asynchronous signals: Terminating a process with certain keystrokes are signals
that are generated by an event external to the running process.
• Synchronous signal is sent to same process whether as asynchronous signal is
sent to another process.
Signals may be handled in different ways:
1. Some signals may be ignored. For example changing the windows size.
2. Other signals may be handled by terminating the program. For example illegal
access of memory.
Delivery of signals in multithreaded programs is more complex than single
thread.
• Following are the condition where/ how should the signals be delivered to
threads/process:

a. Thread applies the signal are received the signal.


b. Every thread in the process received the signal.
c. Deliver the signal to limited threads in the process
d. All the signals are received to particular thread for the process.

The method for delivering a signal depends on the type of signals generated.
1. Synchronous signals is sent to the thread which causes the signal.
2. All the thread received asynchronous signals.

Thread Pool
A thread pool offers a solution to both the problem of thread life-cycle overhead
and the problem of resource thrashing. By reusing threads for multiple tasks, the
thread-creation overhead is spread over many tasks.

• Thread pools group CPU resources, and contain threads. used to execute tasks
associated with that thread pool. Threads host engines that execute user tasks, run
specific jobs such as signal handling and process requests from a work queue.
• Multithreaded server has potential problems and these problems are solved by
using thread pool. Problems with multithreaded server :

1. Time for creating thread


2. Time for discarding thread
3. Excess use of system resources.
Advantages of thread pools :
1. Servicing a request with an existing thread is usually faster than waiting to
create a thread.
2. Thread pool size is fixed.
Deadlock
 Deadlock is a situation where a set of processes are blocked because each
process is holding a resource and waiting for another resource acquired by
some other process.
 For example, in the below diagram, Process 1 is holding Resource 1 and
waiting for resource 2 which is acquired by process 2, and process 2 is waiting
for resource 1.

Deadlock can arise if following four necessary conditions hold simultaneously.


1. Mutual Exclusion: One or more than one resource are non-sharable means
Only one process can use at a time.
2. Hold and Wait: A process is holding at least one resource and waiting for
another resources.
3. No Pre-emption: A resource cannot be taken from a process unless the process
releases the resource means the process which once scheduled will be executed till
the completion and no other process can be scheduled by the scheduler
meanwhile.
4. Circular Wait: A set of processes are waiting for each other in circular form
meansAll the processes must be waiting for the resources in a cyclic manner so
that the last process is waiting for the resource which is being held by the first
process.

Deadlock Handling
The various strategies for handling deadlock are1. Deadlock Prevention
2. Deadlock Avoidance
3. Deadlock Detection and Recovery
4. Deadlock Ignorance
1. Deadlock Prevention
Deadlocks can be prevented by preventing at least one of the four required
conditions:
Mutual Exclusion
Shared resources such as read-only files do not lead to deadlocks.
Unfortunately, some resources, such as printers and tape drives, require exclusive
access by a single process.
Hold and Wait
To prevent this condition processes must be prevented from holding one or more
resources while simultaneously waiting for one or more others.
No Preemption
Preemption of process resource allocations can prevent this condition of
deadlocks, when it is possible.
Circular Wait
One way to avoid circular wait is to number all resources, and to require that
processes request resources only in strictly increasing (or decreasing ) order.

2. Deadlock Avoidance
 In deadlock avoidance, the operating system checks whether the system is in
safe state or in unsafe state at every step which the operating system performs.
 The process continues until the system is in safe state.
 Once the system moves to unsafe state, the OS has to backtrack one step.
 In simple words, The OS reviews each allocation so that the allocation doesn't
cause the deadlock in the system.
3. Deadlock detection and recovery
This strategy involves waiting until a deadlock occurs.
After deadlock occurs, the system state is recovered.
The main challenge with this approach is detecting the deadlock.
4. Deadlock Ignorance
 This strategy involves ignoring the concept of deadlock and assuming as if it
does not exist.
 This strategy helps to avoid the extra overhead of handling deadlock.
 Windows and Linux use this strategy and it is the most widely used method.

You might also like