Answers - Test 2: P1 P2 P3 P4
Answers - Test 2: P1 P2 P3 P4
Question 1
Q
Ans
Q
1
B
8
2
B
9
3
B
10
4
C
11
5
B
12
6
B
13
7
B
14
15
16
Ans
C
C
B
B
B
B
B
B
65536
Question 2
Process
P1
P2
P3
P4
Allocation
R1 R2 R3 R4
1 0 2 1
2 0 1 1
1 1 0 1
1 1 1 1
Max
Available
R1 R2 R3 R4
1 1 2 1
2 2 2 1
2 1 3 1
1 1 2 2
R1 R2 R3 R4
0 0 2 1
0 0 1 0
1 1 3 2
1 0 3 2
2 1 5 3
1 1 2 3
3 2 5 4
3 0 4 4
5 2 6 5
1. What is the need matrix? Write your answer in the table below.
Process
P1
P2
P3
P4
R1
0
0
1
0
Need
R2 R3
1
0
2
1
0
3
0
1
R4
0
0
0
1
2. The above state is a safe state since all the processes can complete
according to the Safety algorithm. The safe sequence is: P4, P1, P3, P2.
3. If process P2 requests for (0,0,2,0), will this request granted? Explain your
answer.
The request of process P2 cannot be granted since the request is greater
than the need of P2.
1/3
4. If process P3 requests for (1,0,2,0), will this request granted? Explain your
answer.
The request of process P3 cannot be granted immediately since the
required resources are not available at the moment therefore, the
process should wait.
Question 3 [3+7 points]
1. Which variables should you declare as shared variable for algorithm 3 for mutual
exclusion as discussed in the class? Declare and initialize them properly.
int turn=0;
Boolean Flag[2]={False,False};
2. Consider the following two cooperating processes. One of the processes is given
below. The shared variable are x, and y.
There are two critical sections in the program. Therefore, both of them should
be taken care of. We need to put wait(s) before the critical sections and
signal(s) after the critical sections. The program should be as follows.
int main(){
int m,n,i;
/* get shared memory and attach it to your process.
Ptr points to the shared memory */
printf(Enter the value of n);
scanf(%d,&n);
wait(s)
ptr->x=1;
signal(s);
i=1;
n=5;
while(i<n) {
m=i;
wait(s);
ptr->x++
m=ptr->x+m;
ptr->y=m+1;
signal(s);
i++;
}
Printf(The job is done!!!!);
}
2/3
3/3