Unit 2 OS 2023
Unit 2 OS 2023
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
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
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 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:
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.
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
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.
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.
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.
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.
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.
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.
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
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.
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.
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
Kernel Space
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
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
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
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
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.
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.
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.
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.
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.
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);
Page 40 of 61
CLASSIC PROBLEMS OF SYNCHRONIZATION:
1. Bounded Buffer Problem
2. Readers and Writers Problem
3. Dining-Philosophers Problem
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);
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);
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
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 R1P2R2P1
But if system in deadlock state, the resource allocation graph must consisting of cycles
P1
P1 P3
R3
R4
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 R1P2R2P3R4P2R3
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.
Starvationa 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.
Safe
R1
P1 P1
R2
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.
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, 2False
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, 2True
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, 2False
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, 2True
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, 3True
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, 5True
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, 7True
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.
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.
Page 61 of 61