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

Quiz2 2022 Modified With Solns

Uploaded by

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

Quiz2 2022 Modified With Solns

Uploaded by

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

I N D I A N I N S T I T U T E O F I N F O R M AT I O N T E C H N O L O G Y, A L L A H A B A D

Quiz 2

Paper Title: Operating System Max Marks: 50 Duration: 1 Hour 30 minutes

-------------------------------------------------------------------------------------------------------------------------------
Note: There are six questions in this question paper covering two pages.
Answer All questions. The marks for rest of the questions have been provided alongside each question.
------------------------------------------------------------------------------------------------------------------------------

Q1: Answer the following with a brief explanation. [5 marks]

(i) (T/F) Switching among threads in the same process is more efficient than switching among processes.
T : Threads share address space

(ii) (T/F) Consider N threads in a process. If one thread passes certain arguments to a function in the
program, these arguments are visible to the other threads?
F : Function arguments are stored on stack and threads do not share stack

(iii) (T/F) Two sibling processes communicate using a pipe?


T : Parent creates a pipe and then forks twice. Both children inherit file descriptors.

(iv) (T/F) Priority Inheritance is a scheme to circumvent priority inversion where the higher priority process
temporarily assumes a lower priority.
T:
Priority inversion : a low-priority thread holds a lock(mutex), blocking a high-priority thread. Due to its low
priority, the lock(mutex) owner may hold the lock(mutex) for an unbounded duration. As a result, it becomes
impossible to guarantee thread deadlines.

Priority inheritance : the lock(mutex) holder inherits the priority of the highest-priority blocked thread. When a
thread tries to lock a lock(mutex) using this protocol and is blocked, the lock(mutex) owner temporarily
receives the blocked thread's priority, if that priority is higher than the owner's. It recovers its original priority
when it unlocks the lock(mutex).

(v) (T/F) A context switch takes place at every system call

F : Since kernel address space is part of the user address space for a process, thus switching between a user
code and kernel code requires no context switch unlike switching between two different user processes which
have two different address spaces

Q2 : Choose the best option. [5 marks]


(i) Master Boot Record (MBR): contains code that
(a) Loads the operating system. (b) Loads the system BIOS.
(c) Loads the Volume Boot Record (VBR). (d) Allows the user to choose which OS to load.

(ii) When a process is first launched, the operating system does not know the size
of this segment:
(a) text (b) data (c) bss – uninitialized data segment (d) heap

(iii) Switching between user level threads of the same process is often more efficient than switching between
kernel threads because:
(a) User level threads require tracking less state. (b) User level threads share same memory address space.
(c) Mode switching is not necessary. (d) Execution stays within the same process with user level threads

(iv) A thread that is blocked on a semaphore is awakened when another thread:


(a) Tries to decrement a semaphore’s value below 0. (b) Tries to increment the semaphore.
(c) Causes the semaphore’s value to reach a specific number. (d) Tries to block on the same semaphore
(v) When we execute the command “ ls | wc “ in UNIX systems, who is the producer and who is the
consumer?
(a) “ls” the consumer and “wc” the producer (b) “ls” the producer and “wc” the consumer
(c) Both are producers (d) Both are consumers

Q3 : Answer in brief. [10 marks]


(a) Give two examples of when a process might go directly from the state ‘running’ to ‘ready’
Soln : i) Time quantum exhausted in round-robin
ii) Higher priority process enters ready queue in preemptive priority scheduling

(b) Consider a computer system that supports multi-threading only in the user space. Suppose a process
sends a signal. Can the sender of the signal specify a particular thread to handle the signal? Justify your
answer.
Soln : No, not even the kernel can do so because the threads are not visible outside the process.

(c) Can an application process and kernel process set up a shared memory region? Justify
Soln : No application processes do not have have direct accesses to the kernel space and kernel processes do not have
the user space part.

(d) Suppose a process creates a pipe. Can its parent use the pipe? Justify
Can its grandchildren use the pipe? Justify your answer.

Soln : The parent cannot use the pipe, but the grandchildren can. When a process creates a child, the child inherits all
open file descriptors and hence inherits all open pipe descriptors. Thus, the grandchildren of the process may have
access to the pipe, but definitely not its parent.

(e) Consider the following table of arrival time and burst time of three processes P0, P1 and P2. The pre-
emptive shortest job first algorithm is used. Scheduling is carried out only at arrival or completion of
processes. What is the average waiting time of the three processes?

Process Arrival Time Burst


Time
P0 0ms 9ms
P1 1ms 4ms
P2 2ms 9ms

Q4. (a) : The following code shows a parent process creating a child process using fork. Assume,
the parent executes line nos. 5 and 6 and then the child gets scheduled.
i. Print the value of var as printed by the child process.
ii. Is f1.txt read by the child? Explain [3 marks]

int fd = open(“f1.txt”) //open a file


int var = 10;
int pid = fork();
if(pid >0) {
close(fd); // file is closed
a = 6;...
}
else if(pid ==0) {
printf("var=%d\n", var);
read(fd, something);
}

Soln : (a) 10. The value is only changed in the parent.


