You are on page 1of 8

EASTPOINT COLLEGE OF ENGINEERING AND TECHNOLOGY

DEPARTMENT OF CSE
ACADEMIC YEAR JUNE_SEPT_2023
INTERNAL ASSESSMENT TEST-II
SCHEME AND SOLUTION

SEMESTER: IV A & B SUBJECT: OS


SUBJECT CODE: 21CS44
Q. MARKS
SOLUTION
NO. ALLOCATED
Q.1.a Consider a system consisting of n processes {P0, P1, ..., Pn }.Each process has a 02+03=5M
segment of code, called a critical section, in which a process may be changing
common variables, updating a table or writing a file.
General structure of a typical process has following (as shown in fig):

A solution to the critical-section problem must satisfy the following three


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
processes wish to enter their critical sections, then only those processes that
are not executing in their remainder sections can participate in the decision
on which will enter its critical section next, and this selection cannot be
postponed indefinitely.
 Bounded waiting. There exists a bound, or limit, on the number of times
that other processes are allowed to enter their critical sections after a
process has made a request to enter its critical section and before that
request is granted.

b 2.5*2=5M

PAGE:
Subject Code:21CS44 Subject: Operating System
Q. MARKS
SOLUTION
NO. ALLOCATED
Q.2.a Schematic View of Monitor: A monitor type presents a set of programmer-defined 02+03=5M
operations that are provided mutual exclusion within the monitor. The monitor type
contains the declaration of variables whose values define the state of an instance of
that type and the bodies of procedures or functions that operate on those variables.
The representation of a monitor type cannot be used directly by the various
processes. Thus, a procedure defined within a monitor can access only those
variables declared locally within the monitor and its formal parameters.

Solution to Dinning Philosopher Problem


 Three states in which we may find a philosopher: Therefore the following
data structure: enum {thinking, hungry, eating} state[5] ;
 Philosopher i can set the variable state[i] = eating only if her two neighbors
are not eating: ( state [(i+4) %5] != eating) and( state [(i+1) % 5] != eating).
 Declare condition self [5],where philosopher i can delay herself when she is
hungry but is unable to obtain the chopsticks she needs.
 Each philosopher, before starting to eat, must invoke the operation
pickup( ).
 This may result in the suspension of the philosopher process
 After the successful completion of the operation, the philosopher may
eat.Following this, the philosopher invokes the putdown( ) operation.
 Thus, philosopher i must invoke the operations pickup( ) and putdown( ) in
the following sequence:
dp.pickup(i); eat dp.putdown(i);
Deadlock: - A set of processes {P1, P2, [3,….P n} are in deadlock state, if and only
b 1+4=5M
if every process in the set is waiting for a resource that is held by another process in
the set.

PAGE:
Subject Code:21CS44 Subject: Operating System
Q. MARKS
SOLUTION
NO. ALLOCATED
Necessary Conditions
A deadlock situation can arise if the following four conditions hold simultaneously
in a system:
1. Mutual exclusion:
At least one resource must be held in a non-sharable mode; that is, only one
process at a time can use the resource. If another process requests that resource,
the requesting process must be delayed until the resource has been released.
2) Hold and Wait A process must be holding at least one resource and waiting to
acquire additional resources that are currently being held by other processes.
3) No Preemption Resources cannot be preempted. that is, a resource can be
released only voluntarily by the process holding it, after that process has completed
its task
4) Circular Wait A set of processes { P0, P1, P2, . . ., P n } waiting must exist such
that P0 is waiting for a resource that is held by P1 ,P1 is waiting for a resource that
is held by P2, ….Pn-1 is waiting for a resource held by P n, and Pn, is waiting for a
resource held by P0.
Case I : Cycle with Deadlock with all Resource Types having only 1-instance.
3.a
2.5*2=5M

Case 2:Cycle with No Deadlock with at least one Resource type having
multiple instances.
consider the resource-allocation graph in Figure
In this example, we also have a cycle P1 → R1 → P3→ R2 → P1

• However, there is no deadlock.


