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

Unit 2 OS 2023

The document discusses Inter Process Communication (IPC) in operating systems, defining independent and co-operating processes, and outlining the reasons for IPC such as information sharing and computation speedup. It explains two IPC models: message passing and shared memory, detailing their operations and synchronization methods. Additionally, it covers process concepts, states, control blocks, scheduling, and the creation and termination of processes.

Uploaded by

nctitacademic
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Unit 2 OS 2023

The document discusses Inter Process Communication (IPC) in operating systems, defining independent and co-operating processes, and outlining the reasons for IPC such as information sharing and computation speedup. It explains two IPC models: message passing and shared memory, detailing their operations and synchronization methods. Additionally, it covers process concepts, states, control blocks, scheduling, and the creation and termination of processes.

Uploaded by

nctitacademic
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 61

Unit 2

INTER PROCESS COMMUNICATION (IPC):


Processes executing the concurrent in the operating system may be
1. Independent Process:
If a process is not affect by any other processes in the system then it is called as
Independent process.
2. Co-operating Process:
When a process is affected by other processes in a system, it is called as co-operating
process. So any process which shares the resources with other processes is known as co-
operating process.

Inter Process Communication (IPC) is a mechanism for process to communicate and to


synchronize their actions.

The Reasons for providing an IPC are


 Information sharing:
Several users may be interested in the same piece of information, we must give an
environment permit the concurrent access.
 Computation speedup:
To run a particular task, we must break it into subtasks, each of which will be executing
in parallel with the others.
 Modularity:
The system is spitted into more number of individual processes or threads.
 Convenience:
Each and individual user may have many tasks at the same time.
For instance, a user may be editing, printing, and compiling in parallel.

Models of Inter Process Communication:


Two models
1. Message passing System:
2. Shared Memory

Page 1 of 61
1. Message passing System:
The function of a message system is to processes communicated with each other without
resorting to shared variables.
IPC provides two operations:
 Send (message):
Fixed sized massage  programming task is difficult
Variable sized massage  programming task is easier
 Receive (message):
It receives a message form a given source.
Method of implementing the send() / receive() operation
 Direct or indirect communication
 Synchronous or Asynchronous communication
 Automatic or Explicit buffering

2. Shared Memory
IPC using shared memory requires communicating processes to establish a region of shared
memory
Shared memory resides in the address space of the process creating the shared memory
segment
1. Naming:
Two ways to processes that want to communicate.
The direct or indirect communication can be used.

 Direct Communication:
In direct communication, each process that wants to communicate must:
 send (P, message) – send a message to process P
 receive(Q, message) – receive a message from process Q
Communication Link
Process P Process Q

Send (P, Message) Receive (Q, Message)

Page 2 of 61
Properties of communication link:
1. Links are established automatically
2. A link is connected with exactly one pair of communicating processes
3. Between each pair there exists exactly one link
4. The link may be unidirectional, but is usually bi-directional

 Indirect Communication:
Messages are directed and received from mailboxes
 Messages are not sent directly from sender to receiver.
 Each mailbox has a unique identification (ID)
 Processes can communicate only if they share a mailbox

Process P Mailbox B Process Q

Send (B, Messages) Receive (B, Messages)

Send and Receive Primitives are defined as:


 send(B, message) – send a message to mailbox B
 receive(B, message) – receive a message from mailbox B

Properties of communication link


1. Link established only if processes share a common mailbox
2. A link may be connected with many processes
3. Each pair of processes may share several communication links
4. Link may be unidirectional or bi-directional

2. Synchronization:
Process Synchronization is the task of organizing the access of several concurrent processes
to shared resources without causing and conflicts

Page 3 of 61
 Message passing may be either blocking or non-blocking.
 Blocking is considered synchronous
1. Blocking send: The sending process is blocked by the process until the message is
received
2. Blocking receive: The receiver blocks until a message is available
 Non-blocking is considered asynchronous
1. Non-blocking send: The sending process sends the message and continues
operations.
2. Non-blocking receive: The receiver receives a valid message or null

3. Buffering:
Buffering is the process of temporarily storing the data to allow for small variation in device
speeds.
 Buffering implemented in three ways:
1. Zero capacity:
The maximum length of queue is 0.
Sender must wait for receiver
No space to store the message in-between
2. Bounded capacity:
The queue has a finite length of n messages.
Sender must wait if queue is full
Sender can send the message if there is a space
3. Unbounded capacity:
Queue has potentially infinite length
Sender never waits
A sender can store any number of messages
___________________________________________________________________________

Page 4 of 61
PROCESSES:

 A process is a program in execution. The Process must be executed in a sequential


fashion.
 A process is more than a program code , because the previous has a program counter,
stack, data section and so on (i.e., the runtime packages).
 Moreover, multiple processes may be connected with one program (e.g., run the same
programs, a web browser, twice)
 Program is a Passive Entity and Process is a Active Entity

 A process includes:
 program counter
 stack
 data section

Program Process
It consists of a set of instructions in a It is a sequence of instruction execution
programming language
It is a static object that exists in a file It is a dynamic object i.e., program in
execution
It is a passive entity It is a active entity
The time span of program is unlimited The time span of process is limited

Page 5 of 61
Process Concept:
 Process is an instance of a program in execution
 It is the unit of work in a modern time sharing operating system.
 Process is more than the program code. It includes current activity as represented by the
value of the program counter and the contents of the processor register.
 And also that a process execution must program run in sequential manner.

Process State:
 As a process executes, it changes 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:

 New: The process is being created.


 Ready: The process is waiting to be assigned to a process
 Running: Instruction are being s executed
 Waiting: The process is waiting for some event to occur
 Terminated: The process has finished execution.

Figure: Diagram of process state.

Page 6 of 61
Process Control Block:
Each process is represented in the operating system by a process control block (PCB) also
called as task control block.
It is a data structure that holds information to manage a particular process.
It contains many pieces of information connected with a specific process.

Figure: Process control block


 Process state: The state may be new, ready, running, and waiting, stopped and so on.
 Program counter: The counter indicates the address of the next instruction to be
executed.
 CPU registers: The registers vary in number and type, depending upon the computer.
The registers are accumulators, index register, stack pointers, and general purpose
registers.
 CPU scheduling information: This information includes a process priority, pointers to
scheduling queues etc
 Memory-Management information: This information includes 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 etc.

Page 7 of 61
PROCESS SCHEDULING:

 It is process that selects from among the processor in memory that reread to execute and
allocates the CPU to one of them
 Multiprogramming:
Process to run at all times. so as to maximize CPU utilization
 Time Sharing:
Switch the CPU memory processes. So that users interact with each program which It is
running.

Scheduling Queues:
 It consists of 3 queue

1. Job Queue
It is queue consists of all processed that enter the system

2. Ready Queue:
It is the queue that contains the processes residing in main memory, ready and wasiting to
execute which are stored as a linked list
This header contains pointer to the first and final PCBs In the list

PCB1 PCB2 PCB3 PCBn

Head
Tail

3. Device Queue:
The set of processes waiting for an I/O device is called a device queue
Process migration between the various queues.
Queue structures: Typically Linked List, Circular List etc

Page 8 of 61
Queuing Diagram:
A common representation of process scheduling is a queuing diagram:
 Two types are queues are present:
1. Ready queue and
2. Set of device queues.
 Rectangular box represents a queue
 Circle represents the resources that work the queues,
 Arrows indicate the flow of processes in the system.

 A new process is put in the ready queue. It waits in the ready queue until it is selected for
execution. Once the process is assigned to the CPU and is executing, one of the several
events could occur:
 The process could provide 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.

Schedulers:

