assignment-2 (tcs/tit 503) - gehu cs/it deptt (tcs/tit 503) topics covered: threads, process...

13
Assignment-2 (TCS/TIT 503) Topics covered: Threads, Process Synchronisation, Semaphores Submission dates: 1)CS A:Monday,26/10/2015 (Time: between 12 pm to 1 pm) 2)CS B:Monday,26/10/2015(in the lecture of OS) 3)IT:Monday ,26/10/2015 (Time: between 12 pm to 1 pm in the cabin of Mr. Dilip kumar) The assignments will not be submitted after the due timings on the same day also(applicable for all sections). Q1) Can a multithreaded solution using multiple user-level threads achieves better performance on a multiprocessor system than on a single-processor system? Ans: A multithreaded system comprising of multiple user-level threads cannot make use of the different processors in a multiprocessor system simultaneously. The operating system sees only a single process and will not schedule the different threads of the process on separate processors. Consequently, there is no performance benefit associated with executing multiple user-level threads on a multiprocessor system. Q2) What can be the circumstances under which a multithreaded solution using multiple kernel threads provide better performance than a single-threaded solution on a single- processor system? Ans: When a kernel thread blocks due to events like I/O event, another kernel thread can be switched in to use the interleaving time in a useful manner. A single-threaded process, on the other hand, will not be capable of performing useful work when a blocking event takes place. Therefore, in scenarios where a program might suffer from frequent blocking events or has to wait for other system events, a multi-threaded solution would perform better even on a single-processor system. Q3) A web server is implemented using the following 2 approaches. Approach 1: For each connection request, create a new separate process to handle that request. Approach 2: A single process is running on server to listen the incoming request and then for each connection request, a new thread is created . The web server has 1 GB memory space in total. Each process which is created on server takes 10 MB of memory space. Each thread takes 100 KB of memory space. a) Find the maximum number of connections which can be created in approach 1 and approach 2, assuming no memory space is taken by OS on server. Ans:

Upload: buithu

Post on 18-Apr-2018

258 views

Category:

Documents


13 download

TRANSCRIPT

Page 1: Assignment-2 (TCS/TIT 503) - GEHU CS/IT Deptt (TCS/TIT 503) Topics covered: Threads, Process Synchronisation, Semaphores ... free slots is sufficient to determine when to block

Assignment-2 (TCS/TIT 503)

Topics covered: Threads, Process Synchronisation, Semaphores

Submission dates:

1)CS A:Monday,26/10/2015 (Time: between 12 pm to 1 pm)

2)CS B:Monday,26/10/2015(in the lecture of OS)

3)IT:Monday ,26/10/2015 (Time: between 12 pm to 1 pm in the cabin of Mr. Dilip kumar)

The assignments will not be submitted after the due timings on the same day also(applicable for all sections).

Q1) Can a multithreaded solution using multiple user-level threads achieves better performance on a multiprocessor system than on a single-processor system? Ans: A multithreaded system comprising of multiple user-level threads cannot make use of

the different processors in a multiprocessor system simultaneously. The operating system sees only a single process and will not schedule the different threads of the process on separate processors. Consequently, there is no performance benefit associated with

executing multiple user-level threads on a multiprocessor system. Q2) What can be the circumstances under which a multithreaded solution using multiple

kernel threads provide better performance than a single-threaded solution on a single-processor system?

Ans: When a kernel thread blocks due to events like I/O event, another kernel thread can be switched in to use the interleaving time in a useful manner. A single-threaded process,

on the other hand, will not be capable of performing useful work when a blocking event takes place. Therefore, in scenarios where a program might suffer from frequent blocking events or has to wait for other system events, a multi-threaded solution would perform better even on a single-processor system. Q3) A web server is implemented using the following 2 approaches.

Approach 1: For each connection request, create a new separate process to handle that request.

Approach 2: A single process is running on server to listen the incoming request and then for each connection request, a new thread is created .

