-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapple_orange_pro.cpp
111 lines (97 loc) · 2.57 KB
/
apple_orange_pro.cpp
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <cstdio>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
#include <cstdlib>
// 场景:当前有一个可以放置两个水果的盘子
// father往盘子中放苹果,mother往盘子中放橘子
// son只从盘子中拿橘子吃,daugter只从盘子中拿苹果吃
// 完成四者的互斥与同步
sem_t orange;
sem_t apple;
sem_t plate;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int appleNum = 0;
int orangeNum = 0;
std::string plateInfo()
{
std::string ret = std::string("盘子中🍎: ") + std::to_string(appleNum)
+ std::string(" 🍊: ") + std::to_string(orangeNum);
return ret;
}
void *placeApple(void *arg)
{
char *name = (char *) arg;
while(1)
{
sem_wait(&plate);
pthread_mutex_lock(&mutex);
appleNum++;
printf("%s👨🏻往盘子中放了个苹果🍎 [%s]\n", name, plateInfo().c_str());
pthread_mutex_unlock(&mutex);
sem_post(&apple);
sleep(1);
}
return nullptr;
}
void *placeOrange(void* arg)
{
char *name = (char *) arg;
while (1)
{
sem_wait(&plate);
pthread_mutex_lock(&mutex);
orangeNum++;
printf("%s🧑🏻往盘子中放了一个橘子🍊 [%s]\n", name, plateInfo().c_str());
pthread_mutex_unlock(&mutex);
sem_post(&orange);
sleep(1);
}
return nullptr;
}
void *eatApple(void *arg)
{
char *name = (char *) arg;
while(1)
{
sem_wait(&apple);
pthread_mutex_lock(&mutex);
appleNum--;
printf("%s👩🏻从盘子中拿了一个苹果吃🍎 [%s]\n", name, plateInfo().c_str());
pthread_mutex_unlock(&mutex);
sem_post(&plate);
}
return nullptr;
}
void *eatOrange(void *arg)
{
char *name = (char *) arg;
while(1)
{
sem_wait(&orange);
pthread_mutex_lock(&mutex);
orangeNum--;
printf("%s😋从盘子中拿了一个橘子吃🍊 [%s]\n", name, plateInfo().c_str());
pthread_mutex_unlock(&mutex);
sem_post(&plate);
}
return nullptr;
}
int main()
{
sem_init(&orange, 0, 0);
sem_init(&apple, 0 , 0);
sem_init(&plate, 0, 2);
pthread_t father, mother, son, daughter;
pthread_create(&father, nullptr, placeApple, (void*)"fahter");
pthread_create(&mother, nullptr, placeOrange, (void*)"mohter");
pthread_create(&son, nullptr, eatOrange, (void*)"son");
pthread_create(&daughter, nullptr, eatApple, (void*)"daughter");
while(1)
{
printf("[main thread]\n");
sleep(1);
}
return 0;
}