Mohamed Abdelrahman Anwar - 20011634 - Sheet 5

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Name ID

Mohamed Abdelrahman Anwar 20011634

Operating Systems
Sheet 5
Concurrency: Mutual Exclusion and
Synchronization

5.1 MUTUAL EXCLUSION: SOFTWARE APPROACHES


1.
a. Mutual exclusion refers to the property of a system or
program where only one process or thread can access a
shared resource or a critical section at any given time. This
means that while one process is using a shared resource,
other processes must wait until that resource is released
before they can access it. The purpose of mutual exclusion
is to prevent concurrent access to shared resources, which
can lead to conflicts and inconsistencies in the system.
b. Critical section refers to the part of a program or code
that accesses shared resources or variables that can be
modified by multiple processes or threads. Since the
critical section can cause race conditions and conflicts
when accessed by multiple processes simultaneously, it is
necessary to ensure mutual exclusion in order to avoid
such issues. Therefore, the critical section is usually
protected by locking mechanisms or synchronization
primitives to enforce mutual exclusion and ensure that
only one process can access the critical section at any
given time.
2.
a. False

5.2 PRINCIPLES OF CONCURRENCY


3.
1. A race condition occurs when multiple processes or
threads read and write data items so that the final
result depends on the order of execution of instructions in
the multiple processes.
4.
1. Mutual exclusion must be enforced: Only one process at a
time is allowed into its critical section, among all processes that
have critical sections for the same resource or shared object.
2. A process that halts in its noncritical section must do so
without interfering with other processes.
3. It must not be possible for a process requiring access to a
critical section to be delayed indefinitely: no deadlock or
starvation.
4. When no process is in a critical section, any process that
requests entry to its critical section must be permitted to enter
without delay.
5. No assumptions are made about relative process speeds or
number of processors.
6. A process remains inside its critical section for a finite time
only
5.3 MUTUAL EXCLUSION: HARDWARE SUPPORT
5.
• Busy waiting is employed: Thus, while a process is waiting
for access to a critical section, it continues to consume
processor time.
• Starvation is possible: When a process leaves a critical
section and more than one process is waiting, the
selection of a waiting process is arbitrary. Thus, some
process could indefinitely be denied access.
• Deadlock is possible: Consider the following scenario on a
single-processor system. Process P1 executes the special
instruction (e.g., compare&swap, exchange) and enters its
critical section. P1 is then interrupted to give the processor
to P2, which has higher priority. If P2 now attempts to use
the same resource as P1, it will be denied access because
of the mutual exclusion mechanism. Thus, it will go into a
busy waiting loop. However, P1 will never be dispatched
because it is of lower priority than another ready process,
P2.

5.4 SEMAPHORES
6. One difference is in second definition s.count initial value
expresses number of processes that can be on the ready list,
when s.count is negative it gives number of processes waiting,
this information cannot be obtained from the first definition
but both definitions are equivalent in functionality.
General questions
7.
a. The lower bound on the final value of tally is n, which is
the number of times that tally is incremented by each
process. Each process runs a loop that increments tally n
times, so the total number of increments is 2n. Therefore,
The upper bound on the final value of tally is 2n.
b. Allowing an arbitrary number of processes to execute in
parallel under the assumptions of part (a) would increase
the upper bound on the final value of tally. Specifically, if
there are k processes executing in parallel, then each
process would increment tally n times, resulting in a total
of kn increments. Therefore, the upper bound on the final
value of tally would be kn, which is greater than the upper
bound of 2n in part (a).
However, the lower bound on the final value of tally would
remain the same, since each process still increments tally n
times. Therefore, the range of final values of tally would be [n,
kn], where k is the number of processes executing in parallel.
The exact final value of tally would depend on the relative
speeds of the processes and the order in which they execute.

8.
The proposed solution is an implementation of the Peterson's
algorithm for two processes, which uses a shared turn variable
and per-process blocked flags to enforce mutual exclusion.
However, there is a problem with the inner while loop:
while (blocked[1-id]) /* do nothing */;

This loop is intended to ensure that the other process is not


blocked before entering the critical section. However, it does
not include any yield or sleep statements, so it can potentially
consume all available CPU time and prevent the other process
from making progress.
Here is a counterexample that demonstrates the problem with
this solution:
boolean blocked [2];

int turn;

void P (int id) {

while (true) {

blocked[id] = true;

while (turn != id) {

while (blocked[1-id]) /* do nothing */;

turn = id;

/* critical section */ printf("Process %d in critical section\n", id);

/* let the other process run */

blocked[id] = false;

yield();

void main() {

blocked[0] = false;

blocked[1] = false;

turn = 0;

parbegin (P(0), P(1));

You might also like