The web server has 1 GB memory space in total. Each process which is created on server takes 10 MB of memory space.

Each thread takes 100 KB of memory space.

a) Find the maximum number of connections which can be created in approach 1 and approach 2, assuming no memory space is taken by OS on server.

Ans:

Page 2: Assignment-2 (TCS/TIT 503) - GEHU CS/IT Deptt (TCS/TIT 503) Topics covered: Threads, Process Synchronisation, Semaphores ... free slots is sufficient to determine when to block

b) Repeat the part (a), assuming 2 MB of memory space is taken by OS on server. Ans:

Page 3: Assignment-2 (TCS/TIT 503) - GEHU CS/IT Deptt (TCS/TIT 503) Topics covered: Threads, Process Synchronisation, Semaphores ... free slots is sufficient to determine when to block

c) Which of the approach: approach 1 or approach 2 seems to be better and why?

Ans:Approach 2 is better as more number of connections can be handled at once.

4) The following is a set of three interacting processes that can access two shared

semaphores: semaphore U = 3;

semaphore V = 0;

[Process 1] [Process 2] [Process 3]

L1:wait(U) L2:wait(V) L3:wait(V) printf("C") printf("A") printf("D")

signal(V) printf("B") goto L3 goto L1 signal(V) goto L2

Within each process the statements are executed sequentially, but statements from different processes can be interleaved in any order that is consistent with the constraints imposed by the semaphores. When answering the questions below assume that once

execution begins, the processes will be allowed to run until all 3 processes are stuck in a wait() statement, at which point execution is halted.

Page 4: Assignment-2 (TCS/TIT 503) - GEHU CS/IT Deptt (TCS/TIT 503) Topics covered: Threads, Process Synchronisation, Semaphores ... free slots is sufficient to determine when to block

a) Assuming execution is eventually halted, how many C's are printed when the set of

processes runs?

Exactly 3. Each time Process 1 executes the "wait(U)" statement, the value of semaphore U is decremented by 1. Since there are no "signal(U)" statements, the loop

in Process 1 will execute only 3 times (ie, the initial value of U) and then stall the fourth time "wait(U)" is executed.

b) Assuming execution is eventually halted, how many D's are printed when this set of

processes runs?

Exactly 3. Process 1 will execute its loop three times (see the answer to the previous question), incrementing "signal(V)" each time through the loop. This will permit "wait(V)" to

complete three times. For every "wait(V)" Process 2 executes, it also executes a "signal(V)" so there is no net change in the value of semaphore V caused by Process 2. Process 3 does decrement the value of semaphore V, typing out "D" each time it does so. So Process 3 will

eventually loop as many times as Process 1.

c) What is the smallest number of A's that might be printed when this set of processes runs?

0. If Process 3 is scheduled immediately after Process 1 executes "signal(V)", then Process 2 might continue being stalled at its "wait(V)" statement and hence never execute its "printf" statements.

d) Is CABABDDCABCABD a possible output sequence when this set of processes runs?

No. Here are the events implied by the sequence above: start: U=3 V=0

printf C: U=2 V=1 printf A: U=2 V=0

printf B: U=2 V=1 printf A: U=2 V=0

printf B: U=2 V=1 printf D: U=2 V=0

printf D: oops, impossible since V=0

e) Is CABACDBCABDD a possible output sequence when this set of processes runs?

Yes:

start: U=3 V=0 printf C: U=2 V=1

printf A: U=2 V=0 printf B: U=2 V=1 printf A: U=2 V=0

printf C: U=1 V=1

Page 5: Assignment-2 (TCS/TIT 503) - GEHU CS/IT Deptt (TCS/TIT 503) Topics covered: Threads, Process Synchronisation, Semaphores ... free slots is sufficient to determine when to block

printf D: U=1 V=0

printf B: U=1 V=1 printf C: U=0 V=2

printf A: U=0 V=1 printf B: U=0 V=2

