Jordi Cucurull Juan Answers for lesson 1 28/01/2016 Exercise 1 – Forking processes

advertisement
Jordi Cucurull Juan
Answers for lesson 1
28/01/2016
Exercise 1 – Forking processes
The output will be 5. When a child process is created it has its own address space (copy of the father
address space). Hence, despite the same variables exist, the content of them is stored in another
location. As a consequence the modifications that one process does to one variable does not affect the
same variable in the other process.
Exercise 2
There are two general techniques to communicate processes. One is shared memory and the other one
is using interprocess-communication (IPC) facilities provided by the operating system. With the first
one a certain memory address space is shared by several processes. With the second one a message
communication scheme is used.
In the case of using a POSIX-compliant C you can use the memory-mapped file support (mmap) to
exchange data between parent and child processes. Another option is to use sockets to communicate the
two processes.
Exercise 3
Example of sequence that will end up in two processes running into the critical section:
Sequence
Mutex
Init
P1 Release
P1 Enters critical section
P2 Acquire
P2 Enters critical section!!!
1
2
2
1
1
Jordi Cucurull Juan
Answers for lesson 1
28/01/2016
Exercise 4 - Mutual Exclusion
Prove that the algorithm satisfy all three requirements of the critical section-problem.
This algorithm use two shared variables, flag and turn. The flag[] array indicates if a process is
interested in entering its critical section (true) or not (false). The turn variable establishes the priority to
enter the critical section in case the two processes show interest to enter.
a) Mutual exclusion
A process Pi can only enter its critical section if flag[j]==false. Two different cases can be
distinguished:
1) Both processes try to enter the critical section at the same time
Both processes reach the first while statement (line 4) at the same time with flag[i]==flag[j]==true.
Hence none of them can enter the critical section. Then, the one selected in the variable turn (let say Pi
in the example) will make the opposite process Pj to hold the if condition (line 5) and temporally set its
flag[j]=false, thus allowing Pi to successfully exit the while statement (line 4). Pj then will keep
waiting in the second while statement (line 8) for a change of the turn value. This condition will persist
as long as Pi is in its critical section, only after leaving it the value of turn is changed (line 17). Then,
mutual exclusion is preserved.
2) One of the processes arrives when another is in the critical section
One process Pi is in the critical section, then flag[i]==true. When the second process Pj arrives it
reaches the while statement and holds its condition (line 4) entering the loop. If turn==i the second
process will continue as detailed in the first case, reaching the second while statement (line 8). If
turn==j, the if condition (line 5) does not hold and the while statement (line 4) is evaluated again with
the same result. This situation will persist as long as Pi is in its critical section, since the value of
flag[i] is not changed to false until the process leaves it (line 17). Then, mutual exclusion is preserved.
b) Progress
A process Pi can be prevented to enter its critical section in two while loops (lines 4 and 8). In the first
while statement Pi can only be stuck if the condition flag[j]==true, i.e. if the other process Pj is in the
critical section or has interest to enter in it. Otherwise, Pi can freely enter the critical section as many
times as it wants.
If Pi and Pj want to enter the critical section at the same time, one of them (say Pi), will wait in the
second while statement until the value of turn changes (and Pj will enter to the critical section). The
waiting process Pi will enter the critical section as soon as the other process Pj leaves it and sets the
values of turn=i and flag[j]=false. Then, progress is guaranteed.
Jordi Cucurull Juan
Answers for lesson 1
28/01/2016
c) Bounded-waiting
When one process Pj leaves its critical section it sets the variable turn=i. The process Pi, waiting for
this change (line 8), can continue its execution and sets flag[i]=true. After doing this, the process Pj
can retry entering the critical section in the first while statement (line 4), but if Pi has set its variable
flag[i]=true (line 11) the process Pj will end up releasing its intention to enter the critical section and
waiting in the lines 8. This demonstrates that the bounded waiting is preserved.
Exercise 5
(a) To check if the system is safe, first we have to calculate the table of needed resources:
Need
A
P0
P1
P2
P3
P4
1
3
1
3
2
B
C
1
1
2
1
1
1
2
4
4
1
And then we find out a possible sequence of execution for the processes that satisfies the calculated
maximum resource needs for each of them:
<P0,P2,P1,P3,P4>
Work
Need 0 <= (2,3,2)
Need 2 <= (2,4,4)
Need 1 <= (4,5,4)
Need 3 <= (5,5,7)
Need 4 <= (9,6,8)
(10,6,8)
(b) In order to know if the requested resources can be immediately granted, we have do the following:
1) Check that the resources requested are not higher than the Need for this process
Request-2 (1,2,0) <= Need-2 (1,2,4)
2) Check that the resources requested are not higher than the resources available
Request-2 (1,2,0) <= Available (2,3,2)
Jordi Cucurull Juan
Answers for lesson 1
28/01/2016
3) Recalculate the Allocated and Need values for the process that request the resources and the
Available resources:
Allocated-2 (3,3,0)
Need-2 (0,0,4)
Available (1,1,2)
4) Find a possible sequence of execution:
<P0,P2,P1,P3,P4>
Work
Need 0 <= (1,1,2)
Need 2 <= (1,2,4)
Need 1 <= (4,5,4)
Need 3 <= (5,5,7)
Need 4 <= (9,6,8)
(10,6,8)
(c) The procedure is the same than before:
1) Check that the resources requested are not higher than the Need for this process
Request-3 (1,1,1) <= Need-3 (3,1,4)
2) Check that the resources requested are not higher than the resources available
Request-3 (1,1,1) <= Available (2,3,2)
3) Recalculate the Allocated and Need values for the process that request the resources and the
Available resources:
Allocated-3 (5,2,2)
Need-3 (2,0,3)
Available (1,2,1)
4) Find a possible sequence of execution:
<P0,?> ← No possible sequence of execution, the request cannot be granted
Work
Need 0 <= (1,2,1)
????? <= (1,3,3)
Download