0% found this document useful (0 votes)
5 views8 pages

CS 4348 Assignment 3

The document discusses various concepts related to concurrency, mutual exclusion, semaphores, deadlock, and resource management in operating systems. It outlines conditions for mutual exclusion, types of semaphores, and the Readers/Writers problem, along with control problems like deadlock and starvation. Additionally, it explains strategies for deadlock prevention and avoidance, resource allocation, and provides examples of reusable and consumable resources.

Uploaded by

lisastansel07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

CS 4348 Assignment 3

The document discusses various concepts related to concurrency, mutual exclusion, semaphores, deadlock, and resource management in operating systems. It outlines conditions for mutual exclusion, types of semaphores, and the Readers/Writers problem, along with control problems like deadlock and starvation. Additionally, it explains strategies for deadlock prevention and avoidance, resource allocation, and provides examples of reusable and consumable resources.

Uploaded by

lisastansel07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Assignment 3

Question 1

The first condition for mutual exclusion is that 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. The second condition for mutual exclusion is
that a process that halts in its noncritical section must do so without interfering with other
processes. The third condition for mutual exclusion is it must not be possible for a process
requiring access to a critical section to be delayed indefinitely - meaning no deadlock or
starvation. The fourth condition is that when no process is in a critical section, any process that
requests entry to its critical section must be permitted to enter without delay. The fifth condition
is that no assumptions are made about relative process speeds or number of processors. The
sixth condition is that a process remains inside its critical section for a finite time only.

Question 2

A general semaphore is an integer value used for signaling among processes. A general
semaphore has a zero or positive value. These are the operations defined on a general
semaphore: a semaphore may be initialized to a nonnegative integer value; the semWait
operation decrements the semaphore value and if the value becomes negative the process
executing the semWait is blocked (otherwise continues execution); the semSignal operation
increments the semaphore value, and if the resulting values is less than or equal to zero the
process blocked by a semWait is unblocked (if there are any). In contrast, a binary semaphore
can only take on the values 0 and 1. These are the operations defined on a binary semaphore:
a binary semaphore can be initialized to 0 or 1; semWaitB operation checks the semaphore
value and if it is zero, the process executing the semWaitB is blocked and if it one, the value is
changed to zero and the process continues execution; the semSignalB operations checks if any
processes are blocked on this semaphore and if so, a process blocked by semWaitB is
unblocked and if no processes are blocked, the value of the semaphore is set to one.

Question 3

For a strong semaphore, processes are unblocked in the order they were blocked, in a first in
first out (FIFO) manner. The process that has been blocked the longest is released from the
queue first. Strong semaphores have a queue which adheres to FIFO policy. Weak semaphores
do not specify the order in which processes are unblocked. Mutual exclusion algorithms often
use strong semaphores as they guarantee freedom from starvation, while weak semaphores do
not.

Question 4

With respect to messages, both the sender and receiver can be blocking or nonblocking. When
a send primitive is executed, there are two possibilities: either the sending process is blocked till
the message is received, or it is not. For the receive primitive there are also two possibilities: if a
message has previously been sent, the message is received and execution continues, or there
is no waiting message. In that case, the process is either blocked until a message arrives or the
process continues to execute, abandoning the attempt to receive. Three common combinations
exist: blocking send, blocking receive; nonblocking send, blocking receive; nonblocking send,
nonblocking receive. Blocking send, blocking receive means both the sender and receiver are
blocked until the message is delivered. Nonblocking send, blocking receive means the sender
can continue, but the receiver is blocked until the requested message arrives. Nonblocking
send, nonblocking receive means neither party is required to wait. Nonblocking send is more
natural for concurrent programming tasks, while blocking receive is more natural.

Question 5