• Observe that process P4 may release its instance of resource type R2.
• That resource can then be allocated to P3, breaking the cycle

PAGE:
Subject Code:21CS44 Subject: Operating System
Q. MARKS
SOLUTION
NO. ALLOCATED
b.  Assumptions:
2+3=5M
Let n = number of processes in the system
Let m = number of resources types.
• Following data structures are used to implement the banker‘s algorithm.
1) Available: - A vector of length m, indicates the no. of available resources of
each type. - If Available[j]=k, then k instances of resource type Rj is
available.
2) Max - an n X m matrix indicates the maximum demand of each process of
each resource. - If Max[i][j]=k, then process Pi may request at most k
instances of resource type Rj.
3) Allocation - an n X m matrix ,indicates no. of resources currently allocated
to each process. - If Allocation[i][j]=k, then Pi is currently allocated k
instances of Rj.
4) Need - an n X m matrix, indicates the remaining resources need of each
process. -If Need[i][j]=k, then Pi may need k more instances of resource Rj
to complete its task.
- So, Need[i][j] = Max[i][j] - Allocation[i][j]
Safety Algorithm

Algorithm to verify whether the system is safe (or) not after allocating the requested
resource
1. Let Work and Finish be vectors of length m and n, respectively. Initialize
Work = Available and Finish[i] =false for i= 0,1, ..., n-l.
2. Find an i such that both
a. Finish[i] ==false
b. Needi ≤ Work
If no such i exists, go to step 4.
3. Work = Work + Allocation, Finish[i] = true Go to step 2.
4. If Finish[i] = true for all. i then the system is in a safe state.

PAGE:
Subject Code:21CS44 Subject: Operating System
Q. MARKS
SOLUTION
NO. ALLOCATED

Max Allocation Need Available 3+4+4=10M


4
Process A B C A B C A B C A B C
P0 0 0 4 0 0 2 0 0 2 1 0 2
P1 2 0 1 1 0 0 1 0 1
P2 1 3 7 1 3 5 0 0 2
P3 8 4 2 6 3 2 2 1 0
P4 1 5 7 1 4 3 0 1 4

Step 1: Work=Available=1 0 2
Finish[i]=False for i=0,1,2,3…n
Step 2: Finish[i]=Finish[0]=P0==False True
Need[P0]<=work, 0 0 2<=1 0 2 True //(P0 Completes Execution
and Releases Resources)
Step 3: Work=work+ Allocation[P0]=1 0 2 + 0 0 2=1 0 4 Finish[i]=P0=True

Step 2: Finish[i] =Finish [1]=P1==False True


Need [P1] <=work
1 0 1 <=1 0 4 //(P1 Completes Execution and Releases Resources)
Step 3: Work=work+ Allocation [P1]=1 0 4+1 0 0 =2 0 4 Finish[i]=P2=True

Step 2: Finish[i]=Finish[2]=P2==False True


Need[P2]<=work
0 0 2 <=2 0 4 True //(P2 Completes Execution and Releases Resources)
Step 3: Work=work+ Allocation[P2]=2 0 4 + 1 3 5=3 3 9 Finish[i]=P2=True

Step 2:Finish[i]=Finish[3]=P3==False True


Need[P3]<=work
2 1 0 <=3 3 9 true //(P3 Completes Execution and Releases Resources)
Step 3: Work=work+ Allocation[P3]=3 3 9 + 6 3 2=9 6 11 Finish[i]=P3=True

Step 2:Finish[i]=Finish[4]=P4==False True


Need[P4]<=work
0 1 4 <= 9 6 11 true //(P4 Completes Execution and Releases Resources)
Step 3: Work=work+ Allocation[P4]=9 6 11+ 1 4 3 =10 10 14 finish[i]=P4=True

Step 4: Finish[i]=True for 0<=i<=4


Hence the system is in a safe state : Safe Sequence <P0 , P1,P2, P3 ,P4>

PAGE:
Subject Code:21CS44 Subject: Operating System
Q. MARKS
SOLUTION
NO. ALLOCATED
If a request from p2 arrives for (0,0,,2)
To decide whether the request is granted, we use resource Request algorithm.

