This repository has been archived by the owner on Oct 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
pc2.c
161 lines (139 loc) · 3.41 KB
/
pc2.c
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#define CAPACITY 4
#define ITEM_COUNT (CAPACITY * 2)
char buffer1[CAPACITY];
char buffer2[CAPACITY];
int in1, in2;
int out1, out2;
char get_item(int n)
{
char item;
if(n == 1)
{
item = buffer1[out1];
out1 = (out1 + 1) % CAPACITY;
}
else if(n == 2)
{
item = buffer2[out2];
out2 = (out2 + 1) % CAPACITY;
}
else
exit(-1);
return item;
}
void put_item(char item, int n)
{
if(n == 1)
{
buffer1[in1] = item;
in1 = (in1 + 1) % CAPACITY;
}
else if(n == 2)
{
buffer2[in2] = item;
in2 = (in2 + 1) % CAPACITY;
}
else
exit(-1);
}
typedef struct {
int value;
pthread_mutex_t mutex;
pthread_cond_t cond;
}sema_t;
void sema_init(sema_t *sema, int value)
{
sema->value = value;
pthread_mutex_init(&sema->mutex, NULL);
pthread_cond_init(&sema->cond, NULL);
}
void sema_wait(sema_t *sema)
{
pthread_mutex_lock(&sema->mutex);
int i = 1;
while(sema->value <= 0)
{
pthread_cond_wait(&sema->cond, &sema->mutex);
}
sema->value--;
pthread_mutex_unlock(&sema->mutex);
}
void sema_signal(sema_t *sema)
{
pthread_mutex_lock(&sema->mutex);
sema->value += 1;
pthread_cond_signal(&sema->cond);
pthread_mutex_unlock(&sema->mutex);
}
sema_t mutex_sema1, mutex_sema2;
sema_t empty_buffer_sema1, empty_buffer_sema2;
sema_t full_buffer_sema1, full_buffer_sema2;
void *consume(void *arg)
{
int item;
for(int i = 0; i < ITEM_COUNT; i++)
{
sema_wait(&full_buffer_sema2);
sema_wait(&mutex_sema2);
item = get_item(2);
printf("\033[34m consume item: %c\n\033[0m", item); //蓝色为消费者
sema_signal(&mutex_sema2);
sema_signal(&empty_buffer_sema2);
}
return NULL;
}
void *compute(void *arg)
{
char item;
for(int i = 0; i < ITEM_COUNT; i++)
{
sema_wait(&full_buffer_sema1);
sema_wait(&mutex_sema1);
item = get_item(1);
sema_signal(&mutex_sema1);
sema_signal(&empty_buffer_sema1);
item += 'A' - 'a';
sema_wait(&empty_buffer_sema2);
sema_wait(&mutex_sema2);
put_item(item, 2);
printf("\033[33m compute item: %c\n\033[0m", item); //黄色为计算者
sema_signal(&mutex_sema2);
sema_signal(&full_buffer_sema2);
}
return NULL;
}
void *produce(void *arg)
{
char item;
for(int i = 0; i < ITEM_COUNT; i++)
{
sema_wait(&empty_buffer_sema1);
sema_wait(&mutex_sema1);
item = 'a' + i;
put_item(item, 1);
printf("\033[31m produce item: %c\n\033[0m", item); //红色为生产者
sema_signal(&mutex_sema1);
sema_signal(&full_buffer_sema1);
}
return NULL;
}
int main()
{
pthread_t producer_tid, computer_tid, consumer_tid;
sema_init(&mutex_sema1, 1);
sema_init(&mutex_sema2, 1);
sema_init(&empty_buffer_sema1, CAPACITY - 1);
sema_init(&empty_buffer_sema2, CAPACITY - 1);
sema_init(&full_buffer_sema1, 0);
sema_init(&full_buffer_sema2, 0);
pthread_create(&producer_tid, NULL, produce, NULL);
pthread_create(&computer_tid, NULL, compute, NULL);
pthread_create(&consumer_tid, NULL, consume, NULL);
pthread_join(producer_tid, NULL);
pthread_join(computer_tid, NULL);
pthread_join(consumer_tid, NULL);
return 0;
}