0% found this document useful (0 votes)
4 views11 pages

File 3

The document discusses various concepts related to process synchronization and deadlock in operating systems, including Peterson's solution, semaphores, critical sections, the Readers-Writers problem, the Dining Philosophers problem, and the Banker's algorithm for deadlock avoidance. It outlines necessary conditions for deadlock, methods for deadlock detection, and recovery mechanisms such as process termination and resource preemption. Additionally, it explains the resource allocation graph and its significance in visualizing resource allocation and process states.

Uploaded by

meela Yashwanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

File 3

The document discusses various concepts related to process synchronization and deadlock in operating systems, including Peterson's solution, semaphores, critical sections, the Readers-Writers problem, the Dining Philosophers problem, and the Banker's algorithm for deadlock avoidance. It outlines necessary conditions for deadlock, methods for deadlock detection, and recovery mechanisms such as process termination and resource preemption. Additionally, it explains the resource allocation graph and its significance in visualizing resource allocation and process states.

Uploaded by

meela Yashwanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

OS 2nd UNIT USMAN GROUP-2

11) Illustrate the Peterson’s solution and semaphores provide a solution for
critical section problem.

Semaphores are just normal variables used to coordinate the activities of


multiple processes in a computer system. They are used to enforce mutual
exclusion, avoid race conditions, and implement synchronization between
processes.

When more than one processes try to access the same code segment that
segment is known as the critical section. The critical section contains shared
variables or resources that need to be synchronized to maintain the consistency
of data variables.

Peterson’s Algorithm
Referred to as producer-consumer problem (or bounded buffer problem)
describes two processes, the producer and the consumer, which share a
common, fixed-size buffer used as a queue. Producers produce an item and put
it into the buffer. If the buffer is already full then the producer will have to wait
for an empty block in the buffer. Consumers consume an item from the buffer.
If the buffer is already empty then the consumer will have to wait for an item
in the buffer. Implement Peterson’s Algorithm for the two processes using
shared memory such that there is mutual exclusion between them. The solution
should have free from synchronization problems.

Peterson’s Algorithm is used to synchronize two processes. It uses two


variables, a bool array flag of size 2 and an int variable turn to accomplish it.
In the solution, i represents the Consumer and j represents the Producer.

Initially, the flags are false. When a process wants to execute its critical section,
it sets its flag to true and turn into the index of the other process. This means
that the process wants to execute but it will allow the other process to run first.
The process performs busy waiting until the other process has finished its own
critical section. After this, the current process enters its critical section and adds
or removes a random number from the shared buffer. After completing the
critical section, it sets its own flag to false, indicating it does not wish to execute
anymore.

The program runs for a fixed amount of time before exiting. This time can be
changed by changing value of the macro-RT.

12) What is a critical section? Apply hardware solution for critical section
problem in detail.

Process Synchronization or Critical Section problems occur when two


processes running concurrently share the same data or same variable. The
value of that variable may not be updated correctly before its being used by a
second process.

There are three algorithms in the hardware approach of solving Process


Synchronization problem:
Test and Set
Swap
Unlock and Lock

1. Test and Set:


Here, the shared variable is lock which is initialized to false. TestAndSet(lock)
algorithm works in this way – it always returns whatever value is sent to it and
sets lock to true. The first process will enter the critical section at once as
TestAndSet(lock) will return false and it’ll break out of the while loop. The
other processes cannot enter now as lock is set to true and so the while loop
continues to be true. Mutual exclusion is ensured. Once the first process gets
out of the critical section, lock is changed to false. So, now the other processes
can enter one by one.

2. Swap:
Swap algorithm is a lot like the TestAndSet algorithm. Instead of directly
setting lock to true in the swap function, key is set to true and then swapped
with lock. First process will be executed, and in while(key), since key=true ,
swap will take place and hence lock=true and key=false. Again, next iteration
takes place while(key) but key=false , so while loop breaks and first process
will enter in critical section. Now another process will try to enter in Critical
section, so again key=true and hence while(key) loop will run and swap takes
place so, lock=true and key=true (since lock=true in first process). so this will
keep on executing and another process will not be able to enter in critical
section.
3. Unlock and Lock
Unlock and Lock Algorithm uses TestAndSet to regulate the value of lock but
it adds another value, waiting[i], for each process which checks whether or not
a process has been waiting. A ready queue is maintained with respect to the
process in the critical section. All the processes coming in next are added to the
ready queue with respect to their process number, not necessarily sequentially.
Once the ith process gets out of the critical section, it does not turn lock to false
so that any process can avail the critical section now

13) Briefly explain Readers and writers’ problem

The Readers-Writers Problem is a classic synchronization issue in operating


systems that involves managing access to shared data by multiple threads or
processes. The problem addresses the scenario where:
Readers: Multiple readers can access the shared data simultaneously without
causing any issues because they are only reading and not modifying the data.
Writers: Only one writer can access the shared data at a time to ensure data
integrity, as writers modify the data, and concurrent modifications could lead
to data corruption or inconsistencies.

