OS Assignment
OS Assignment
Submitted By:
Nafizur Rahman
Registration No. :
2018831034
Department of Software
Engineering.
The critical section cannot be executed by more than one process at the same time; operating
system faces the difficulties in allowing and disallowing the processes from entering the critical
section. The critical section problem is used to design a set of protocols which can ensure that
the Race condition among the processes will never arise.
Peterson’s Solution:
Peterson’s Solution is a classical software based solution to the critical section problem.
In Peterson’s solution, we have two shared variables:
• Boolean flag[i] :Initialized to FALSE, initially no one is interested in entering the critical
section
• int turn: The process whose turn is to enter the critical section.
• do {
• flag[i] = true;
• turn = j;
• while (flag[j] && turn == j);
• /* critical section */
• flag[i] = false;
• /* remainder section */
• }
• while (true);
Work process:
The data of a mutex is an integer in memory. Its value starts at 0, meaning that it is
unlocked. If you wish to lock the mutex, you check if it is zero and then assign one. The mutex is
now locked, and you are the owner of it.
The trick is that the test-and-set operation has to be atomic. If two threads happen to read
0 at the same time, then both would write 1 and think they own the mutex. Without CPU support
there is no way to implement a mutex in user space: this operation must be atomic with respect
to the other threads. Fortunately, CPUs have a function called “compare-and-set” or “test-and-
set” which does exactly this. This function takes the address of the integer and two values: a
compare value and a set value. If the comparison value matches the current value of the integer,
then it is replaced with the new value.
Semaphore:
Semaphores are integer variables that are used to solve the critical section
problem by using two atomic operations, wait and signal that are used for process
synchronization.
The definitions of wait and signal are as follows −
• Wait
The wait operation decrements the value of its argument S, if it is positive. If S is
negative or zero, then no operation is performed.
wait(S)
{
while (S<=0);
S--;
}
• Signal
The signal operation increments the value of its argument S.
signal (S)
{
S++ ; }
3. What is Deadlock? Explain it using a real life experience.
Answer:
A deadlock happens in operating system when two or more processes need some
resource to complete their execution that is held by the other process.
For example, in the below diagram, Process 1 is holding Resource 1 and waiting for
resource 2 which is acquired by process 2, and process 2 is waiting for resource 1.
In the above diagram, the process 1 has resource 1 and needs to acquire resource 2. Similarly
process 2 has resource 2 and needs to acquire resource 1. Process 1 and process 2 are in
deadlock as each of them needs the other’s resource to complete their execution but neither of
them is willing to relinquish their resources.
Example:
Suppose two trains are coming toward each other on the same track and there is only
one track, none of the trains can move once they are in front of each other. In this situation a
deadlock arises. Because both the trains can’t use the only track in the same time. Similarly, when
two processes require same resource then a deadlock happens.
There is a buffer of n slots and each slot is capable of storing one unit of data. There are
two processes running, namely, producer and consumer, which are operating on the buffer. A
producer tries to insert data into an empty slot of the buffer. A consumer tries to remove data
from a filled slot in the buffer. As we might have guessed by now, those two processes won't
produce the expected output if they are being executed concurrently.
There needs to be a way to make the producer and consumer work in an independent manner.
Looking at the above code for a producer, we can see that a producer first waits until there is
atleast one empty slot.
• Then it decrements the empty semaphore because, there will now be one less empty
slot, since the producer is to insert data in one of those slots.
• Then, it acquires lock on the buffer, so that the consumer cannot access the buffer until
producer completes its operation.
• After performing the insert operation, the lock is released and the value of full is
incremented because the producer has just filled a slot in the buffer.
do
{
wait(full);
wait(mutex);
signal(mutex);
signal(empty);
}
while(TRUE);
• The consumer waits until there is atleast one full slot in the buffer.
• Then it decrements the full semaphore because the number of occupied slots will be decreased by one, after the
consumer completes its operation.
• After that, the consumer acquires lock on the buffer.
• Following that, the consumer completes the removal operation so that the data from one of the full slots is
removed.
• Then, the consumer releases the lock.
• Finally, the empty semaphore is incremented by 1, because the consumer has just removed data from an occupied
slot, thus making it empty.
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.
Example:
Let's consider 3 processes P1, P2 and P3, and two types of resources R1 and R2. The
resources are having 1 instance each.
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.
The graph is deadlock free since no cycle is being formed in the graph.
6. Check whether the graph has above any deadlock and explain
step by step.
Answer:
Allocation Need
Processes
R1 R2 R1 R2
P1 1 0 0 1
P2 0 1 1 0
P3 0 1 0 0
Available = [ R1 R2 ] = [ 0 0 ]
Step-01:
Then,
Available
=[00]+[01]
=[01]
Step-02:
∎With the instances available currently, only the requirement of the process P1 can be satisfied.
∎So, process P1 is allocated the requested resources.
∎It completes its execution and then free up the instances of resources held by it.
Then-
Available
=[01]+[10]
=[11]
Step-03:
∎With the instances available currently, the requirement of the process P2 can be satisfied.
∎So, process P2 is allocated the requested resources.
∎It completes its execution and then free up the instances of resources held by it.
Then-
Available
=[11]+[01]
=[12]
Thus,
∎There exists a safe sequence P3, P1, P2 in which all the processes can be executed.
∎So, the system is in a safe state.
[P.T.O.]
7. Explain Banker’s Algorithm.
Answer:
Banker’s Algorithm:
The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that
tests for safety by simulating the allocation for 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 reason behind the name ‘banker’s algorithm’ is that it is mostly used in banking
systems. Banker’s algorithm helps to identify whether a loan should be provided or not.
The characteristics of Banker’s algorithm are:
∎ If a process demands the resources, then it has to wait.
∎ Banker’s algorithm consists of advanced features for maximum resource allocation.
∎ In the banker’s algorithm, various resources are maintained that fulfill the needs of at least
one client.
∎ In the system, we have limited resources.
∎ In the banker’s algorithm, if the process gets all the needed resources, then it is must to return
the resources in a restricted period.
There are four types of data structures used to implement Banker’s algorithm:
∎Available
∎Max
∎Allocation
∎Need
Process means any program is in execution. Process control block controls the operation
of any process.
Thread:
Thread is the segment of a process means a process can have multiple threads and these
multiple threads are contained within a process.
The differences between thread and process are given in the below table:
Thread Process
Thread means segment of a process. Process means any program is in execution.
Thread takes less time to terminate Process takes more time to terminate.
It takes less time for creation and context It takes more time for creation and context
switching switching
Thread is more efficient in term of Process is less efficient in term of communication.
communication.
Thread consumes fewer resources. Process consumes more resources.
Threads share memory. Process is isolated.
Thread switching does not require calling an Process switching uses interface in operating
operating system and causing an interrupt to system.
the kernel.
Second thread in the same task could run, If one server process is blocked no other server
while one server thread is blocked. process can execute until the first process
unblocked.
Thread has Parents’ PCB, its own Thread Process has its own Process Control Block, Stack
Control Block and Stack and common and Address Space.
Address space.
If we open too many tabs at once, it quickly uses up your RAM. This doesn’t leave much
room for other things we want to do with our PC. Browser extensions, called add-ons, can also
put a lot of load on our computer’s processor.
10. Explain Amdahl's law.
Answer:
Amdahl's law is a formula which gives the theoretical speedup in latency of the
execution of a task at fixed workload that can be expected of a system whose resources are
improved.
It relates the improvement of the system’s performance with the parts that didn’t perform
well, like we need to take care of the performance of those parts of the systems. This law often
used in parallel computing to predict the theoretical speedup when using multiple processors. It
is named after computer scientist Gene Amdahl (a computer architect from IBM and Amdahl
corporation), and was presented at the AFIPS Spring Joint Computer Conference in 1967. It is
also known as Amdahl’s argument.
Formula:
Amdahl’s Law can be expressed in mathematically as follows −
SpeedupMAX = 1/ ((1-p) + (p/s))
SpeedupMAX = maximum performance gain
s = performance gain factor of p after implement the enhancements.
p = the part which performance needs to be improved.