Step 1: Request [P2]<=Need[P2] i.e. (0, 0, 2 )<=( 0 0 2 ) - true.


Step 2: Request [P2]<=Available. (0, 0, 2 )<= ( 1 0 2 ) - true.
Step 3: Available = Available – Request [P2] = (1 0 2 ) - (0 0 2)= 1 0 0
Allocation [P2] = Allocation [P2] + Request [P2] = 1 3 5+0 0 2=1 3 7
Need [P2] = Need [P2] – Request [P2] = 0 0 2-0 0 2=0 0 0
We arrive at the following new system state:

Max Allocation Need Available


Process A B C A B C A B C A B C
P0 0 0 4 0 0 2 0 0 2 1 0 0
P1 2 0 1 1 0 0 1 0 1
P2 1 3 7 1 3 7 0 0 0
P3 8 4 2 6 3 2 2 1 0
P4 1 5 7 1 4 3 0 1 4

P0: Need[P0]<=work
0 0 2<=1 0 0 False
So P0 must wait
P1: Need[P1]<=work
1 0 1<=1 0 0 False
So P1 must wait
P2: Need[P2]<=1 0 0
0 0 0<=1 0 0 True
Work==work+ allocation[P2]=1 0 0+ 1 3 7=2 3 7
P3: Need[P3]<=work
2 1 0 <=2 3 7 True
Work=work+ Allocation[P3]=2 3 7+6 3 2=8 6 9
P4: Need[P4]<=work
0 1 4<=8 6 9 True
Work=work+ Allocation[P4]=8 6 9+1 4 3=9 10 12
P0: Need[P0] <=work
0 0 2<=9 10 12 True
Work=work+ Allocation[P0] =9 10 12+0 0 2=9 10 14
P1: Need[P1] <=work
1 0 1<=9 10 14 True
Work=work+ Allocation[P1] =9 10 14+0 0 2=9 10 16

PAGE:
The Request of P1 will be granted immediately.So safe sequence <P2,P3,P4,P0,P1>
Q. MARKS
SOLUTION
NO. ALLOCATED
5.a.
2+3=5M

 TLB is a small, fast lookup hardware cache, called a translation look-aside


buffer (TLB). The TLB is associative, high-speed memory. Each entry in
the TLB consists of two parts: a key (or tag) and a value. When the
associative memory is presented with an item, the item is compared with all
keys simultaneously.
 When a logical address is generated by the CPU, its page number is
presented to the TLB. If the page number is found, its frame number is
immediately available and is used to access memory. The whole task may
take less than 10 percent longer than it would if an unmapped memory
reference were used. If the page number is not in the TLB (known as a TLB
miss), a memory reference to the page table must be made. When the frame
number is obtained, we can use it to access memory.
 In addition, the page number and frame number will be added to the TLB,
so that they will be found quickly on the next reference. if the TLB is
already full of entries, the operating system must select one for replacement.
Replacement policies range from least recently used (LRU) to random.
b A common approach for handling address spaces larger than 32 bits is to use a
hashed page table, with the hash value being the virtual page number. Each entry 2.0*2=5M
in the hash table contains a linked list of elements that hash to the same location.

Each element consists of three fields: (1) the virtual page number, (2) the value of
the mapped page frame, and (3) a pointer to the next element in the linked list.

PAGE:
Q. MARKS
SOLUTION
NO. ALLOCATED
The virtual page number is compared with field 1 in the first element in the
linked list. If there is a match, the corresponding page frame (field 2) is used
to form the desired physical address. if there is no match, subsequent entries
in the linked list are searched for a matching virtual page number.

 Each inverted page-table entry is a pair where the <Process-id, Page


number> where the process-id assumes the role of the address-space
identifier.
 When a memory reference occurs, part of the virtual address, consisting of
<Process-id, Page number>is presented to the memory subsystem. The
inverted page table is then searched for a match. If a match is found—say; at
entry i—then the physical address <i, offset> is generated. If no match
is found, then an illegal address access has been attempted

PAGE:

You might also like