-
Notifications
You must be signed in to change notification settings - Fork 0
/
pc2.c
157 lines (122 loc) · 3.15 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
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
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);
sema->value--;
while (sema->value < 0)
pthread_cond_wait(&sema->cond, &sema->mutex);
pthread_mutex_unlock(&sema->mutex);
}
void sema_signal(sema_t *sema)
{
pthread_mutex_lock(&sema->mutex);
++sema->value;
if(sema->value<=0)
pthread_cond_signal(&sema->cond);
pthread_mutex_unlock(&sema->mutex);
}
#define CAPACITY 4
int buffer1[CAPACITY],buffer2[CAPACITY];
int pIn,calIn;
int calOut,conOut;
int get_item(int *out,int*buffer)
{
int item;
item = buffer[*out];
*out = ((*out) + 1) % CAPACITY;
return item;
}
void put_item(int item,int *in,int*buffer)
{
buffer[*in] = item;
*in = ((*in) + 1) % CAPACITY;
}
sema_t empty_buffer1_sema;
sema_t full_buffer1_sema;
sema_t empty_buffer2_sema;
sema_t full_buffer2_sema;
sema_t buffer1_mutex_sema;
sema_t buffer2_mutex_sema;
#define ITEM_COUNT (CAPACITY * 2)
void *consume(void *arg)
{
int i;
int item;
for (i = 0; i < ITEM_COUNT; i++) {
sema_wait(&full_buffer2_sema);
sema_wait(&buffer2_mutex_sema);
item = get_item(&conOut,buffer2);
sema_signal(&buffer2_mutex_sema);
sema_signal(&empty_buffer2_sema);
printf(" consume item: %c\n", item);
}
}
void *calculate(void *arg)
{
int i;
int item;
for (i = 0; i < ITEM_COUNT; i++) {
sema_wait(&full_buffer1_sema);
sema_wait(&buffer1_mutex_sema);
item = get_item(&calOut,buffer1);
sema_signal(&buffer1_mutex_sema);
sema_signal(&empty_buffer1_sema);
item=item+'A'-'a';
sema_wait(&empty_buffer2_sema);
sema_wait(&buffer2_mutex_sema);
put_item(item,&calIn,buffer2);
sema_signal(&buffer2_mutex_sema);
sema_signal(&full_buffer2_sema);
printf(" calculate item: %c\n", item);
}
}
void *produce(void*args)
{
int i;
int item;
for (i = 0; i < ITEM_COUNT; i++) {
sema_wait(&empty_buffer1_sema);
sema_wait(&buffer1_mutex_sema);
item = i + 'a';
put_item(item,&pIn,buffer1);
sema_signal(&buffer1_mutex_sema);
sema_signal(&full_buffer1_sema);
printf("produce item: %c\n", item);
}
}
int main()
{
pthread_t consume_tid;
pthread_t produce_tid;
pthread_t calculate_tid;
pIn=0;
conOut=0;
calIn=0;
calOut=0;
sema_init(&empty_buffer1_sema, CAPACITY);
sema_init(&full_buffer1_sema, 0);
sema_init(&empty_buffer2_sema, CAPACITY);
sema_init(&full_buffer2_sema, 0);
sema_init(&buffer1_mutex_sema, 1);
sema_init(&buffer2_mutex_sema, 1);
pthread_create(&produce_tid, NULL, produce, NULL);
pthread_create(&consume_tid, NULL, consume, NULL);
pthread_create(&calculate_tid, NULL,calculate, NULL);
pthread_join(produce_tid,NULL);
pthread_join(consume_tid,NULL);
pthread_join(calculate_tid,NULL);
return 0;
}