# 題目
* 林子皓
* 楊秉沅
Q1.How Peterson's Solution meets critical section requirements? Briefly explain.(Processes are Pi,Pj)
A1.
Mutual Exclusion:
The variable turn can never be i or j at the same time,so only one while loop can be breaked and enter the critical section.
Progress:
Consider Pi and Pj are only one set flag to true.The one that sets flag to true that can enter critical section immediately.
Consider Pi and Pj both set flag to true. if turn = i, i can execute critical section. After i exit ,Pi will set the flag to false. At this time,Pj can break the while loop and enter critical section immediately.
Bounding waiting:
If Pi finish,and then CPU allocate Pi to execute again before Pj. But Pi will set turn =j,So Pi stuck in while loop,and will not execute again and again immediately.And then Pj can enter critical section,will not be starved
Q2
P1 and P2 execute in different threads.The Processes share two semaphores: s1(initialized to 1) and s2 (initialized to 0). The Processes also share a global variable x (initialized to 0).
//P1
wait(s1);
x = x+1;
print(x);
signal(s2);
signal(s1);
//P2
wait(s1);
x = x+1;
print(x);
wait(s2);
signal(s1);
Which of the following outcomes is/are possible when processes P1 and P2 execute concurrently?
(A)P1 runs first and prints 1, P2 runs next and prints 2
(B)P2 runs first and prints 1, P1 runs next and prints 2
\(C)P1 runs first and prints 1, P2 does not print anything (deadlock)
(D)P2 runs first and prints 1, P1 does not print anything (deadlock)
A2:A、D
A:P1 first,pass wait(s1)>>s1 =0>>x=1>>print(1)>>s2=1>>s1=1,P2 second,pass wait(s1)>>s1=0>>x=2>>print(2)>>s2=0>>s1=1
D:P2 first,pass wait(s1)>>s1 =0>>x=1>>print(1)>>stuck in wait(s2),P1 also stuck in wait(s1)
* 王尚鵬
1. Which of the following statements is correct?
(a). One instruction of a High-Level language can only translate into one instruction of Low-Level language.
(b). If the size of buffer is full, producer can directly pass the item to consumer.
\(c). In producer and consumer’s problem, producer generate item and store it in a buffer and consumer can take the items from the buffer.
(d). When producer put a new item into buffer, the counter will decrease to tell consumer the space is getting smaller.
Ans1:\(c)
2. What is the meaning of variable “conuter” in the Producer code?
Ans2: records how much data are stored in the buffer.
* 楊閔恩