(b) Yes, the file is only closed in the parent
Q4 (b) : Write a pseudo code showing sequence of suitable system calls to create the following
process tree : main is the first process that is created followed by the other processes. [3 marks ]

main

A B

C D E F
Soln :

main()
{
L1l = fork();
if(L1l == 0) {
L21 = fork();
if (L21 = = 0)
exec(“C”);
else {
L21R = fork();
if(L21R = = 0) {
exec(“D”);
else
exec(“A”);
}
}

else{
wait();
L1r = fork();
if (L1r == 0){
L22 = fork();
if (L22 = = 0){
exec(“E”);
else {
L22R = fork();
if (L22R = = 0){
exec(“F”);
else wait();

exec(“B”);

Q4. (c) : You are working for a company that maintains an online music website. The website
displays the list of songs, the singer, the lyricist, the music director and the album along with a short
summary of the song. Songs are displayed on the website according to their current popularity
( obtained from the feedback of the customers ). Customers who want to listen to songs search the
database with a keyword. All song records having the keyword are listed as per their popularity
rating ( in decreasing order). The customer search is implemented by a search process.
What kind of IPC mechanism do you think will be preferable in the database server for storing the
song records and interaction among the search processes? [2 marks]
Soln : Shared Memory if the database server and the interaction happens at same machine.
Else massage passing

Q4.(d): This pseudo-code for a process and TestAndSet function, without any other assumptions, solves the
critical section problem between N processes. Is the statement correct? Justify. [2 marks]
Initial value of lock = FALSE; void Process(int N){
boolean TestAndSet(boolean lock) while(1){
{ while(TestAndSet (lock)) no-op;
boolean initial = lock; critical section
lock = true; lock = TRUE;
return initial; remainder section
} }

Soln : True.
The lock code provided to the left returns the initial value of lock i.e. False and then gets updated to True. Thus
the first process that requests for entry to Critical section receives the lock = F and itself asserts lock = T. Thus,
any other process when requests entry to CS during the time first process is in CS, loops on the lock ( while
condition of the code on the right). Thus, the Test and Set instruction ensures mutual exclusion.

Q5 (a). : What could the output of the concurrent execution of process A and process B be? [3 marks]

// Initialization
int x=0; int y=0;
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”);
printf(“d”);
y=1;
Soln :
b1adc
b1acd

Q5. (b) : What problem does the following code solve ?


[ Hint : Try to reach to the problem clearly explaining the working of the code] [3 marks]

Binary Semaphore mutex = 1;


Binary Semaphore proceed = 0;
int count =0;
while(1){
P(mutex);
count = count+1;
if(count==p) then P(proceed);
V(mutex);
P(proceed);
V(proceed);
}
Solution : The problem solved by the above code is the Barrier syncronization problem. The problem states that when
there are p processes involved in the system, each has to wait for all others at a point before proceeding further.

Q5 (c). : Alice and Bob are two siblings who quarrel very often. If they do not quarrel they sleep.
The situation is depicted by the the execution of two threads Alice and Bob

Void Alice() Void Bob()


{ {
while(1) { while(1) {
Sleep(); x = Quarrel(x);
x = Quarrel(x); Wakeup(thread Alice);
Wakeup(thread Bob); Sleep();
} }
}
i. The code shown above exhibits a well-known synchronization flaw. Briefly outline a scenario in
which this code would fail, and the outcome of that scenario.
ii. Show how to fix the problem by replacing the Sleep and Wakeup calls with semaphore P (down) and
V (up) operations. You should not disable interrupts. [4 marks]
Soln :
i. Missed wakeup--one thread calls Wakeup before the other one calls Sleep. A missed wakeup can occur
during any iteration of the loop if the scheduler forces a Yield between the Wakeup and the Sleep.

ii. Show how to fix the problem by replacing the Sleep and Wakeup calls with semaphore P (down) and
V (up) operations. No, you may not disable interrupts.

Alice () { Bob () {
while (TRUE) { while (TRUE) {
P(); P();
x = Quarrel(); x = Quarrel();
V(); V();
} }
} }

Q6. (a) : Albert, Neil, and Richard are three scientists who are attending a seminar. During the banquet
dinner they decide to sit together and discuss about their research while having noodles. The waiter
however, apologetically explains that they do not have sufficient chopsticks to supply along with noodles.
Only two pairs of chopsticks (for a total of four chopsticks) would be provided to be shared among the three
people. Albert proposes that all four chopsticks be placed in an empty glass at the centre of the table and that
each diner should obey the following protocol:
while (!had_enough_to_eat()) {
acquire_one_chopstick(); /* May block. */
acquire_one_chopstick(); /* May block. */
eat();

release_one_chopstick(); /* Does not block.*/


release_one_chopstick(); /* Does not block.*/
}
Can this dining plan lead to deadlock? Explain your answer. [2 marks]

Soln : No.
According to the given plan, three people would require six chopsticks in total to complete eating the noodles.
However, only four chopsticks are provided.
Thus, according to the solution provided, even if all three pick up one chopstick each, one additional chopstick
will be left which will be acquired by any one of three scientists. He/she will then have two chopsticks, enough for
eating. He/she will complete eating according to the algorithm. Once done, he/she will release both chopsticks
which will then be picked up by both the scientists waiting for their tur and eventually both will have a pir of
chopsticks and would complete eating.

Q6. (b) : Consider the following programs where A and B are arbitrary computations.
Initially, s1 = s2 = d =1; c1 = c2 = 0

P(s1) P(s2)
c1 := c1 + 1 c2 := c2 + 1
If (c1 == 1) then P(d) If (c2 == 1) then P(d)
V(s1) V(s2)
A B
P(s1) P(s2)
c1 := c1 – 1 c2 := c2 – 1
If (c1 == 0) then V(d) If (c2 == 0) then V(d)
V(s1) V(s2)

a) How many invocations of A can proceed concurrently?


b) While A is running how many invocations of B can proceed concurrently?
c) Can A starve?
d) Can B starve?
e) Is deadlock possible? [5 marks]]

Soln :

(a) As many instances of A as possible.


The first instance of A updates c1 =1 and then executes P(d). While the first instance is running, any other
instance that tries to execute A, drops through the first if condition and goes on to execute the critical section (A).
Subsequently all other instances repeat the same i.e. fall through the if condition and thus, all instances will be
able to invoke A.

(b) As many instances as possible. Same logic as in part (a)

(c) Yes.
The first instance of A may starve under the following situation :

Assume that first instance of B gets scheduled first and executes P(d).
Then the first instance of A is scheduled which enters with c1=0, updates c1=1 and then finds the if condition
TRUE, with d value 0. Thus, the first instance of A loops over P(d) and waits for it to be updated i.e. the operation
V(d).

The V(d) operation can occur under two conditions : i. either an instance of A executes V(d) or an instance of B
executes V(d).

The instance of A that executes V(d) is the one that executes with c1=1 i.e. the first instance. However, the first
instance loops over P(d) and does not proceed.

The second possibility is the first instance of B executes V(d). However, for that to happen the instance will have
to complete critical section and then move on to decrement c2 and execute the if condition.

However, from the above two (a) and (b) parts of the question, we realize that more than one instance can be in
the critical section. Thus, more than one instance of B can be in the critical section, causing scheduling among
these options. As a result, the first instance of B may not get a chance to run the V(d) operation, causing first
instance of A to starve waiting for V(d)

(d) Same as c

(e) Deadlock :

Yes.

Consider the situation when the first instance of A (with c1=1) loops over P(d) and then a context switch happens
and the first instance of B(c2=1) also loops over P(d). If such a situation arrives then V(d) operation for first
instance of A have to be through first instance of B which itself is looping and similarly, the V(d) operation for
first instance of B have to be through first instance of A which itself is loops. Thus, the first instances of both A
and B will have a cyclic dependency leading to deadlock.
Q6.(c) : Consider the (following) Bakery Algorithm, a solution of the Critical Section Problem for n
processes. Fill in the blanks. [3 marks]
repeat
choosing[i] := true;
number[i] := max (number[0], number[1],…,number[n-1])+1;
-------------------------------------------
choosing[i] := false;
for j := 0 to n-1
do begin
while (choosing[j])
do no-op;
while number[j] != 0 and number([j],j) < number([i],i)
do no-op;
end;
critical section
number[i] = 0; // The exit section
------------------
remainder section

Explanation :

The Bakery algorithm is one of the simplest known solutions to the mutual exclusion problem for
the general case of N process. Bakery Algorithm is a critical section solution for N processes. The
algorithm preserves the first come first serve property.
Before entering its critical section, the process receives a number. Holder of the smallest number
enters the critical section.

If processes Pi and Pj receive the same number,


if i < j , Pi is served first;
else Pj is served first.

The numbering scheme always generates numbers in increasing order of enumeration; i.e., 1, 2,
3, 3, 3, 3, 4, 5,

Notation – lexicographical order (ticket #, process id #) :


Firstly the ticket number is compared.
If same then the process ID is compared next, i.e.-
– (a, b) < (c, d) if a < c or if a = c and b < d
– max(a [0], . . ., a [n-1]) is a number, k, such that k >= a[i] for i = 0, . . ., n - 1

Shared data – choosing is an array [0..n – 1] of boolean values; & number is an array [0..n – 1] of
integer values. Both are initialized to False & Zero respectively

The pseudo code details :

First, the process sets its “choosing” variable to TRUE indicating its intent to enter critical
section. Then it gets assigned the highest ticket number corresponding to other processes. Then the
“choosing” variable is set to FALSE indicating that it now has a new ticket number. This is in-fact
the most important and confusing part of the algorithm. It is actually a small critical section in
itself ! The very purpose of the first three lines is that if a process is modifying its TICKET value
then at that time some other process should not be allowed to check its old ticket value which is
now obsolete. This is why inside the for loop before checking ticket value we first make sure that
all other processes have the “choosing” variable as FALSE.
After that we proceed to check the ticket values of processes where process with least ticket
number/process id gets inside the critical section. The exit section just resets the ticket value to zero.

********************************* END ****************************************

You might also like