0% found this document useful (0 votes)
3 views

ICT1007-Part1-Tutorial3(Answer)

The document contains a tutorial with questions and answers related to semaphores, deadlock prevention, and avoidance in the context of process synchronization and resource allocation. It discusses the implementation of semaphores in code, the resource allocation graph for processes and resources, and the differences between deadlock prevention and avoidance. Additionally, it mentions the importance of communication with staff regarding assignment progress.

Uploaded by

yuxun19999
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

ICT1007-Part1-Tutorial3(Answer)

The document contains a tutorial with questions and answers related to semaphores, deadlock prevention, and avoidance in the context of process synchronization and resource allocation. It discusses the implementation of semaphores in code, the resource allocation graph for processes and resources, and the differences between deadlock prevention and avoidance. Additionally, it mentions the importance of communication with staff regarding assignment progress.

Uploaded by

yuxun19999
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

ICT1007-Part1-Tutorial 3

1. We have seen this question in tutorial 2. Change the following code, using pipe, to allow the
value in the child to pass back to the parent for printing. The result of printed value should
be 20.

Answer:

From lecture notes: ICT1007-Part1-04


ICT1007-Part1-Tutorial 3

2. Explain the 3 Semaphores used in the bounded Buffer Problem. What are they? How are
they used?

Answer:

• Not_full: used to regulate the production rate of the producer, not to over produce
because of the limited buffer size
• Mutex: to ensure the production and consumption process is atomic (no
interruption of other processes).
• Not_empty: used to facilitate and notify the consumer in consuming the produced
item
• Lecture notes: ICT1007-Part1-6
ICT1007-Part1-Tutorial 3

3. Using semaphores with wait() and signal() functions, design a code for Process 1 and 2 (P1
and P2) with the respective code segments shown as c1 to c8. You should use one or more
semaphores to ensure the fulfilment of the condition where c2 must execute before c6 and
c8 must execute before c4. Clearly define the initial value of the semaphores.
Is there a possibility to design with just a single semaphore? If no, explain the reason. If yes,
show the code.
P1 { c1; c2; c3; c4; }
P2 { c5; c6; c7; c8; }

Answer:
s1=0; s2=0;
P1 { c1; c2; signal(s1); c3; wait(s2); c4; }
P2 { c5; wait(s1); c6; c7; c8; signal(s2); }
A single semaphore:
s1=0;
P1 { c1; c2; signal(s1); c3; wait(s1); c4; }
P2 { c5; wait(s1); c6; c7; c8; signal(s1); }
There is a possibility where wait(s1) in P2 run after the wait(s1) in P1. In this case P1 will
complete and P2 will be blocked forever at wait(s1);
Lecture slides: ICT1007-Part1-07

4. There are 4 processes (p1,p2,p3,p4) and 2 resources (r1,r2). Each resource has 2 instances.
Process 1 is currently using/holding an instance of r2. Another instance of r2 is held by P4.
P2 is using an instance of r1. P3 is holding another instance of r1. To complete P1 it requires
an instance of r1. P3 requested for an instance of r2 before it can complete.
ICT1007-Part1-Tutorial 3

• Draw the resource allocation graph for the above case.


• Determine whether is there a deadlock in the graph. If no, demonstrate with a trace
of the sequence of execution for all processes.
• What would happen if both P2 and P4 halted in their execution and are not able to
complete the execution?

Answer:

o There is no deadlock.
o One possible sequence of execution is P2, P1, P3, P4
o It would end up with a deadlock if P2 or P4 can’t finish, since both P1 and P3 can
never complete without the additional resources of r1 and r2. Since they can’t
complete, they also can’t release their resources.
o Lecture slides notes: ICT1007-Part1-07

5. What are the differences between Deadlock Prevention and Deadlock Avoidance?
ICT1007-Part1-Tutorial 3

Answer:

• Deadlock prevention is the mechanism to ensure that at least one of the necessary
conditions for deadlock can never occur. On the other hand, deadlock avoidance is the
mechanism to ensure that the system does not enter an unsafe state. Thus, this is the
main difference between deadlock prevention and deadlock avoidance.
• In deadlock prevention, the system does not require information of the existing
resources, resource availability and resource requests whereas, in deadlock avoidance,
the system requires information on the existing resources, resource availability and
resource requests to find whether the system is in a safe or unsafe state. Hence, this is
another difference between deadlock prevention and deadlock avoidance.
• Non-blocking synchronization algorithms and serializing tokens are some deadlock
prevention algorithms while Banker’s algorithm is the most common deadlock
avoidance algorithm.
• Moreover, the state of resources is an important difference between deadlock
prevention and deadlock avoidance. In deadlock prevention, all resources are requested
at once while, in deadlock avoidance, the requests for resources are manipulated until at
least one safe path is found.
• In brief, deadlock is a situation that occurs due to a set of processes in which each
process holds a resource and waits to acquire a resource held by another process in the
set. Deadlock prevention and deadlock avoidance are two mechanisms related to
deadlocks. The main difference between deadlock prevention and deadlock avoidance is
that the deadlock prevention ensures that at least one of the necessary conditions to
cause a deadlock will never occur, while deadlock avoidance ensures that the system will
not enter an unsafe state.
• Lecture notes: ICT1007-Part1-07

6. Mid demonstration of the progress of your assignment.


• Update the staff handling your tutorial the progress of your assignment.
• Clarify any doubt if you have.

You might also like