OS作業二

名稱
OS作業二
心得
1. Consider the three conditions for a solution of critical section problem to be a correct solution: Mutual Exclusion, Progress and Bounded Waiting. Please explain

Answer:

Mutual exclusion > If one process is executing their critical section, the second process which wish to do so will become blocked by the flag of the first process. If both processes attempt to enter at the same time, the last process to execute "turn = j" will be blocked.

Progress > Each process can only be blocked at the while if the other process wants to use the critical section. If both of flag[j] and turn==j conditions are true, the other will be allowed to enter the critical section. Upon exiting the critical section, will set flag[j] to false, releasing process i. The shared variable turn assures that only one process at a time can be blocked, and the flag variable allows one process to release the other when exiting their critical section.

Bounded Waiting > As each process enters their entry section, they set the turn variable to be the other processes turn. Since no process ever sets it back to their own turn, this ensures that each process will have to let the other process go first at most one time before it becomes their turn again.



2. While Peterson’s solution is correct, please discuss the situation where P1 and P2 are entering the Critical Section at about the same time. How to decide which process can enter its Critical Section?

Answer:

If both processes try to enter at the same time, turn is set to both i and j at the same time. Only one of these assignments lasts; the other will occur, but will be overwritten immediately. The eventual value of turn decides which of the two processes is allowed to enter its critical section first.



3. Please specify under what kind of situation where busy waiting can be considered advantageous?

Answer:

While read is busy waiting, write is using CPU. While write is busy waiting, read is using CPU. Therefore, there is less chance to happen conflict between read and write to use CPU.



4. Consider the reader program of the reader-writer problem below. Please explain
(1) why the readers need to use the semaphore “mutex” but the writers don’t.
(2) why the readers also work on the “wrt” semaphore which is used by the writers.
wait(mutex);
readcount++;
if (readcount == 1)
wait(wrt);
signal(mutex);

reading is performed

wait(mutex);
readcount--;
if (readcount == 0)
signal(wrt);
signal(mutex):

Answer:

(1) No reader will kept waiting unless a writer has already obtained permission to use the shared database.

(2) Because the shared database can only for one writer using or for one or more reader using. When the shared database is used, wrt will become 0 while the executing is finished, wrt will become 1.