Page 9 of 61
 Scheduler is an activity that will be done by the OS component called the scheduler
Types of schedulers:
1. Long-term schedulers or job schedulers
2. Medium-term scheduler
3. Short-term scheduler or CPU scheduler
1. Long-term schedulers or job schedulers:
Selects which processes should be brought into ready queue?
 Determine which program are admitted to the system for processing
 Control the Degree of multi programming
 Attempt to keep a max of processor bound and I/O bound processes.
 It is invoked very infrequently (seconds, minutes)
There may be minutes between the creations of new processes in the system. The long-term
schedulers are needed to be raised only when the process leaves the system.
2. Short-term scheduler or CPU scheduler:
Selects which process should be execute next and allocates the CPU?
 Determine which process is going to execute next.
 The short term scheduler is also known as dispatcher
 It is invoked on a event that may load to choose another process for execution
o Clock Interrupts, I/O Interrupts
o OS cells and traps
o Signals
 The short term scheduler is very frequently (milliseconds)
3. Medium-term scheduler:
The medium-term scheduler removes processes from memory, and thus reduces the degree
of multiprogramming.
At some later time, the process can be reintroduced into memory and its execution can be
continued where it left off. This scheme is called swapping.
The process is swapped out, and is later swapped in, by the medium-term scheduler.
I/O-bound process: Spends more time doing I/O than computations, many short CPU bursts
CPU-bound process: Spends more time doing computations; few very long CPU bursts.
Context Switch:

Page 10 of 61
 Switching the CPU to another process requires saving the same of the old process and
loading the saved state for the new process. This task is known as a context switching
 Context-switch time is overloaded
o System does no useful work while switching
o Can become a bottle neck
 Time dependent on context switch is dependent on hardware support
 The speed ranges from 1 to 1000 milliseconds.
__________________________________________________________________________

OPERATIONS ON PROCESSES:
The OS should be able to create and delete process dynamically
1. Process Creation
In general-purpose systems, some way is needed to create processes as needed during operation.
There are four principal events led to processes creation.
1. System initialization.
2. Execution of a process Creation System calls by a running process.
3. A user request to create a new process.
4. Initialization of a batch job.
 Foreground processes interact with users. Background processes that stay in background
sleeping but suddenly springing to life to handle activity such as email, webpage, printing,
and so on. Background processes are called daemons. This call creates an exact clone of
the calling process.

 A process may create a new process by some create process such as 'fork'. It chooses to
does so, creating process is called parent process and the created one is called the child
processes. Only one parent is needed to create a child process.

 Note that unlike plants and animals that use sexual representation, a process has only one
parent. This creation of process (processes) yields a hierarchical structure of processes like
one in the figure.

Page 11 of 61
 Notice that each child has only one parent but each parent may have many children. After
the fork, the two processes, the parent and the child, have the same memory image, the
same environment strings and the same open files. After a process is created, both the
parent and child have their own distinct address space. If either process changes a word in
its address space, the change is not visible to the other process.

 Following are some reasons for creation of a process


1. User logs on.
2. User starts a program.
3. Operating systems creates process to provide service, e.g., to manage printer.
4. Some program starts another process, e.g., Netscape calls xv to display a picture.

2. Process Termination
 A process terminates when it finishes executing its last statement. Its resources are returned
to the system, it is purged from any system lists or tables, and its process control block
(PCB) is erased i.e., the PCB's memory space is returned to a free memory pool.
 Reason for Process Termination
The new process terminates the existing process, usually due to following reasons:
1. Normal Exist
Most processes terminates because they have done their job. This call is exist in UNIX.
2. Error Exist
When process discovers a fatal error. For example, a user tries to compile a program that
does not exist.
3. Fatal Error
An error caused by process due to a bug in program for example, executing an illegal
instruction, referring non-existing memory or dividing by zero.
4. Killed by another Process
A process executes a system call telling the Operating Systems to terminate some other
process. In UNIX, this call is killing. In some systems when a process kills all processes
it created are killed as well (UNIX does not work this way).
CPU SCHEDULING:

Page 12 of 61
 The assignment of physical processors to processes allows processors to accomplish work.
 The problem of determining when processors should be assigned and to which processes is
called processor scheduling or CPU scheduling.
 When more than one process is runnable, the operating system must decide which one first.
 The part of the operating system concerned with this decision is called the scheduler, and
algorithm it uses is called the scheduling algorithm.

Goals of scheduling (objectives):


 Many objectives must be considered in the design of a scheduling discipline.
 In particular, a scheduler should consider fairness, efficiency, response time, turnaround
time, throughput, etc.,
 Some of these goals depend on the system one is using for example batch system, interactive
system or real-time system, etc. but there are also some goals that are desirable in all
systems.

CPU Scheduling Criteria:


Some commonly used criteria are given below:
 Fairness: Avoid the process from the starvation. All the process must be given equal
opportunity to execute.
 Waiting time: the average period of time a process spends waiting. Waiting time may be
expressed on turnaround time less the actual execution time.
 Efficiency: Scheduler should keep the system (or in particular CPU) busy cent percent of
the time when possible. If the CPU and all the Input/Output devices can be kept running all
the time, more work gets done per second than if some components are idle.
 Response Time: A scheduler should minimize the response time for interactive user.
 Turnaround: A scheduler should minimize the time batch users must wait for an output.
 Throughput: It refers to the amount of work completed in a unit of time. The number of
processes the system can execute in a period of the time. The higher the number, the more
work is done by the system.

Page 13 of 61
Preemptive vs. Non-preemptive Scheduling:
The Scheduling algorithms can be divided into two categories with respect to how they deal with
clock interrupts.

1. Non-preemptive Scheduling
A scheduling discipline is non-preemptive if, once a process has been given the CPU, the
CPU cannot be taken away from that process.
Following are some characteristics of non-preemptive scheduling
In non-preemptive system, short jobs are made to wait by longer jobs but the overall
treatment of all processes is fair.
In non-preemptive system, response times are more predictable because incoming high
priority jobs cannot displace waiting jobs.
In non-preemptive scheduling, a scheduler executes jobs in the following two situations.
a. When a process switches from running state to the waiting state.
b. When a process terminates.

2. Preemptive Scheduling
A scheduling discipline is preemptive if, once a process has been given the CPU can
taken away. The strategy of allowing processes that are logically runable to be
temporarily suspended is called Preemptive Scheduling and it is contrast to the "run to
completion" method.

Dispatcher:
Dispatcher module gives control of the CPU to the process selected by the short term scheduler,
this involves
 Switching context
 Switching to user mode
 Jumping to the proper location in the user program to restart that program

Dispatcher:
Time it takes for the dispatcher to step one process and start another running

Page 14 of 61
SCHEDULING ALGORITHM:
CPU Scheduling deals with the problem of deciding which of the processes in the ready queue is
to be allocated the CPU. Following are some scheduling algorithms
1. FCFS Scheduling.
2. Round Robin Scheduling.
3. SJF Scheduling.
4. Priority Scheduling.
5. Multilevel Queue Scheduling.
6. Multilevel Feedback Queue Scheduling.

First-Come-First-Served (FCFS) Scheduling:


 It is also called as First-In-First-Out (FIFO) or Run-to-Completion or Run-Until-
Done
 Perhaps, First-Come-First-Served algorithm is the simplest scheduling algorithm is the
simplest scheduling algorithm.
 Processes are dispatched according to their arrival time on the ready queue.
 Being a non-preemptive discipline, once a process has a CPU, it runs to completion.
 The FCFS scheduling is fair in the formal sense or human sense of fairness but it is unfair