printf D: U=0 V=1 printf D: U=0 V=0

f) Is it possible for execution to be halted with either U or V having a non-zero value?

No. If U has a non-zero value, Process 1 will be able to run. If V has a non-zero value, Process 3 will be able to run.

Q5) The following pair of processes share a common variable X:

Process A Process B int Y; int Z;

A1: Y = X*2; B1: Z = X+1; A2: X = Y; B2: X = Z;

X is set to 5 before either process begins execution. As usual, statements within a process are executed sequentially, but statements in process A may execute in any order with respect to statements in process B.

A. How many different values of X are possible after both processes finish executing?

There are four possible values for X. Here are the possible ways in which statements from A and B can be interleaved.

A1 A2 B1 B2: X = 11 A1 B1 A2 B2: X = 6

A1 B1 B2 A2: X = 10 B1 A1 B2 A2: X = 10

B1 A1 A2 B2: X = 6 B1 B2 A1 A2: X = 12

B. Suppose the programs are modified as follows to use a shared binary semaphore S:

Process A Process B

int Y; int Z; wait(S); wait(S);

A1: Y = X*2; B1: Z = X+1; A2: X = Y; B2: X = Z;

signal(S); signal(S);

Page 6: Assignment-2 (TCS/TIT 503) - GEHU CS/IT Deptt (TCS/TIT 503) Topics covered: Threads, Process Synchronisation, Semaphores ... free slots is sufficient to determine when to block

S is set to 1 before either process begins execution and, as before, X is set to 5.

Now, how many different values of X are possible after both processes finish

executing?

The semaphore S ensures that, once begun, the statements from either process execute without interrupts. So now the possible ways in which statements from A and B can be

interleaved are: A1 A2 B1 B2: X = 11 B1 B2 A1 A2: X = 12

C. Finally, suppose the programs are modified as follows to use a shared binary semaphore T:

Process A Process B int Y; int Z;

A1: Y = X*2; B1: wait(T); A2: X = Y; B2: Z = X+1;

signal(T); X = Z;

T is set to 0 before either process begins execution and, as before, X is set to 5.

Now, how many different values of X are possible after both processes finish

executing?

The semaphore T ensures that all the statements from A finish execution before B begins. So now there is only one way in which statements from A and B can be interleaved:

A1 A2 B1 B2: X = 11

Q6) The following pair of processes share a common set of variables: "counter", "tempA" and "tempB":

Process A Process B ... ... A1: tempA = counter + 1; B1: tempB = counter + 2;

A2: counter = tempA; B2: counter = tempB; ... ...

The variable "counter" initially has the value 10 before either process begins to execute.

A. What different values of "counter" are possible when both processes have finished executing? Give an order of execution of statements from processes A and B that

would yield each of the values you give. For example, execution order A1, A2, B1, B2 would yield the value 13.

Page 7: Assignment-2 (TCS/TIT 503) - GEHU CS/IT Deptt (TCS/TIT 503) Topics covered: Threads, Process Synchronisation, Semaphores ... free slots is sufficient to determine when to block

There are three possible values for X. Here are the possible ways in which statements from

A and B can be interleaved. A1 A2 B1 B2: X = 13

A1 B1 A2 B2: X = 12 A1 B1 B2 A2: X = 11

B1 A1 B2 A2: X = 11 B1 A1 A2 B2: X = 12

B1 B2 A1 A2: X = 13

B. Modify the above programs for processes A and B by adding appropriate signal and wait operations on the binary semaphore "sync" such that the only possible final value of "counter" is 13. Indicate what should be the initial value of the semaphore "sync".

We need to ensure that A and B run uniterrupted, but it doesn't matter which runs first. semaphore sync = 1;

Process A Process B wait(sync); wait(sync);

A1: tempA = counter + 1; B1: tempB = counter + 2; A2: counter = tempA; B2: counter = tempB; signal(sync); signal(sync);

