Assignment-1
Assignment-1
Operating System
Assignment-1
Deadline: 14-03-2025
Q. 1 What is a race condition? Give an example and simulate with C programming code.
Q. 2 Consider the resource allocation graph for set of three processes P1, P2,and P3 and
resources R1 (one instance ), R2( two instances) and R3 (one instance) as given below.
identify whether the system is in safe state or the system is in deadlocked. (mention the
execution sequence).
Q. 3 Consider the following set of processes, with the length of the CPU burst given in
milliseconds:
i. Find out turnaround time for each process using the Non-Pre-emptive Priority
Scheduling (a smaller priority number implies a higher priority)?
ii. Find out turnaround time for each process using Pre-emptive Priority Scheduling
(a smaller priority number implies a higher priority)?
iii. Find out turnaround time for each process using Non-Pre-emptive Shortest Job
First Time?
iv. Find out turnaround time for each processes using Pre-emptive Shortest Job First
Time (or Shortest-Remaining-Time-First)?
Q. 4 Consider three concurrently executing threads in the same process using two
semaphores s1 and s2. Assume s1 has been initialized to 1, while s2 has been initialized to 0.
What are the possible values of the global variable x, initialized to 0, after all three threads
have terminated?
Department of Computer Science and Engineering
School of Computing & Information Technology
/* thread A */
P(&s2);
P(&s1); x = x*2;
V(&s1);
/* thread B */
P(&s1);
x = x*x;
V(&s1);
/* thread C */
P(&s1);
x = x+3;
V(&s2);
V(&s1);
Q. 6 What could the output of the concurrent execution of process A and process B be? (State
all possible outputs).
int x=0;
int y=0; “initialization”
Process A Process B
while(x==0) {do-nothing}; printf(“b”);
printf(“a”); x=1;
y=1; while(y==0) {do-nothing};
y=0; printf(“c”);
Department of Computer Science and Engineering
School of Computing & Information Technology
printf(“d”);
y=1;
Q. 7 A resource allocation system that uses the Banker’s algorithm for 3 resource types (A,
B, C) and 5 users (P0, P1, P2, P3 P4) is currently in the following state. (Alloc: resources
held by each user. Max: max need of each user. Req: ongoing request of each user. Avail:
free resources.)
Allocation Max Req. Avail
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 2 2 3 3 2
P1 2 0 0 3 2 2 0 2 1
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 0
P4 0 0 2 4 3 3 2 3 0
i. What is the content in need matrix.
ii. Is the state safe. If you answer yes, give a sequence of process ids that leads to all
processes completed. If you answer no, give a sequence of activities that results in
a deadlocked state.
Q. 8 Write the syntax and explain the use of following system calls:
Fork, exec, wait, exit, read, write, open, dup, dup2.
Q. 10 The Sleeping-Barber Problem:- There is a barber shop with one barber and a number of
chairs for waiting customers. Customers arrive at random times and if there is an available
chair, they take a seat and wait for the barber to become available. If there are no chairs
available, the customer leaves. When the barber finishes with a customer, he checks if there
are any waiting customers. If there are, he begins cutting the hair of the next customer in the
queue. If there are no customers waiting, he goes to sleep.
The problem is to write a program that coordinates the actions of the customers and the
barber in a way that avoids synchronization problems, such as deadlock or starvation.
Department of Computer Science and Engineering
School of Computing & Information Technology
Q. 11 A system has four processes and five allocatable resources. The current allocation and
maximum needs are as follows:
Allocated Maximum Available
Process A 10211 11212 00x11
Process B 20110 22210
Process C 11010 21310
Process D 11110 11221
Q. 13 Two processes, A and B, each need three records, 1, 2. And 3, in a database. If A asks
for them in the order 1, 2, 3, and B asks for them in the same order, deadlock is not possible.
However, if B asks for them in the order 3, 2, 1, then deadlock is possible. With three
resources, there are 3! or 6 possible combinations each process can request the resources.
What fraction of all the combinations is guaranteed to be deadlock free?
Q. 14 The Cigarette-Smokers Problem. Consider a system with three smoker processes and
one agent process. Each smoker continuously rolls a cigarette and then smokes it. But to roll
and smoke a cigarette, the smoker needs three ingredients: tobacco, paper, and matches. One
of the smoker processes has paper, another has tobacco, and the third has matches. The agent
has an infinite supply of all three materials. The agent places two of the ingredients on the
table. The smoker who has the remaining ingredient then makes and smokes a cigarette,
signaling the agent on completion. The agent then puts out another two of the three
ingredients, and the cycle repeats. Write a program to synchronize the agent and the smokers.
3. Propose a pseudocode solution or flowchart for a system that allows both multiple
readers and multiple writers while ensuring fairness.
4. Discuss real-world applications where this problem occurs and how modern systems
(such as databases) handle concurrent read and write operations.
5. Explain starvation in the Reader-Writer Problem and propose methods to prevent
starvation for both readers and writers.