in the sense that long jobs make short jobs wait and unimportant jobs make important
jobs wait.

 FCFS is more predictable than most of other schemes since it offers time.
 FCFS scheme is not useful in scheduling interactive users because it cannot guarantee
good response time
 The code for FCFS scheduling is simple to write and understand.

 One of the major drawbacks of this scheme is that the average time is often quite long.
 The First-Come-First-Served algorithm is rarely used as a master scheme in modern
operating systems but it is often embedded within other schemes.

Page 15 of 61
Round Robin Scheduling;
 One of the oldest, simplest, fairest and most widely used algorithms is round robin (RR).
 In the round robin scheduling, processes are dispatched in a FIFO manner but are given a
limited amount of CPU time called a time-slice or a quantum.
 If a process does not complete before its CPU-time expires, the CPU is preempted and
given to the next process waiting in a queue.
 The preempted process is then placed at the back of the ready list.
 Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective
in time-sharing environments in which the system needs to guarantee reasonable response
times for interactive users.
 The only interesting issue with round robin scheme is the length of the quantum. Setting
the quantum too short causes too many context switches and lower the CPU efficiency.
On the other hand, setting the quantum too long may cause poor response time and
approximates FCFS.
 In any event, the average waiting time under round robin scheduling is often quite long.

Shortest-Job-First (SJF) Scheduling:


 It is also called as Shortest-Process-Next (SPN).
 Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process)
with the smallest estimated run-time-to-completion is run next.
 In other words, when CPU is available, it is assigned to the process that has smallest next
CPU burst.
 The SJF scheduling is especially appropriate for batch jobs for which the run times are
known in advance. Since the SJF scheduling algorithm gives the minimum average time
for a given set of processes, it is probably optimal.
 The SJF algorithm favors short jobs (or processors) at the expense of longer ones.
 The obvious problem with SJF scheme is that it requires precise knowledge of how long
a job or process will run, and this information is not usually available.
 The best SJF algorithm can do is to rely on user estimates of run times.
 In the production environment where the same jobs run regularly, it may be possible to
provide reasonable estimate of run time, based on the past performance of the process.

Page 16 of 61
 But in the development environment users rarely know how their program will execute.
 Like FCFS, SJF is non-preemptive therefore; it is not useful in timesharing environment
in which reasonable response time must be guaranteed.

Shortest-Remaining-Time (SRT) Scheduling:


 The SRT is the preemptive counterpart of SJF and useful in time-sharing environment.
 In SRT scheduling, the process with the smallest estimated run-time to completion is run
next, including new arrivals.
 In SJF scheme, once a job begins executing, it run to completion.
 In SJF scheme, a running process may be preempted by a new arrival process with
shortest estimated run-time.
 The algorithm SRT has higher overhead than its counterpart SJF.
 The SRT must keep track of the elapsed time of the running process and must handle
occasional preemptions.
 In this scheme, arrival of small processes will run almost immediately. However, longer
jobs have even longer mean waiting time.

Priority Scheduling:
 The basic idea is straightforward: each process is assigned a priority, and priority is
allowed to run. Equal-Priority processes are scheduled in FCFS order. The shortest-Job-
First (SJF) algorithm is a special case of general priority scheduling algorithm.
 An SJF algorithm is simply a priority algorithm where the priority is the inverse of the
(predicted) next CPU burst. That is, the longer the CPU burst, the lower the priority and
vice versa.
 Priority can be defined either internally or externally. Internally defined priorities use
some measurable quantities or qualities to compute priority of a process.
 Priority scheduling can be either preemptive or non-preemptive.
 When process arrives at the ready queue, its priority is compared with the priority of the
currently running process.
 A preemptive priority-scheduling algorithm will preempt the CPU if the priority of the
newly arrived process is higher than the priority of the currently running process.

Page 17 of 61
 A non-preemptive priority scheduling algorithm will simply put the new process at the
head of the ready queue.
 A major problem with priority-scheduling algorithms is indefinite blocking or
starvation.
 A process that is ready to run but lacking the CPU is considered blocked – waiting for the
CPU.
 A priority-scheduling algorithm can leave some low-priority processes waiting
indefinitely for the CPU.

 Generally one of the two things will happen.


 Either the process will eventually be run, or the computer system will eventually crash
and lose all unfinished low-priority processes.
 A solution to the problem of indefinite blockage of low-priority processes is aging. Aging
is a technique of gradually increasing the priority of processes that wait in the system for
a long time.

Multilevel Queue Scheduling:


 A multilevel queue scheduling algorithm partitions the ready queue into several separate
queues.
 The processes are permanently assigned to one queue, 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 can be used as foreground and background queues.
1. Foreground queues:
this is for interactive processes, with highest priority. This can be scheduled using RR
scheduling algorithm.
2. Background queues: This is for batch processes, with lowest priority and uses FCFS
scheduling algorithm for scheduling.

Page 18 of 61
System processes

Interactive processes

Interactive editing processes

Batch processes

Student processes

 Let us look at an example of a multilevel queue scheduling algorithm with five queues:
 System processes
 Interactive processes
 Interactive editing processes
 Batch processes
 Student processes
 We can execute the high priority queue first.
 It means that, no process in 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 would be preempted.

Advantages:
1. The processes are permentally assigned to queue when they enter the systems
2. Since processes do not change their fore or back ground nature.
3. So there is low scheduling overhead.

Disadvantages:
1. Starvation of process for CPU occurs here all the process never allowed to change their
queue inflexibility and if one or the other higher priority queues never become empty.

Page 19 of 61
Multilevel Feedback Queue Scheduling:
 In a multilevel queue scheduling algorithm, processes are permanently assigned to a
queue on entry to the system.
 Processes do not move between queues.
 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.
 Multilevel feedback queue scheduling, allows a process to move between the queues.
 The idea is to separate processes with different CPU burst characteristics.
1. If a process uses too much CPU time, it will be moved to a lower priority queue.
2. If a process waits too long in a lower priority queue may be moved to a higher-priority
queue. This form of aging prevents starvation.

Time Quantum (q) = 8 Highest Priority

Time Quantum (q) = 16


Aging

FCFS Algorithm Lowest Priority

 For example consider a multilevel feedback queue scheduler with three queues,
numbered from 0 to 2.
 The scheduler first executes all processes in queue 0.
 Only when queue 0 is empty will it execute processe4s 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 that arrives for queue 0 will, in turn preempt a process in queue 1.
 A process entering the ready queue is put in queue 0.
 A process in queue is given a time quantum of 8 milliseconds.

Page 20 of 61
 If it does not finish within this time it is moved to the tail of the queue. 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.
 Process in queue 2 is run on an FCFS basis, only when queues 0 and 1 are empty.
Multilevel feedback queue scheduler is defined by the following parameters:
1. The number of queues
2. The scheduling algorithm for each queue
3. The method used to determine when to upgrade a process to a higher priority queue
4. The method used to determine when to demote a process to a lower priority queue
5. The method used to determine which queue a process will enter when the process
needs service.
_____________________________________________________________________________

Page 21 of 61
THREADS:
 A thread sometimes called light weight process (LWP).
 LWP is a basic unit of CPU utilization; it comprises a thread ID, a program counter, a
register set, and a stack.
 A thread is the entity within a process that can be scheduled for execution
 Thread is a single sequence flow of stream within a program.

 A Thread some similarities to process:


1. A thread also has the life cycle
2. Sharing the processor.
3. Each thread has its own stack and program counter
4. Thread executes sequencing within a process.
 Dissimilarities:
1. Thread are not independent of one another
2. Thread requires lesser creation time compared to process
3. Switching between thread is faster
4. Thread improve communication efficiency
Benefits:
1. Responsiveness
Increasing the responsiveness to user
Time consuming operation is performed in a separate thread; the approach remains
responsive to the user
2. Resource Sharing
Process can only share resources through techniques such as shared memory and message
passing
3. Economy
It is more economical to create and context switch threads
More time consuming to create and manage processes than thread
4. Scalability
Threads may be running in parallel on different processing cores
A single threaded process can run on only one processor regardless how many are available

Page 22 of 61
Thread Implementation;
1. User level threads:
 These types of threads are loaded in the user space only. So the kernel does not know
anything about them.
 When threads are managed in the user space, each process must have its own private
thread table.
 This private table consists of the information of the program counter, stack pointer,
registers etc. and is managed by the run time system.

Process

Thread User space


Thread table

Kernel Space

2. Kernel Level Thread:


 In this method, the kernel knows about and manages the threads. No runtime system is
needed in this case.
 Instead of thread table in each process, the kernel has a thread table that keeps track of all
threads in the system.
 In addition, the kernel also maintains the traditional process table to keep track of
processes. Operating Systems kernel provides system call to create and manage threads.
Process

Thread User space


Process Table

Kernel Space
Thread Table

Page 23 of 61
Multi- Threading Models:
A process is divided into smaller tasks and each task is called as a thread. The use of multiple
threads in as single program, all running at the same time and performing different tasks is called
multithreading.

Multithreading Models:
 Many-to-One
 One-to-One
 Many-to-Many

Many-to-One:
 Many user-level threads mapped to single kernel thread.
 The thread management is done in user space.
 So it is efficient.
 But the whole process will block when a thread increases a blocking system call.
 Here multiple threads are unable to run in parallel on multiprocessors.

One-to-One:
 Each user-level thread maps to kernel thread
 When a thread creates a blocking system call, it gives more concurrency.
 This permits multiple threads to run in parallel on the multicomputer.
Drawbacks:
1. Creating a user thread needs the creating of corresponding kernel thread.

Page 24 of 61
Many-to-Many Model:
 Allows many user level threads to be mapped to many kernel threads
 Allows the operating system to create a sufficient number of kernel threads
Disadvantages:
1. Many user threads can be created by the developers.
2. The corresponding kernel can run in parallel on a multiprocessor.
3. When a thread does a blocking system call, the kernel can schedule another thread for
execution

Process Thread

Page 25 of 61
Processes cannot share the same memory Threads can share memory and files
Process creation is time consuming Thread creation is not time consuming
Process execution is very slow Thread execution is very fast
Process is loosely coupled i.e., lesser Thread are tightly coupled i.e., more resource
resource sharing sharing
Communication between the process is Communication between the thread is easy
difficult and efficient
It is a heavy weight process as it requires It is a light weight process as it requires less
more resources resources

Page 26 of 61
THREADING ISSUES:
Some of the issues to consider in designing multithreaded programs

1. The fork and exec system calls:


 The fork system calls is used to establish a separate, duplicate process.
There are two versions of fork in some UNIX systems.
1. One version is used to duplicate all threads.
2. Another version is used to duplicates only thread which calls the fork ().
Exec (): When this is called, the whole process is replaced by the program given in the
parameter to exec.

2. Thread Cancellation
 The process of terminating a thread before it has ended is said to be thread cancellation.
 A thread which is to be cancelled is known as target thread.
Two general approaches:
1. Asynchronous cancellation
One thread immediately terminates the target thread immediately
2. Deferred cancellation
The target thread to periodically check it should terminate, allowing it an opportunity to
terminated itself in an orderly fashion.

Signal Handling:
 Signals notify a process that a particular event has occurred.
 A signal may be received either synchronously or asynchronously, depending upon the
source and reason for the event being signaled.
 A signal handler is used to process signals
 Signal is generated by particular event
 The signal is delivered to a process
 Once delivered the signal must be handled

 A signal may be handled by one of two possible handlers

Page 27 of 61
1. A default signal handlers
Kernel runs when handling that signal
2. A user defined signal handlers
The default action can be overridden by a user default signal handler that is called
to handle the signal
 When a multithread process receives a signal, to what thread should that signal be
delivered?
1. Deliver the signal to the thread to which the signal applies
2. Deliver the signal to every thread in the process
3. Deliver the signal to certain threads in the process
4. Assign a specific thread to receive all signals for the process
Thread-Specific Data/Thread-Safe:
 Data that a thread needs for its own operation are thread-specific.
 Poor support for thread-specific data could cause problem.
 For example, while threads have their own stacks, they share the heap.
 What if two malloc() or new are executed at the same time requesting for memory from
the heap? Or, two printf or cout is run simultaneously?
 If a library can be used by multiple threads properly, it is a thread-safe one.
Thread Pool:
 While we know that managing threads are more efficient than managing processes,
creating and terminating threads are still not free.
 After a process is created, one can immediately create a number of threads and have them
waiting.
 When a new task occurs, one can wake up one of the waiting threads and assign it the
work.
 After this thread completes the task, it goes back to wait.
 In this way, we save the number of thread creation and termination.
 These threads are said in a thread pool.

Page 28 of 61
PROCESS SYNCHRONIZATION:
 As we know that the process of changing context from an executing program to an interrupt
handler is called as context switching
 On the other hand, the method of changing context from an executing program to another
process is called as process switching

Tasking may be implicit or explicit


1. Implicit tasking means processes are defined b y the system
2. Explicit tasking means processes are defined by the programmer in a single application
Suppose C, L, S and R are four instruction each requiring 3, 4, 2 and 3 units of time,
respectively.

Then, there are two ways of executing them


1. Sequential Processing
While (true)
{ C; L;
R; S;
} So, one round takes 12 units of time
2. Multitasking
C1

L1 S1

C2 R1

L1 S2

C3 R2

To complete two rounds, it takes 15 units o time whereas in the sequential case it takes 24 units
of time. So there is a performance gain. However, process synchronization is needed here.

Page 29 of 61
Racing problem;
The situation where, two or more processes are reading or writing some shared data and
the result depends on who runs precisely when is called as a race condition.
To avoid race condition

Critical Section Problem:


A system consists on n processes {P0, P1, and Pn-1}. Each process has a division of code called
a critical section.

Critical Section
Each process has a code segment called critical section.
In which the process may be changing common variables, updating a table, writing a file and so
on.
When one process is execute in its critical section. When one process is executing in its critical
section, no other process is allowed to execute in its critical section.

Types of Section:
1. Entry Section.
The section of code implementing the request permission to enter the critical section.
2. Exit Section:
The critical section may be followed by an exit section.
The remaining code is the remainder section.
Fig. General structure of a typical process Pi.
Do {
Entry section
Critical section
Exit Section
Remainder section
} while (1);

Page 30 of 61
A solution to the critical section problem must satisfy the following requirements:
 Mutual Exclusion:
If process Pi is executing in its critical section, then no other processes can be executing
in their critical sections.
 Progress:
If no process is executing in its critical section and some process wish to enter their
critical sections, then only those processes that are not executing in their remainder
section can enter the critical section.
 Bounded waiting:
There exists a bound on the number of times that other processes are allowed to enter
their critical sections after a process has made a request to its critical section.

Two Process Solutions:


1. Algorithm 1:
Here the algorithms are used to only two processes at a time. The processes are represented as
P0, P1.
A common integer variable turns which is assigned initially to 0 or 1.
This variable has to be shared by the process.
When the turn == i, then the process Pi is permitted to run in its critical section.
do {
While (turn! = i);
Critical section
Turn = j;
Remainder section
} while (1);
This gives assurance for only one process at a time can be in its critical section.
But it does not satisfy the progress requirement.