C. Modify the original programs for processes A and B by adding binary semaphores

and signal and wait operations to guarantee that the final result of executing the two processes will be counter = 11. Give the initial values for every semaphore you

introduce.

semaphore s1 = 0; semaphore s2 = 0; Process A Process B A1: tempA = counter + 1; B1: tempB = counter + 2; signal(s1); wait(s1); wait(s2); B2: counter = tempB;

A2: counter = tempA; signal(s2);

Page 8: Assignment-2 (TCS/TIT 503) - GEHU CS/IT Deptt (TCS/TIT 503) Topics covered: Threads, Process Synchronisation, Semaphores ... free slots is sufficient to determine when to block

Q7) Write the producer-consumer solution using monitors with the explanation.

Ans:

We have two types of processes: producers and consumers. A producer process

repeatedly generates some data that must be consumed by one consumer process. When a producer process generates the data, it puts it into a shared buffer that is fixed in size.

Let us assume that the buffer has n-slots that can each hold 1 data item. Each consumer process is repeatedly looking for a data item to consume. A producer process must block if there are no free slots in the buffer. A consumer process must block if there are no data items (full-slots) in the buffer. The buffer is shared between all producer and consumer processes.To solve this problem, we will use a monitor (let us call it mutex) with 2 conditions (let us call them prod and cons). The producers block on condition prod, and consumers block on condition cond. A single shared variable emptys, denoting number of free slots is sufficient to determine when to block (producers block, when emptys == 0, and consumers blocks when emptys == N). The code is pretty symmetric. The code structure shown here is typical of solutions using monitors. A process acquires the lock, and then tests whether it is safe to do the work. If not, it blocks using wait() on a condition. When it wakes up, it must test again since the conditions may have changed between the time it was woken up and the time it re-entered mutual exclusion (some one

else could have entered in the mutual exclusion in between and changed the state). This is done as a while loop. In the worst case, this will simply involve an extra check, and so is

not very harmful. When a process is finished with the critical section (in this problem, the use of the shared buffer), it updates state and signals one or more conditions (since

signals have no lasting effects, no harm is done by signaling spuriously, other than waking up some processes unnecessarily).

Producers Consumers

while (1) { produce a data item ;

// synchronization code mutex.lock();

while (emptys == 0) { prod.wait();

} ADD DATA ITEM TO A FREE SLOT; emptys--; cons.signal(); mutex.unlock(); }

while (1) { // synchronization code

mutex.lock(); while (emptys == N) {

cons.wait(); }

REMOVE DATA ITEM FROM A FULL SLOT; emptys++; prod.signal(); mutex.unlock(); consume the data item ; }

Any othervalid solution is also acceptable.

Page 9: Assignment-2 (TCS/TIT 503) - GEHU CS/IT Deptt (TCS/TIT 503) Topics covered: Threads, Process Synchronisation, Semaphores ... free slots is sufficient to determine when to block

Q8) Both busy waiting and blocking methods can be used as means to address critical

section problems and process synchronization. Describe how blocking is different from busy waiting, and discuss the advantages and disadvantages of each.

Ans: Busy waiting means a process simply spins (does nothing but continue to test its entry

condition) while it is waiting to enter its critical section(cpu cycles are wasted). This continues to use (waste) CPU cycles, which is inefficient. Additionally, if a low priority

process is in its critical section, and a high priority process goes into busy waiting, it may never get to its critical section., On a single CPU, busy waiting becomes a circular issue, so

blocking would make more sense, as it only requires a single call to the OS to change state to Ready/Run, once it is notified that blocking process is complete. For blocking solutions,

we would need to be careful because there is a chance that an incremental counter might be corrupted; processes can go into a sleep state and never awake

Q9) Do semaphores solve the problem of process synchronization completely? What are some of the things that might still go wrong in a solution that requires synchronization using semaphore?

Ans: The primary outstanding concern with use of semaphores is that they can lead to race

