You are on page 1of 4

Assignment no 2

CS1002 – Operating system

Submitted by:Warisha Malik


Roll No. : 64 bscs
Class Section: Bscs

“On my honor, as student of Sir Syed CASE Institute Islamabad, I have neither given nor
received unauthorized assistance on this academic work.”

Submitted to:
Sir Waqas
DATE
For the following cases explain clearly how mutual exclusion and
bounded waiting property can be satisfied for 4 processes(P0, P1,
P2, P3).
1) if P0 want to access critical section again and again and no
other process access critical section.
Solution:
In this case, mutual exclusion and bounded waiting properties will be satisfied for
all processes. Since P0 is the only process accessing the critical section, it will not
have to wait for any other process to release the lock. Additionally, since no other
process is accessing the critical section, there is no possibility of any process
waiting indefinitely, thus the bounded waiting property is also satisfied.

2) if P2 access critical section first and execute their for a while,


what happened to all other processes if
they also want to access critical section. clearly explain (entry
section of code) your argument with the help of algorithm variables
state and conditions.
Solution:
If P2 accesses the critical section first, then all other processes (P0, P1, P3) will
have to wait until P2 releases the lock. In the entry section of the code, when any
process (let's say P1) wants to access the critical section, it will set its waiting flag
to true and try to acquire the lock using TestAndSet(). If the lock is already held by
P2, P1 will enter into the while loop and wait until the lock is released by P2. Thus,
the bounded waiting property is satisfied since P1 will not wait indefinitely.
Similarly, when P0 or P3 try to access the critical section, they will also wait for the
lock to be released by P2.
3) if P2 access critical section first and all other processes in busy
loop of algorithm, explain (exit section of code) how
other processes will get access to critical section if P2 want to leave
from critical section. Assume one by one they all
exit from code and never return to entry section of code.
Solution:
In this scenario, if P2 wants to leave the critical section, it will set the lock variable
to false and check if any other process is waiting in the waiting queue. If no other
process is waiting, then the critical section will be available for any process to
acquire the lock. However, if another process (let's say P1) is waiting, then P2 will
set P1's waiting flag to false, allowing P1 to enter the critical section. P2 will
continue to execute the remainder section and release the lock. Once P1 finishes
executing the critical section, it will check if any other process is waiting in the
waiting queue. If no process is waiting, it will release the lock and exit. If another
process (let's say P0) is waiting, then P1 will set P0's waiting flag to false and allow
it to enter the critical section. This process will continue until all processes have
entered the critical section and completed their execution.

4) if P2 access critical section first and all other processes in busy


loop of algorithm, can P2 access critical section of code again
after if it execute exit section.
(All conditions should be explain clearly with variables state and
loop conditions (if any involve)).
Solution:
If P2 executes the exit section and releases the lock, it can access the critical
section again only if no other process is waiting in the waiting queue. If another
process (let's say P0) is waiting, P2 will have to wait until P0 finishes its execution
and releases the lock. Once the lock is released, P2 can acquire the lock and enter
the critical section again. However, if P2 wants to access the critical section again
immediately after releasing the lock, it will have to wait until all other processes
have completed their execution and released the lock. Only then can it acquire the
lock and enter the critical section again.

Reference Code From slide 27 :

do {
waiting[i] = TRUE;
key = TRUE;
while (waiting[i] && key)
key = TestAndSet(&lock);
waiting[i] = FALSE;

// critical section
j = (i + 1) % n;
while ((j != i) && !waiting[j])
j = (j + 1) % n;
if (j == i)
lock = FALSE;
else
waiting[j] = FALSE;
// remainder section
} while (TRUE);

You might also like