The first context in which concurrency arises is in multiple applications. Multiprogramming was
invented to allow processing time to be dynamically shared among many active applications. An
example of this is if we have one application, or program, which is reading input from the user
and another application which is in parallel printing output to a log file from previous
computations. The second context is in structured applications. As an extension of the principles
of structured programming and modular design, some applications are able to be effectively
programmed as a set of concurrent processes. An example of this is if we have one driver
program which performs computations in one process while also printing output to the console
in another process. The third context is operating system structure. The same structuring
advantages mentioned previously apply to system programs, and the operating system is often
implemented as a set of threads or processes. An example of this is if we have multiple tabs in
a browser which can be different threads according to the operating system.

Question 6

The first degree of awareness is when processes are unaware of each other. This is a
competition relationship and the results of one process are independent of the action of others.
The timing of processes may be affected. Potential problems include mutual exclusion,
deadlock, and starvation. The second degree of awareness is if processes are indirectly aware
of each other, such as through a shared object. This is a cooperation by sharing relationship.
The results of one process may depend on information obtained from other processes. The
timing of a process may be affected. Potential problems include mutual exclusion, deadlock,
starvation, and data coherence. The third degree of awareness is processes are directly aware
of each other (they have communication primitives available to them). This is a cooperation by
communication relationship. The results of one process may depend on information obtained
from others. The timing of processes may be affected. Potential problems include deadlock and
starvation.
Question 7

One control problem is mutual exclusion. Suppose two processes require access to a
nonsharable resource. It is important that only one program is allowed in its critical section, or
the portion of the program that uses the critical resource. We cannot simply rely on the OS to
understand and enforce this restriction as the detailed requirements may not be obvious. The
second control problem is deadlock. Suppose we have a situation with two processes and two
resources. If the OS assigns resource one to process 2 and resource 2 to process one, but both
processes need both resources, each process is waiting for one of the two resources and
neither will release their resource it owns until it acquires the other. This can lead to deadlock.
The third control problem is starvation. Starvation can occur when one process has been
indefinitely denied access to the critical resource, even though there is no deadlock situation.
For example, suppose we have three processes which each need periodic access to the
resource. If there is a situation where process 1 and process 3 continuously access the
resource, process 2 may be starved.

Question 8

The first condition associated with the Readers/Writers problem is that any number of readers
may simultaneously read the file. The second condition is that only one writer at a time may
write to the file. The third condition is that if a writer is writing to the file, no reader may read it.

Question 9

(a) A monitor

Monitor RandomNumber {

Int randomNumber;
Condition empty, full;

Procedure GenerateRandomNumber() {
if (randomNumber is not empty) {
cwait(empty);
}
randomNumber = createARandomNumber();
csignal(full);
}

Procedure UseRandomNumber(pid) {
If (randomNumber is empty) {
cwait(full);
}
Use the randomNumber;
randomNumber = empty;
csignal(empty);
}
}

Process0() {
Int count = 0;
While (count <= 10) {
RandomNumber.GenerateRandomNumber();
count++;
}
}

Process1-10() {
For (pid from 1 to 10) {
RandomNumber.UseRandomNumber(pid);
}
}

(b) Messages

Process0() {
Int randomNumber = empty;

For (int pid = 1; pid <= 10; pid++) {


randomNumber = createARandomNumber();

send(pid, randomNumber);
randomNumber = empty;
}
}

Process1-10() {
Int randomNumberReceived;

receive(process0_id , randomNumberReceived);

Use randomNumberReceived;
}
Question 10

The three conditions which must be present for deadlock to occur are mutual exclusion, hold
and wait, and no preemption. Mutual exclusion refers to only one process being able to use a
resource (or instance of a resource) at a time. Hold and wait refers to a process being allowed
to hold allocated resources while waiting for another. No preemption refers to no resource being
able to be forcefully taken from a process.

Question 11

The fourth condition which can create deadlock is circular wait. This is when a closed chain of
processes exists, such that each process holds at least one resource needed by the next
process in the chain. The fourth condition is different from the other three because it is a
potential consequence of the first three. Given that the first three conditions exist, a sequence of
events may occur that lead to an unresolvable circular wait. This is the definition of deadlock. It
is unresolvable as the first three conditions hold.

Question 12