condition problems i.e they can violate mutual exclusion.Additionally, they do not naturally protect against deadlock.

Semaphores are tools only, and as such can be implemented incorrectly as they do not guarantee the sequence in which events occur, which may lead to a problem with synchronization. Q10) Consider the following program fragment:

semaphore s1=1,s2=1;

wait(s1); a++; wait(s2); v++; wait(s2) wait(s1);

(s1, s2 are counting semaphores). Now, consider two processes(or threads ) running this fragment of code simultaneously, can there be a deadlock? Why, or why not?

Ans:

Process 1 wait(s1); a++; wait(s2); v++; wait(s2) wait(s1);

Process 1 wait(s1); a++; wait(s2); v++; wait(s2) wait(s1);

Process 1 wait(s1); a++; wait(s2); v++; wait(s2) //line x wait(s1);

Process 2 wait(s1); a++; wait(s2); v++; wait(s2) wait(s1);

Page 10: Assignment-2 (TCS/TIT 503) - GEHU CS/IT Deptt (TCS/TIT 503) Topics covered: Threads, Process Synchronisation, Semaphores ... free slots is sufficient to determine when to block

Say process 1 runs first ,after line x, s2=-1 and process p2 makes s1=-1.Both process now

blocked one at s2 ant other at s1.So deadlock occurs.

Q11) Consider the following code:

shared int flag[2]={0,0} , turn=0;

Process 1: Process 2:

check the following code and tell which of the 3 requi rements (Mutual exclusion, bounded waiting, progress) is violated Ans: Mutual exclusion is violated. If process 1 runs line 3,then line 4,then its while loop,it will go in CS.

Now process 0 runs line 3,then line 4,then its while loop,it will also go in CS.

Q12) Answer the following semaphore regarding counting semaphore. Give brief explanation for each.

a)Can there ever be a process blocked on semaphore with non negative value? Ans:No, there cannot be any process blocked on semaphore with non negative value.

The thread can block only when semaphore value is negative and semaphore value is decremented by wait() .(In addition,the negative value of semaphore tells the number of

blocked process on that semaphore) b)Can a semaphore have negative value without having any process blocked on it?

Ans:Yes.A semaphore can be created with its initial value as some negative value.and no thread is blocked yet. Q13) Two processes X and Y need to access a critical section. Consider the following synchronization construct used by both the processes

Process X: Process Y:

/ *other codefor process X * / while (true) { P= true; Q= true; while (Q== true) { / *criticalsection*/ P=false; } } / *other codefor process X*/

/ *other codefor process Y * / while (true) { Q= true; P= true; while (P== true) { / *criticalsection*/ Q=false; } } / *other codefor process Y*/

flag[1]=1; //line 3 turn=1; //line 4s while(flag[0]==1&&turn==1); critical section; flag[1]=0;

flag[0]=1; //line 1 turn=1; /line 2 while(flag[1]==1&&turn==0); critical section; flag[0]=0;

Page 11: Assignment-2 (TCS/TIT 503) - GEHU CS/IT Deptt (TCS/TIT 503) Topics covered: Threads, Process Synchronisation, Semaphores ... free slots is sufficient to determine when to block

Here, P and Q are shared variables and both are initialized to false. Which one of the

following statements is true? (A) The proposed solution prevents deadlock but fails to guarantee mutual exclusion

(B) The proposed solution guarantees mutual exclusion but fails to prevent deadlock (C) The proposed solution guarantees mutual exclusion and prevents deadlock

(D) The proposed solution fails to prevent deadlock and fails to guarantee mutual exclusion Give proper explanation for your answer.

Ans: P and Q =FALSE Initially.

Assume that, process X is willing to enter into critical section. So it makes P= True, then if processor switches to process Y, then process Y can enter into critical section.

After entering into the critical section, then if processor switches to process X, then process X also can enter into the critical section.

It is clearly showing that both are in critical section at a time which leads to “failing to guarantee material exclusion”