Challenges of the Reader-Writer Problem


The challenge now becomes how to create a synchronization scheme such that
the following is supported:
• Multiple Readers: A number of readers may access simultaneously if no writer
is presently writing.
• Exclusion for Writers: If one writer is writing, no other reader or writer may
access the common resource.

Solution of the Reader-Writer Problem


There are two fundamental solutions to the Readers-Writers problem:
• Readers Preference: In this solution, readers are given preference over writers.
That means that till readers are reading, writers will have to wait. The Writers
can access the resource only when no reader is accessing it.
• Writer’s Preference: Preference is given to the writers. It simply means that,
after arrival, the writers can go ahead with their operations; though perhaps
there are readers currently accessing the resource.
Functions for Semaphore
Semaphores are synchronization tools used in operating systems to manage
access to shared resources by multiple threads or processes. They use simple
integer values and two main operations to control access:
wait() : decrements the semaphore value.
signal() : increments the semaphore value.

Writer Process
Writer requests the entry to critical section.
If allowed i.e. wait() gives a true value, it enters and performs the write. If not
allowed, it keeps on waiting.
It exits the critical section.

Reader Process
Reader requests the entry to critical section.
If allowed:
o it increments the count of number of readers inside the critical section.
If this reader is the first reader entering, it locks the wrt semaphore to
restrict the entry of writers if any reader is inside.
o It then, signals mutex as any other reader is allowed to enter while
others are already reading.
o After performing reading, it exits the critical section. When exiting, it
checks if no more reader is inside, it signals the semaphore “wrt” as now,
writer can enter the critical section.
14) Explain in detail about Dinning Philosophers problem.

The Dining Philosopher Problem states that K philosophers are seated around
a circular table with one chopstick between each pair of philosophers. There is
one chopstick between each philosopher. A philosopher may eat if he can pick
up the two chopsticks adjacent to him. One chopstick may be picked up by any
one of its adjacent followers but not both.

Suppose Philosopher P0 wants to eat, it will enter in Philosopher() function,


and execute take_chopstick[i]; by doing this it holds C0 chopstick after that it
execute take_chopstick[ (i+1) % 5]; by doing this it holds C1 chopstick( since i
=0, therefore (0 + 1) % 5 = 1)
Similarly suppose now Philosopher P1 wants to eat, it will enter in
Philosopher() function, and execute take_chopstick[i]; by doing this it
holds C1 chopstick after that it execute take_chopstick[ (i+1) % 5]; by doing
this it holds C2 chopstick( since i =1, therefore (1 + 1) % 5 = 2)

We use a semaphore to represent a chopstick and this truly acts as a solution of


the Dining Philosophers Problem. Wait and Signal operations will be used for
the solution of the Dining Philosophers Problem, for picking a chopstick wait
operation can be executed while for releasing a chopstick signal semaphore can
be executed.
15) What is a deadlock? Explain necessary conditions for deadlock?

Deadlock is a situation in computing where two or more processes are unable


to proceed because each is waiting for the other to release resources. Key
concepts include mutual exclusion, resource holding, circular wait, and no
Preemption.
Deadlock is an infinite Process it means that once a process goes
into deadlock it will never come out of the loop and the process will enter for
an indefinite amount of time. There are only detection, resolution, and
prevention techniques. But, there are no Deadlock-stopping techniques.

Necessary Conditions for the Occurrence of a Deadlock


Let’s explain all four conditions related to deadlock in the context of the
scenario with two processes and two resources:

1. Mutual Exclusion
Mutual Exclusion condition requires that at least one resource be held in a non-
shareable mode, which means that only one process can use the resource at any
given time. Both Resource 1 and Resource 2 are non-shareable in our scenario,
and only one process can have exclusive access to each resource at any given
time. As an example:
Process 1 obtains Resource 1.
Process 2 acquires Resource 2.

2. Hold and Wait


The hold and wait condition specifies that a process must be holding at least
one resource while waiting for other processes to release resources that are
currently held by other processes. In our example,
Process 1 has Resource 1 and is awaiting Resource 2.
Process 2 currently has Resource 2 and is awaiting Resource 1.
Both processes hold one resource while waiting for the other, satisfying the
hold and wait condition.
3. No Preemption
Preemption is the act of taking a resource from a process before it has finished
its task. According to the no Preemption condition, resources cannot be taken
forcibly from a process a process can only release resources voluntarily after
completing its task.
For example – Process p1 have resource r1 and requesting for r2 that is hold by
process p2. then process p1 cannot pre-empt resource r2 until process p2 can
finish his execution. After some time, it tries to restart by requesting both r1
and r2 resources.