The hold-and-wait condition can be prevented by requiring that a process request all required
resources at one time and blocking the process until all requests can be granted at one time.
However, this is an inefficient approach. A process may be held up for a long time waiting for all
resource requests to be filled and resources allocated to a process may remain unused for a
period of time which is undesirable.

Question 13

The first way the no preemption condition can be prevented is if a process holding certain
resources is denied a further request, that process must release its original resources and if
necessary, request them again together with the additional resource. The second way is if a
process requests a resource currently held by another process, the OS may preempt the
second process and require it to release its resources. This only works if no two processes
share the same priority.

Question 14

The circular wait condition can be prevented by defining a linear ordering of resource types. If a
process has been allocated resources of type R, then it may subsequently request only those
resources of types following R in the ordering. However, this may be inefficient and
unnecessarily slow down processes and deny resource access.
Question 15

Deadlock avoidance allows mutual exclusion, hold and wait, and no preemption, but makes
choices to ensure deadlock does not happen. Avoidance allows more concurrency than
deadlock prevention does. A decision is made dynamically whether the current resource
allocation request will, if granted, lead to a deadlock potentially. Deadlock avoidance requires
knowledge of future resource requests. Deadlock detection involves periodically checking for
deadlock. If a deadlock does happen, then it will take steps to recover from it. With deadlock
detection, requested resources are granted to processes whenever possible. It is like the
opposite of deadlock prevention. Deadlock prevention means designing a system such that
deadlock cannot happen. Indirect deadlock prevention is preventing mutual exclusion, hold and
wait, or no preemption. Direct deadlock prevention is preventing circular wait. Deadlock
prevention means constraining resource requests to prevent at least one of the four conditions
of deadlock.

Question 16

One integrated deadlock strategy approach is to group resources into a number of different
resource classes. Then use the linear ordering strategy defined previously for prevention of
circular wait to prevent deadlock. Within a resource class, use the algorithm which is most
appropriate for that class. An integrated deadlock strategy is swappable space: prevention of
deadlocks by requiring that all the required resources that may be used be allocated at one
time. Another is process resources: avoidance because it is reasonable to expect processes to
declare ahead of time the resources that they will require in this class. Another integrated
deadlock strategy is main memory: prevention by preemption is the most appropriate strategy
for main memory. When a process is preempted, it is swapped to secondary memory, freeing
space to resolve the deadlock. Another integrated deadlock strategy is internal resources:
prevention by means of resource ordering can be used here.

Question 17

Examples of reusable resources include the processors, I/O channels, main and secondary
memory, devices and data structures (such as field, databases, and semaphores). Examples of
consumable resources include interrupts, signals, messages/network packets, and information
in I/O buffers.

Question 18

If process 2 requests resource B, the operating system will accept the request.

When running the resource allocation denial algorithm the following are the results:

Initial State -
C={11111 A={10000 C-A={01111
01101 00100 01001
10001 00001 10000
21010 11000 10010
01210 00000 01210
00100} 00100} 00000}

R={32211} V={11010}

Process 2 Requests Resource B -

C={11111 A={10000 C-A={01111


01101 01100 00001
10001 00001 10000
21010 11000 10010
01210 00000 01210
00100} 00100} 00000}

R={32211} V={10010}

Select Process 3 to Run -


1<=1 and 0<=0 and 0<=0 and 0<=1 and 0<=0
V={10011}

Select Process 2 to Run -


0<=1 and 0<=0 and 0<=0 and 0<=1 and 1<=1
V={11111}

Select Process 1 to Run -


0<=1 and 1<=1 and 1<=1 and 1<=1 and 1<=1
V={21111}

Select Process 4 to Run -


1<=2 and 0<=1 and 0<=1 and 1<=1 and 0<=1
V={32111}

Select Process 6 to Run -


0<=3 and 0<=2 and 0<=1 and 0<=1 and 0<=1
V={32211}

Select Process 5 to Run -


0<=3 and 1<=2 and 2<=2 and 1<=1 and 0<=1
V = { 3 2 2 1 1}

All processes were able to complete successfully, so Process 2’s request for Resource B will be
accepted.

You might also like