To enter into the critical section process X is not waiting for process Y and vice versa. So we

can “Prevent Deadlock” so, overall, option 1 is correct or TRUE.

Q14) The following program consists of 3 concurrent processes and 3 binary semaphores.

The semaphores are initialized as S0=1, S1=0, S2=0.

Process P0 Process P1 Process P2

while (true) { wait (S0); print (0); release (S1); release (S2);

}

wait (S1); Release (S0);

wait (S2); release (S0);

How many times will process P0 print '0'? (a) At least twice (b) Exactly twice (c) Exactly thrice (d) Exactly once Give proper explanation for your answer. Ans: (a) At least twice Initially only P0 can go inside the while loop as S0 = 1, S1 = 0, S2 = 0. P0 first prints ‘0’ then, after releasing S1 and S2, either P1 or P2 will execute and release S0. So 0 is printed again.

Q15) Consider a multiprocessor system and a multithreaded program written using the many-to-many threading model. Let the number of user-level threads in the program be more than the number of processors in the system. Discuss what will happen performance

wise in the following scenarios. a. The number of kernel threads allocated to the program is less than

Page 12: Assignment-2 (TCS/TIT 503) - GEHU CS/IT Deptt (TCS/TIT 503) Topics covered: Threads, Process Synchronisation, Semaphores ... free slots is sufficient to determine when to block

the number of processors.

When the number of kernel threads is less than the number of processors, then some of the

processors would remain idle since the scheduler maps only kernel threads to processors and not user-level threads to processors.

b. The number of kernel threads allocated to the program is equal to

the number of processors.

When the number of kernel threads is exactly equal to the number of processors, then it is possible that all of the processors might be utilized simultaneously. However, when a kernel

thread blocks inside the kernel (due to I/o event or while invoking system calls), the corresponding processor would remain idle.

c. The number of kernel threads allocated to the program is greater

than the number of processors but less than the number of user level threads.

When there are more kernel threads than processors, a blocked kernel thread could be swapped out in favor of another kernel thread that is ready to execute, thereby increasing

the utilization of the multiprocessor system.

Q16) A single-lane bridge connects the two villages ,Village A and Village B. Farmers in the

two villages use this bridge to deliver their produce to the neighbouring town. The bridge can become deadlocked if both a northbound and a southbound farmer get on the bridge at the same time (Both village farmers are stubborn and are unable to back up). Using exactly one semaphore, design an algorithm that prevents deadlock. Do not be concerned about starvation and inefficiency.

Ans:

One semaphore is all that is needed to solve this problem. When a driver approaches the bridge they will reserve (request) the semaphore. If granted, they will cross the bridge, once

they have crossed the bridge they will signal (release) the semaphore. If not, they will wait until they have received the semaphore

For example: semaphore ok_to_cross=1;

wait(ok_to_cross); cross the bridge;

…………… Signal(ok_to_cross);

Or

More specifically semaphore ok_to_cross = 1; main() { ………. enter_bridge();

Page 13: Assignment-2 (TCS/TIT 503) - GEHU CS/IT Deptt (TCS/TIT 503) Topics covered: Threads, Process Synchronisation, Semaphores ... free slots is sufficient to determine when to block

cross_bridge();

exit_bridge(); ………….

}

void enter_bridge() {

wait(ok_to_cross); }

void exit_bridge() {

signal(ok_ to_cross); }

void cross_bridge() {

//logic of crossing the bridge; }

Q17) If the wait() and signal() semaphore operations are not executed atomically, then can mutual exclusion be violated? Why or why not? Ans: Yes, mutual exclusion can be violated. A wait () operation atomically decrements the value associated with a semaphore. If two wait() operations are executed on a semaphore

when its value is 1 and if the two operations are not performed atomically, then it is possible that both operations might proceed to decrement the semaphore value, thereby violating mutual exclusion.In the same manner,it is true for signal() also.