File 3
File 3
11) Illustrate the Peterson’s solution and semaphores provide a solution for
critical section problem.
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.
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.
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
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.
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.
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).
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.