4. Circular Wait
Circular wait is a condition in which a set of processes are waiting for resources
in such a way that there is a circular chain, with each process in the chain
holding a resource that the next process needs. This is one of the necessary
conditions for a deadlock to occur in a system.
Example: Imagine four processes—P1, P2, P3, and P4—and four resources—
R1, R2, R3, and R4.
P1 is holding R1 and waiting for R2 (which is held by P2).
P2 is holding R2 and waiting for R3 (which is held by P3).
P3 is holding R3 and waiting for R4 (which is held by P4).
P4 is holding R4 and waiting for R1 (which is held by P1).

16) Explain Banker’s algorithm for deadlock avoidance with an example?

The Banker’s Algorithm is a resource allocation and deadlock avoidance


algorithm that tests for safety by simulating the allocation for the
predetermined maximum possible amounts of all resources, then makes an “s-
state” check to test for possible activities, before deciding whether allocation
should be allowed to continue.

The banker’s algorithm is named so because it is used in the banking system to


check whether a loan can be sanctioned to a person or not. Suppose there are n
number of account holders in a bank and the total sum of their money is S. Let
us assume that the bank has a certain amount of money Y. If a person applies
for a loan, then the bank first subtracts the loan amount from the total money
that the bank has (Y) and if the remaining amount is greater than S then only
the loan is sanctioned. It is done because if all the account holders come to
withdraw their money, then the bank can easily do it.

Banker's Algorithm in Operating Systems


Likewise, in an operating system:
o When a new process is created, it needs to provide all the vital information,
such as which processes are scheduled to run shortly, resource requests, and
potential delays.
o This knowledge helps the OS decide which sequence of process executions
needs to proceed to avoid any deadlock.
o Since the order of executions that should occur in order to prevent deadlocks
is defined, the Banker's Algorithm is usually considered a deadlock avoidance
or a deadlock detection algorithm in OS

Need [i, j] = Max [i, j] – Allocation [i, j]


So, the content of Need Matrix is:

17) Explain about resource allocation graph (RAG)?

The resource allocation graph is the pictorial representation of the state of a


system. As its name suggests, the resource allocation graph is the complete
information about all the processes which are holding some resources or
waiting for some resources.
It also contains the information about all the instances of all the resources
whether they are available or being used by the processes.
In Resource allocation graph, the process is represented by a Circle while the
Resource is represented by a rectangle. Let's see the types of vertices and edges
in detail.
A resource can have more
than one instance. Each instance
will be represented by a dot inside the rectangle.
Edges in RAG are also of two types, one represents assignment and other
represents the wait of a process for a resource. The above image shows each of
them.
A resource is shown as assigned to a process if the tail of the arrow is attached
to an instance to the resource and the head is attached to a process.
A process is shown as waiting for a resource if the tail of an arrow is attached
to the process while the head is pointing towards the resource.

According to the graph, R1 is being used by P1, P2 is holding R2 and waiting


for R1, P3 is waiting for R1 as well as R2.
19) Explain about Deadlock detection and Deadlock Recovery mechanisms in
detail.
20) Write a short notes on Deadlock Recovery mechanisms in detail

Ways of Handling a Deadlock


There are several ways of handling a deadlock, some of which are mentioned
below:

1. Process Termination
To eliminate the deadlock, we can simply kill one or more processes. For this,
we use two methods:
Abort all the Deadlocked Processes : Aborting all the processes will certainly
break the deadlock but at a great expense. The deadlocked processes may have
been computed for a long time, and the result of those partial computations
must be discarded
Abort one process at a time until the deadlock is eliminated : Abort one
deadlocked process at a time, until the deadlock cycle is eliminated from the
system.

2. Resource Preemption
To eliminate deadlocks using resource preemption, we preempt some
resources from processes and give those resources to other processes.
Selecting a Victim : We must determine which resources and which processes
are to be preempted and also in order to minimize the cost.
Starvation : In a system, it may happen that the same process is always picked
as a victim. As a result, that process will never complete its designated task.
This situation is called Starvation and must be avoided. One solution is that a
process must be picked as a victim only a finite number of times.

3. Priority Inversion
A technique for breaking deadlocks in real-time systems is called priority
inversion. A higher priority is given to the process that already has the needed
resources, and a lower priority is given to the process that is still awaiting
them.

4. RollBack
In database systems, rolling back is a common technique for breaking
deadlocks. When using this technique, the system reverses the transactions of
the involved processes to a time before the deadlock. The system must keep a
log of all transactions and the system’s condition at various points in time in
order to use this method.
For 19th add this too

Deadlock Detection
1. If Resources Have a Single Instance
In this case for Deadlock detection, we can run an algorithm to check for the
cycle in the Resource Allocation Graph. The presence of a cycle in the graph is
a sufficient condition for deadlock.

2. If There are Multiple Instances of Resources


Detection of the cycle is necessary but not a sufficient condition for deadlock
detection, in this case, the system may or may not be in deadlock varies
according to different situations.

3. Wait-For Graph Algorithm


The Wait-For Graph Algorithm is a deadlock detection algorithm used to
detect deadlocks in a system where resources can have multiple instances. The
algorithm works by constructing a Wait-For Graph, which is a directed graph
that represents the dependencies between processes and resources.

You might also like