2. Algorithm 2
The remedy to algorithm 1 is we can replace the variable turn with the following array:
Boolean flag[2];

Page 31 of 61
The elements of the array are initialized to false.
When the flag[i] is true, this value indicates that Pi is ready to enter the critical section.
The structure of the process P1 is given as
Do {
flag[i] = true;
while (flag[j]);
Critical Section
flag[i] = false;
Remainder section
{ while (1);
Here, first the process Pi first sets flag[i] = true
This represents that it is ready to enter its critical section.
Then Pi checks to verify that process Pjdoes not enter into its critical section.
Suppose if the Pj is ready, to enter its critical section then Pi has to wait until the Pjdoes not like
to be in the critical section.
So, now the Pi isenters its critical section.
The Pi is set to flag[i] = false when existing the critical section.
In this mutual exclusion is satisfied.
But progress is not satisfied.

3. Algorithm 3:
By combining the key ideas of algorithm 1 and 2 we obtain the correct solution to the critical
section problem where all the three requirements areseen.
The processes share the two variables:
Boolean flag[2];
int turn;
At the starting stage, the value of flag [0] = flag [1].
The process structure is given as
Do {
Flag[i] = true;
Turn = j;

Page 32 of 61
While (flag[j] && turn == j);
Critical section
Flag[i] = false;
Remainder section
} while (1);
Here the process P1 first assigns flag [i] =true.
Then it sets the turn = j by asserting that if any other process wants to enter the critical section, it
is possible to prepare.
So that the other process wishes to enter the critical section can enter. If both process try to enter
at the same time then value of turn decides which of the two process to enter.

Multiple Process Solution:


Bakery algorithm is used in multiple process solution.
It solves the problem of critical section for n processes.
Each process requesting entry to critical section is given a numbered token such that the number
on the token is larger than the maximum number issued earlier.
This algorithm was developed for a distributed environment.
Do {
choosing[i] :=true;
number[i] := max(number[0], number[1],…,number[n-1]) +1;
choosing[i] :=false;
for(j= 0; j <n; j++)
{
While (choosing[j]);
While ((number[j]! =0) &&number [j , j]<[i , i]));
}critical section
number[i]:=0;
remainder section
} while (1);

Page 33 of 61
SYNCHRONIZATION HARDWARE:
 Many systems provide hardware support for critical section code
 Uni-processors environment, the critical section problem is simple solved. Here the current
sequence of instructions would be allowed to execute in order without preemption.
 Multiprocessor environment. Other instructions would not be run. So the unexpected
change does not performed to the shared variable. But it not suitable for multiprocessor
environment. Disabling interrupts on a multiprocessor can be time consuming as the
message is passed to all the processors.

 This message passing


Delays entry into every critical section
Decreases the system efficiency
 Modern machines provide special atomic hardware instructions
1. Either test memory word and set value
2. Swap contents of two memory words

1. Test-and-set instruction
This instruction is executed automatically.
The definition of TestAndSet instruction is given as
boolean TestAndSet (boolean *target)
{
booleanrv = *target;
*target = TRUE;
returnrv:
}
When two Test And Set instructions are executed at the same time, they will be executed
sequentially in some arbitrary order.
The mutual exclusion is implemented by declaring a Boolean variable ‘Lock’. This is declared as
false when the machine prefers Test and Set instructions.

Page 34 of 61
The structure of process Pi is given as
do
{
whileTest-and-Set (lock)do no-op; no-op is no operation
critical section
lock:= false;
remainder section
} while (1);
2. Swap instruction:
The Swap instruction is used on the contents of the two words.
The mutual exclusion is provided when the system supports swap instruction.
This swap instruction is defined as
void Swap (boolean *a, boolean *b)
{
boolean temp = *a;
*a = *b;
*b = temp:
}
The mutual exclusion is obtained by declaring a global Boolean variable ‘Lock’.
This ‘Lock’ is assigned to false.
Also every process holds a local Boolean variable key.
The structure of process Pi given as
do{
key = TRUE;
while ( key == TRUE)
Swap (&lock, &key );
critical section
lock = FALSE;
remainder section
}while (1);
_____________________________________________________________________________

Page 35 of 61
SEMAPHORES:
Overcome the difficulty of critical section problem.
It is a synchronization tool that does not require busy waiting.
A semaphore S is integer value ( counting Semaphore ) that can only be accessed via two
standard ( atomic ) operation); wait and signal
Originally termed as P and V

Wait:
Operation wait(s)
Begin
If s> 0
Then s= s – 1
Else block the process on s;
End:

Signal:
Operation wait(s)
Begin
If some processes are blocked on s then activate one blocked process
Else s = s + 1;
End

When a process performs a wait operation on a semaphore the operations either blocks the
process on the semaphore or allows it to continue its execution after decrementing the value of
semaphore.
A signal operation activates a process blocked on the semaphore or increment the value of
semaphore by 1.

Usage:
The semaphores are used to solve the n process critical section problem. These n processes share
a semaphore called as Mutex. This mutex is initialized to 1.

Page 36 of 61
Every process Pi is given as
Do {
Wait(mutex);
Critical section
Signal(mutex);
Remainder section
} while (1);
Also the synchronization problems are solved by the semaphores.

Example:
Let the two processes P1 with a statement S1 and
P2 with a statement S2.
Here S2 has to be done after the S1 is completed
This is achieved by allowing the P1 and P2 to share a common semaphore ‘Synch’.
It is assigned to 0 initially.
Also it is obtained by
Inserting the statement S1: Signal (Synch); in process P1
Inserting the statement Wait (Synch); S2 in process P2,
Because synch is initialized to 0, P2 will execute S2 only after P1 has invoked a signal (synch)
after S1.

Implementation: The disadvantages of mutual exclusion and semaphore require busy waiting.

Spin lock
A type of mutex lock that occur when the process spins while waiting for the lock to become
available
When a process is in its critical section, any other process which tries to enter its critical section
must loop continuously in the entry code.
Advantage of Spin Lock
No context switch a required when a process must wait on a lock, and a context switch may take
considerable time.

Page 37 of 61
Semaphore implementation with no busy waiting:
To overcome the need for busy waiting we can modify the definition of wait and signal
semaphore operation.
With each semaphore there is an associated waiting queue.
Each entry in a waiting queue has 2 data item
Value (of the type integer)
Pointer to next record in the list.

Assume two simple operations:


1. Block suspends: the process that invokes it.
2. Wakeup(P): remove one of the processor in waiting queue and placed in the ready
queue. Semaphore operations are now defined as
wait (S):
{ value--;
if (Value< 0)
{
Add this process to waiting queue;
Block();
}
}
signal(S):
{ value++;
if (value <= 0)
{
Removed process to waiting queue;
Wakeup;
}
}

Page 38 of 61
Block/Resume Semaphore Implementation:
If process is blocked, enqueue PCB of process and call scheduler to run a different process.
Semaphores are executed atomically;
 No two processes execute wait and signal at the same time.
 Mutex can be used to make sure that two processes do not change count at the same time.
 If an interrupt occurs while mutex is held, it will result in a long delay.
 Solution: Turn off interrupts during critical section.

Two Types of Semaphores:


1. Counting Semaphore
Integer value can range over an unrestricted domain.
2. Binary Semaphore
Integer value can range only between 0 and 1; simpler to implement.
It is also known as Mutex locks
Can implement a counting semaphore S as a binary semaphore.

Implementing S (counting semaphore) as a Binary Semaphore:


Let S be a counting semaphore.
To implement it n terms of binary semaphore.
We need the following data structures
Binary semaphore S1,S2;
Int C;
Initially S1=1, S2=0 and value of the integer C is set to the initial value of the counting
semaphore S

Implementing S:
Wait operation
wait(S1);
C--;
If(C<0)
{

Page 39 of 61
Signal(s1)
Wait(S2);
}
Signal(s1);

Signal operation
Wait(S1);
C++;
If(C<0)
Signal(S2);
Else
Signal(s1);

Deadlock and Starvation:


Deadlock
Two or more processes are waiting indefinitely for an event that can be caused by only one of the
waiting processes
Let S and Q be two semaphores initialized to 1
P0 P1
wait (S); wait (Q);
wait (Q); wait (S);
. . .
signal (S); signal (Q);
signal (Q); signal (S);
Starvation
It is In-definite blocking.
A process may never be removed from the semaphore queue in which it is suspended.

Page 40 of 61
CLASSIC PROBLEMS OF SYNCHRONIZATION:
1. Bounded Buffer Problem
2. Readers and Writers Problem
3. Dining-Philosophers Problem

1. Bounded Buffer Problem:


This illustrates the concept of cooperating processes; consider the producer and consumer
problem. A producer process produces information that is consumed by a consumer.

Example:
 A print program produces characters that are consumed by the printer device.
 We must have available buffer of items that can be filled by the producer and emptied by
the consumer.
 A producer can produce one item while the consumer is consuming another item.
 The producer and the consumer must be synchronized so that the consumer does not try
to consume n item that has not yet been produced.

In unbounded buffer producer consumer problems has no problem on the limit on the size of
the buffer. The consumers have to wait for new items but the producer can always produce new
items.

In bounded buffer producer consumer problem assumes fixed buffer size. In this case if
consumer must wait if the buffer is empty and the producer must wait if the buffer is full.

In the bounded buffer problem, we assume the pool consists of n buffers, each capable of holding
one item.
The mutex semaphore provides mutual exclusion for access to the buffer pool and is initialized
to the value 1.
The empty and full semaphores count the number of empty and full buffers. The semaphore
empty is initialized to value n, and full initialized to the value 0.

Page 41 of 61
Fig. The structure of the producer process.
Do {
….
Produce an item
….
Wait (empty);
Wait (mutex);
….
Add to buffer
….
Signal (mutex);
Signal (full);
} while (1);
Fig. The structure of consumer process.
Do {
Wait (full);
Wait (mutex);
….
Remove an item from buffer
….
Signal (mutex);
Signal (empty)
….

Page 42 of 61
Consume the item
…. }while(1);

2. The Readers –Writers Problem


A data object such as a file is to be shared among several processes. Some of these processes
may want to read the contents of the object, others may want to update.

Processes that are interested in only reading are referred to as readers and the rest as writers.
If two readers share the data object no effects will result, it a writer and some other process
access the shared object simultaneously errors occur.
This synchronization problem is referred to as readers-writers problem.
The reader’s writer’s problem has several variations.

The simplest one, referred to as the first readers-writers problem, requires that no reader will
be kept waiting unless a writer already obtained permission to use the shared object.

Second readers-writers problem requires that once a writer is ready, that writer performs its
write as soon as possible.

Solution to either problem is starvation. In the first case writers may starve, in the second case
readers may starve.

The solution to the first readers-writers problem, the reader processes share the following data
structures:
Semaphore mutex, wrt;
intreadcount;
The semaphores mutex and wrt are initialized to 1; readcount is initialized to 0. The
semaphore wrt is common to both the reader and writer processes. The mutex semaphore is used
to confirm mutual exclusion when the variable readcount is updated. The readcount variable
keeps track of how many processes are currently reading the object.

Page 43 of 61
Fig. The structure of reader process:
wait(mutex);
readcount++;
if (readcount ==1)
wait(wrt);
signal(mutex);

reading is performed

wait(mutex);
readcount --;
if(readcount ==0)
signal(wrt);
signal(mutex);
Fig. The structure of a writer process:
Page 44 of 61
wait(wrt);

writing is performed

signal(wrt);

3. The Dining-Philosophers Problem:


Consider five philosophers who spend their lives thinking and eating.
The philosophers share a common circular table surrounded by five chairs each for the five
philosophers.
In the center of the table is a bowl of rice, and the table is arranged with five single chopsticks.
When a philosopher thinks, she does not interact with her colleagues. From time to time, a
philosopher gets hungry and tries to pick up the two chopsticks that are closest to her.
A philosopher may pick up only one chopstick at a time. She cannot pick up a chopstick that is
already in the hand of a neighbor.
When a hungry philosopher has both her chopsticks at the same time, she eats without releasing
her chopsticks.
When she is finished eating, she puts down both of her chopsticks and starts thinking again.

The dining philosopher is a simple representation of the need to allocate several resources
among several processes in a deadlock and starvation free manner.
One simple solution is to represent each chopstick by a semaphore. A philosopher goes to take
the chopstick by executing a wait operation on that semaphore. She releases her chopsticks by
executing the signal operation on appropriate semaphores. Thus the shared data are:

Page 45 of 61
Semaphore chopstick[5];
Where all the elements of chopstick are initialized to 1.
do{
wait(chopstick[I]);
wait(chopstick[(I+1)%5];

eat

signal(chopstick[I]);
signal(chopstick[(I+1)]%5);

think

}while(1);
This solution guarantees that no two neighbors are eating simultaneously, it must be
rejected because it has the possibility of creating a deadlock. Suppose that all five philosophers
become hungry simultaneously, and each takes her left chopstick. All the elements of chopstick
will now be equal to 0. When each philosopher tries to take her right chopstick, she will be
delayed forever.
Several remedies to deadlock problem are:
 Allow at most four philosophers to be sitting simultaneously at the table.
 Allow a philosopher to pick up her chopsticks only if both chopsticks are available.
 Use asymmetric solution: an odd philosopher picks up first her left chopstick and then
her right chopstick, whereas an even philosopher picks up her right and then her left
chopstick.
_____________________________________________________________________________

Page 46 of 61
MONITORS:
 Consist of private data and operations on that data.
 Can contain types, constants, variables and procedures.
 Only the procedures explicitly marked can be seen outside the monitor.
 The monitor body allows the private data to be initialized.
 The compiler enforces mutual exclusion son a particular monitor.
 Each monitor has a boundary queue, and processes wanting to call a monitor routine
join this queue if the monitor is already in use.
 Monitors are an improvement over conditional critical regions because all the code that
accesses the shared data is localized.
1. Condition synchronization
Just as plain critical regions needed to be extended to conditional critical regions, so do monitors
(as described thus far) need to be extended to make them as applicable as semaphores.
Condition variables allow processes to block until some condition is true and then be woken up:
var c : condition;
Two operations are defined for condition variables:
i. Delay ii. Resume
Delay
 Similar to the semaphore wait operation.
 delay(c) blocks the calling process on c and releases the lock on the monitor.
Resume
 Similar to the semaphore signal operation.
 resume(c) unblocks a process waiting on c.
 resume is a nop if no processes are blocked (compare to signal, which always has an
effect).
But once resume has been called we have (potentially) two processes inside the monitor:
 The process that called delay and has been woken up.
 The process that called resume.
Solutions:
 Resume-and-continue The woken process must wait until the one that called resume
releases the monitor.

Page 47 of 61
 Immediate resumption The process that called resume must immediately leave the
monitor (this is what Pascal-FC uses).
Resume-and-continue means that processes that call delay must use
while not B do
delay(c);
instead of
if not B then
delay(c);
Because a resumer might carry on and alter the condition after calling resume but before exiting
the monitor.
2. Immediate resumption variants
1. Resume and exit: The resumer is automatically forced to exit the monitor after calling
resume.
2. Resume and wait: The resumer is put back on the monitor boundary queue. When it gets
back in, it is allowed to continue from where it left off.
3. Resume and urgent wait: The resumer is put on a second queue that has priority over
the monitor boundary queue.
3. Nested monitor calls
A nested monitor call is when a procedure in monitor A calls a procedure in a different monitor,
say monitor B. They are potentially troublesome: think about what happens if the procedure in
monitor B contains a delay statement.
There are several approaches that can be taken when faced with this situation:
1. Retain the lock on A when calling the procedure in B, release the lock on B when calling
delay in B.
2. Retain the lock on A when calling the procedure in B, release both locks when calling delay
in B.
3. Release the lock on A when making a nested call.
4. Ban nested calls altogether!
Pascal-FC chooses the first of these alternatives.
_____________________________________________________________________________

Page 48 of 61
DEADLOCK:
 A process request the resources, the resources are not available at that time, so the
process enter into the waiting state. The requesting resources are held by another waiting
process both are in waiting state, this situation said to be deadlock.
Example: R1

P1 P1

P1 and p2 are two processes.


 R1 and r2 are two resources. R2
 P1 request the resources R1.
 R1 held by the processes P2.
 P2 request the resources R2.
 R2 held by the processes P1.
 Then both are entered into the waiting state.
 There is no work progress for process P1 and p2. It is also a deadlock.

Types of Dead lock:


1. Process Deadlock
A process is deadlocked when it is waiting on an event which will never happen.
2. System deadlock
A system is deadlocked when one or more processes are deadlocked

RESOURCE ALLOCATION GRAPH (RAG).


Deadlock can be represented by resource allocation graph.
Resource allocation graph is a directed graph. It is used to represent the deadlocks.
The graph consisting of two types of nodes
R1
1. Process it is represented by Circles.
2. Resources it is represented by squares
P1 P1

R2
Page 49 of 61
And the graph is consisting of two types of edges
1. Requesting edge
2. Assignment edge
An edge from process to resource is said to be a requesting edge.
And an edge from a resource to a process is said to be assignment edge.
If the resource allocation graph consisting of cycles P1 R1P2R2P1
But if system in deadlock state, the resource allocation graph must consisting of cycles

CONDITION FOR DEADLOCK OR CHARACTERISTICS OF DEADLOCK:


It must satisfy for four conditions.
1. Mutual exclusion:
It means resources are (at least one) in non-sharable mode only, it means only one process at a
time can use a resource.
If some other process requests that resources the requesting process must be wait until the
resources has been released.
For example: line printers
The line printers are always in non sharable mode only
Only one process can use the resource at a time.

2. Hold and Wait


Each and every process in deadlock state must hold at least one resource is waiting for additional
resources that are currently being held by other processes.
Example: R2
R1

P1
P1 P3

R3
R4

P1, P2 and P3 are Processes


R1, R2, and R3 are Resources

Page 50 of 61
Each processes holding a resources and waiting for another resources.
P1 holding the resources R3 and is waiting for the resources R1.
P2 holding resources R1 and R2 are waiting for R3 resources.
P3 holding the resources R2 is waiting for resources R4.

3. No preemption:
It means resources are not released in the middle of the work they released only after the process
has completed its task.

4. Circular Wait:
In above figure,
 P1 is waiting for a resource R1 held by the processes P2.
 P2 is waiting for a resource R2 held by the processes P3.
 P3 is waiting for a resource R4 held by the processes P2.
 P2 is waiting for a resource R3 held by the processes P1.
P1 R1P2R2P3R4P2R3

METHODS FOR HANDLING DEADLOCKS:


Principally, there are three different methods for dealing with the deadlock problem:
1. Protocol
Ensure that the system will never enter a deadlock state.
2. Detect and Recover
Allow the system to enter a deadlock state detect it and then recover.
3. Ignore the problem
Ensure that deadlocks never occur in the system.

Deadlock Management:
The system can use either deadlock prevention or a deadlock-avoidance scheme.

Page 51 of 61
Deadlock prevention:
It is to prevent that deadlocks occurrence by ensuring that at least one of the necessary conditions
cannot hold.

Deadlock avoidance:
Requires that the operating system be given in advance additional information concerning which
resources a process will request and use during its lifetime.

DEADLOCK PREVENTION:
Deadlock prevention is same as take the prevention methods before attacking the deadlock
For a deadlock to occur,
Each of the four necessary conditions must hold by ensuring that at least one of these
conditions can’t hold.
We can prevent the occurrence of the deadlock.
So apply this technique on four necessary conditions.

Prevention Methods:
1. Mutual exclusion:
 It means only one process can use the resources at a time
 It means the resources are not shared by the number of process at a time. So the resources
are sharable mode only.
 We can deny this condition, with simple protocol
 Convert the all non-sharable resources to sharable resources.
 So this condition is not satisfied by the deadlock, the n we can prevent the deadlock.
 Here is a small correction; a printer is not shared by number of process at a time,
because we can’t convert the printer from non sharable to sharable mode.
 So we can’t apply this prevention method if the system consisting of printers.

Page 52 of 61
2. Hold and Wait
 It means each and every process in deadlock state must hold at least one resource and
waiting for at least one resources.
 We can deny this condition, with simple protocol
 I.e. A process request the resources (it doesn’t hold any other resources) only when the
process has none.
 This protocol is very expensive and time consuming one.
 For example:
 Process wants to copy the data from a tape drive to a disk file and then take a print out.
Initially the processes consisting of tape drive and disk file
 Now the process to be requests the printer. Apply the above said protocol. The process
should releases the tape drive ad disk file before the request of printer. So it is time
consuming
 Each process to request and be allocated all its resources before it begins execution. It is
very difficult to implement because more number of resources are available to begin the
execution.
For example:
 P1…..P10 are 100 processes
 Each requires a printer to complete their jobs. Applying the above protocol the system
must consist of 100 printers. So it is very difficult to implement.
 There are two main disadvantages:
 Resources Utilization it is very poor, since many of the resources may be allocated but
unused for a long period.
 Starvationa process that needs several popular resources may have to wait indefinitely
because at least one of the resources i.e. needs always allocated to some other process.

3. No preemption:
 It means resources are not released in the middle of the processing of a resource.
 We ensure that this condition doesn’t hold if we use the following protocol.
1) A process request some resources, we first check whether they are available.
If they are available we allocate them

Page 53 of 61
2) If they are not available, we check whether they are allocated to some other process
i.e. waiting for additional resources.
 If so, we prevent the desired resources from the waiting process and allocate to the
requesting process.
 If resources are not either available or held by a waiting process then the requesting
process must wait.
 While it is waiting some of it resources may be used by other process.

4. Circular Wait:
 We ensure that circular wait must not happen if we apply a simple solution.
 i.e. numbering all the resources types and each process request resources in an increasing
order of enumeration.
 Alternatively:
 Whenever a process request an instance resources type R j, it has released only resources
Ri. Such that F(Ri) > F(Rj).
 A process can initially request any number of instances type R j, if and only if F(Rj) >
F(Ri)
 If there two protocols are used then the circular wait cannot hold.

Page 54 of 61
DEADLOCK AVOIDANCE:
It is one for the methods of dynamically escaping from the deadlocks.
The word dynamically means online.
In this scheme if a process request for resources the avoidance algorithm checks before the
allocation of resources about the state of the system.

There are two states:


 Safe state  the system allocates the resources to the requesting process.
 Unsafe states  do not allocate the resources.
