进程同步问题

#读写者问题(多个)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
int wmutex=1,rmutex=1;//读写信号量
int readCount=0;//读者数量
void Writer();
void Reader();
int main()
{
cobegin
Reader();
Writer();
coend;
}
//写者:
void Writer()
{
while(1){
P(wmutex);
...
写书
...
V(wmutex);
}
}
//读者
void Reader()
{
while(1)
{
P(rmutex);
if(readCount==0)
P(wmutex);
readCount++;
V(rmutex);
...
读书
...
P(rmutex);
readCount--;
if(readCount==0)
V(wmutex);
V(rmutex);
}
}

#哲学家就餐问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int chopstick[]={1,1,1,1,1};//五个筷子
int mutex=1;
int main()
{
cobegin
switch(i)
{
V(SB);
case 1~5: Pi();break;
}
coend;
}
Pi()//第i个哲学家进餐
{
while(1)
{
P(mutex); //在取筷子前获得互斥量
P(chopstick[i]); //取左边筷子
P(chopstick[(i+1)%5]); //取右边筷子
V(mutex); //释放取筷子的信号量
...
eat; //进餐
...
V(chopstick[i]); //放回左边筷子
V(chopstick[(i+1)%5]); //放回右边筷子
...
think;//思考
...
}
}

#生产者消费者问题

== 一个生产者 一个消费者 一个缓冲器:==

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int buffer;//1 表示生产 0表示消费
int empty=1,full=0;//空和满
void producer();
void consumer();
int main()
{
cobegin
producer(); consumer();
coend;
}
void producer()
{
while(1)
{
...
生产
...
P(empty);
buffer=1;
V(full);
}
}
void consumer()
{
while(1)
{
P(full);
buffer=0;
V(empty);
...
消费
...
}
}

一个生产者 一个消费者 n个缓冲器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
int buffer[1024],t=0,k=0;
int empty=1,full=0;
void producer();
void consumer();
int main()
{
cobegin
producer(); consumer();
coend;
}
void producer()
{
while(1)
{
...
生产
...
P(empty);
buffer[k]=1;
k=(++k)%n;
V(full);
}
}
void consumer()
{
while(1)
{
P(full);
buffer[t]=0;
t=(++t)%n;
V(empty);
...
消费
...
}
}

#司机与售票员问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int s1=0,s2=0;
void P1();//司机
void P2();//售票员
int main()
{
cobegin
P1(); p2();
coend;
}
void P1()
{
while(1){
P(s1);
...
启动
正常行驶
到站
...
V(s2);
}
}
void P2()
{
while(1)
{
关门
V(s1);
售票
P(s2);
开门
}
}

#吃水果

问题描述
桌子上有一只盘子,最多可容纳两个水果,每次只能放人或取出一个水果。爸爸专向盘子中放苹果(apple),妈妈专向盘子中放桔子(orange),1个儿子专等吃盘子中的桔子,1个女儿专等吃盘子中的苹果。请用P、V操作来实现爸爸、妈妈、儿子、女儿之间的同步与互斥关系。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
int mutex=1;
int empty=2;
int apple=0,orange=0;
void father(), mother();
void son(),daughter();
int main()
{
cobegin
father(); mother();
son(); daughter();
coend;
}
void father()
{
while(1)
{
P(empty);
P(mutex);
put an apple;
V(mutex);
V(apple);
}
}
void mother()
{
while(1)
{
P(empty);
P(mutex);
put an orange;
V(mutex);
V(orange);
}
}
void son()
{
while(1)
{
P(orange);
P(mutex);
eat an oeange;
V(mutex);
V(empty);
}
}
void daughter()
{
while(1)
{
P(apple);
P(mutex);
eat an apple;
V(mutex);
V(empty);
}
}

#独木桥问题
某条河上只有一座独木桥,以便行人过河。现在河的两边都有人要过桥,按照下面的规则过桥。为了保证过桥安全,请用P、V操作分别实现正确的管理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
int SA=1,SB=1,mutex=1;
int countA=0,countB=0;
void A(),B();
int main()
{
cobegin
A();
B();
coend;
}
void A()
{
P(SA);
if(countA==0)
{
P(mutex);
countA++;
}
V(SA);
过独木桥;
P(SA);
countA--;
if(countA==0)
{
V(metux);
}
V(SA);
}
void B()
{
P(SB);
if(countB==0)
{
P(mutex);
countB++;
}
V(SB);
过独木桥;
P(SB);
countB--;
if(countB==0)
{
V(metux);
}
V(SB);
}