OS questions 1)Two processes, P1 and P2, execute at the same time the following
ID: 3569629 • Letter: O
Question
OS questions
1)Two processes, P1 and P2, execute at the same time the following code: (10pts)
for (i=0; i<2; i++)
S = S+1;
Where S is a shared integer with the initial value of 0.
a) What would be the value of S if the two processes would execute to completion in the order P1, P2 (this means that P1 starts and runs to completion, then P2 executes)?
b) What are possible values for S in reality? (Hint, codes interlace.)
c) Use semaphore(s) to fix the problem: in other words, modify the code in such a way as to always obtain the result described in part a).
2)Consider the following snapshot of a system:
Process Allocation Max Available
A B C D A B C D A B C D
P0 1 2 0 1 1 6 6 2 0 2 2 0
P1 1 0 0 1 1 1 0 1
P2 0 4 4 0 1 4 4 2
P3 0 0 0 2 0 0 2 3
P4 1 0 0 1 3 7 4 4
a) Is the system in a safe state (Use algorithm to show it) ?
b) Is a request from Process P4 arrives for (0, 1, 0, 0) can the request be granted immediately (Use the data to show it) ?
c) Is a request from Process P4 arrives for (0, 0, 1, 0) can the request be granted immediately (Use the data to show it)?
3)Given the following processes, priorities, and burst times, what will be the average wait time using a simple priority-based scheduler?
Process Bust Time Priority
P1 10 3
P2 9 1
P3 20 4
P4 2 5
P5 3 2
15
14
17
10
4)The following code, using semaphores, will result in what kind of problem
mutex.acquire
criticalSection();
mutex.acquire();
deadlock
no problem will result in most cases
livelock
mutual exclusion violation
5)For a particular workload in a particular system, it takes 10
Explanation / Answer
a) S=4
b)
li r1, 0 # i in r1
li r2, 2 # loop limit in r2
loop: bge r1, r2, done # exit if i greater than limit
ld r3, S # r3 gets the value of S
addi r3, 1 # increment the value of S
st S, r3 # store the value of S back in memory
addi r1, 1 # i <- i+1
j loop
done:
Depending on the order instructions of the two processes are interleaved, the possible values
for S are 2, 3 and 4.
c)
semaphore mutex=1;
for (i=0; i<2; i++) {
wait(mutex); // wait(&mutex) to be more precise
S = S+1;
signal(mutex);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.