So taking care before the allocation is said to be deadlock avoidance.
The students may have a doubt about safe and unsafe state.
Safe state means no deadlock will happen; even we allocate the resources to requesting process.
Unsafe state means the deadlock may happen if the grant the resources.
Unsafe
Deadlocko

Safe

Figure: safe, unsafe and deadlock state.

R1

P1 P1

R2

Figure: unsafe state in resources allocation graph


A safe state is not a deadlock state.
On the other hand a deadlock sate is an unsafe state
Not all states are deadlocks. However, an unsafe state may lead to deadlock.
As long as the state is safe, the operating system can avoid unsafe state.

Page 55 of 61
Bankers Algorithm:
 The resource-allocation graph algorithm is not applicable to a resource allocation system
with multiple instances of each resource type.
 The deadlock-avoidance algorithm that we describe next is applicable to such a system,
but is less efficient than the resource-allocation graph scheme.
 This algorithm is commonly known as the banker's algorithm.

 The name was chosen because this algorithm could be used in a banking system to ensure
that the bank never allocates its available cash such that it can no longer satisfy the needs
of all its customers.

 When a new process enters the system, it must declare the maximum number of instances
of each resource type that it may need. This number may not exceed the total number of
resources in the system.

 When a user requests a set of resources, the system must determine whether the
allocation of these resources will leave the system in a safe state. If it will, the resources
are allocated; otherwise/the process must wait until some other process releases enough
resources.

 Several data structures must be maintained to implement the banker's algorithm. These
data structures encode the state of the resource-allocation system.

Example: Resource-Allocation State: Available ABC = [10, 5, 7] - [7, 2, 5]= [3, 3, 2]


Process Allocation MAX Available
A B C A B C A B C
P1 0 1 0 7 5 3 3 3 2
P2 2 0 0 3 2 2
P3 3 0 2 9 0 2
Process
P4 2 1 1 2 Need
2 2
A B C
P5 0 0 2 4 3 3
P1 7 4 3
Need = MAX - Allocation
P2 1 2 2
P3 6 0 0 Page 56 of 61

P4 0 1 1
P5 4 3 1
Currently the system is safe sequence
Safe sequence: Safe sequence is calculated as follows:
1. Need of each process is compared with available.
If Needi  Availablei, then the resources are allocated to that process and process will
releases the resource.
2. If Need >= Available, next process need is taken for comparison.
3. In the above example,
Need of Process P1 is (7, 4, 3) & Available is (3, 3, 2)
Need >= Available
7, 4, 3 >= 3, 3, 2False
So the system will move for next process
4. Need for Process P2(1, 2, 2) & Available is (3, 3, 2), so
Need < Available (work),
1, 2, 2 > 3, 3, 2True
Then Finish[i] = True
Request of p2 is granted and a process P2 is releases the resource to the system.
Work: = Work + Allocation or
Available = Available + Allocation = (3, 3, 2) + (2, 0, 0) = (5, 3, 2) New available
This procedure continues for all processes.
5. Next Process P3 need (6, 0, 0) is compared with new available (5, 3, 2)
Need > Available
6, 0, 0 > 5, 3, 2False
So the system will move for next process
6. Process P4 need (0, 1, 1) is compared with new available (5, 3, 2)

Page 57 of 61
Need < Available
0, 1, 1 < 5, 3, 2True
Available = Available + Allocation = (5, 3, 2) + (2, 1, 1):= (7, 4, 3) New Available
7. Then Process P5 need (4, 3, 1) is compared with new available (7, 4, 3)
Need < Available
4, 3, 1 < 7, 4, 3True
Available = Available + Allocation = (7, 4, 3) + (0, 0, 2):= (7, 4, 5) New Available
8. Then Process P1 need (7, 4, 3) and available (7, 4, 5) is request is granted then the
system may be in the deadlock state. After the granting the request, available resource
is (0, 0, 2) so the system is in unsafe state.
9. Then Process P3 need (6, 0, 0) is compared with new available (7, 4, 5)
Need < Available
6, 0, 0 < 7, 4, 5True
Available = Available + Allocation = (7, 4, 5) + (3, 0, 2) = (10, 4, 7) New available
10. Last the remaining Process P1 need (7, 4, 3) and available (10, 4, 7)
Need < Available
7, 4, 3 < 10, 4, 7True
Available = Available + Allocation = (10, 4, 7) + (0, 1, 0) = (10, 5, 7)
State is Safe:
Safe sequence is <P2, P4, P5, P3, and P1>

DEADLOCK DETECTION:

Page 58 of 61
 It is used by employing an algorithm that tracks the circular waiting and killing one or
more processes. So that deadlock is removed.
In Deadlock Detection Techniques:
 The Resources are granted to the requesting processes without any check.
 The system state is examined periodically to determine of a set of processes is deadlock.
 A deadlock is resolved by aborting and restarting a process, (resign or give up) all the
resources that the process held.

There are two algorithms for Deadlock Detection:


1. Single instance for each resource type
2. Multiple or Several instance for each resource type

1. Single Instance of Each Resource Type:


 In Resource Allocation Graph (RAG), every resource has only one instance or
single instance.
 Then we define Deadlock Detection algorithm that uses a different of the RAG and is
called wait for graph (WFG).
How to get this graph from RAG:
 We can get this graph from RAG by removing the nodes of type resource and
collapsing the appropriate edge. In this situation if the wait for graph has any cycle
then there is deadlock in the system. To detect deadlock, the system needs to maintain
the WFG and to periodically invoke an algorithm that searches for a cycle in the
graph.
 The complexity algorithm will be O(n 2) where n is the number of vertices of n the
graph. We can draw corresponding WFG by removing all nodes that represent
resources and collapsing this edge.
 Observe carefully that the system is in deadlock sate there exists a cycle between
P1….P4. Also a check is found between P1, P2, P3 and P4.
 So all processes P1 to P4 are deadlocked

Page 59 of 61
2. Several Instances of a Resource Type
 The wait-for graph scheme is not applicable to a resource-allocation system with multiple
instances of each resource type.
 The deadlock-detection algorithm that we describe next is applicable to such a system.
The algorithm employs several time-varying data structures that are similar to those used
in the banker's algorithm
 This algorithm requires an order; of m x n2 operations to detect whether the system is in
a deadlocked state.

Page 60 of 61
RECOVERY FROM DEADLOCK:
When a detection algorithm determines that a deadlock exists, several alternatives exist.
 One possibility is to inform the operator that a deadlock has occurred, and to let
the operator deal with the deadlock manually.
 The other possibility is to let the system recover from the deadlock automatically.

There are two options for breaking a deadlock.


 First Solution is simply to abort one or more processes to break the circular wait.
 Second option is to preempt some resources from one or more of the deadlocked
processes.
1. Process Termination
 Abort all deadlocked processes: it means releases the all processes in the deadlock
state and start the allocation from the starting point
 Abort one process at a time until the deadlock cycle is eliminated: first abort one of
the processes on the deadlock state and allocated the resources to some other process in
the deadlock state the check whether the deadlock braked or not.
If not abort another process from the deadlock, continue this process until we recover
from deadlock. Compared to first one second one is better
 In which order should we choose to abort?
- Priority of the process.
- How long process has computed, and how much longer to completion.
- Resources the process has used.
- Resources process needs to complete.
- How many processes will need to be terminated
- Is process interactive or batch?
2. Resource Preemption
 Selecting a victim – minimize cost.
 Rollback – return to some safe state, restart process for that state.
 Starvation – same process may always be picked as victim, include number of rollback
in cost factor.

Page 61 of 61

You might also like