Repository navigation
Expand file tree
/
Copy pathprodcons_sem.c
More file actions
171 lines (153 loc) · 4.05 KB
/
Copy pathprodcons_sem.c
File metadata and controls
171 lines (153 loc) · 4.05 KB
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
162
163
164
165
166
167
168
169
170
171
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#define BUFFERSIZE 10
/* 3 threads, one producer,
* 2 consumers */
pthread_t producer_thread;
pthread_t consumer_thread1, consumer_thread2;
/* A protected buffer,
* protected with 3 semaphores */
typedef struct prot_buffer {
sem_t *sem_read;
sem_t *sem_write;
int readpos, writepos;
char buffer[BUFFERSIZE];
} prot_buffer_t;
prot_buffer_t *b;
sem_t *mutex;
void *producer(void *str) {
printf("[P] read characters and put into shared buffer (quit with 'E')...\n");
#ifdef DEBUG
printf("[P] %s started...\n", (char *)str);
#else
(void)str;
#endif
char c;
for (;;) {
while ('\n' != (c = getchar())) {
if ('E' == c) {
pthread_cancel(consumer_thread1);
pthread_cancel(consumer_thread2);
pthread_exit(NULL);
}
#ifdef DEBUG
printf("[P] %s put %c\n", (char *)str, c);
#endif
if (sem_wait(b->sem_write))
perror("sem_wait");
if (sem_wait(mutex))
perror("sem_wait");
#ifdef DEBUG
printf("[P] %s in critical section...\n", (char *)str);
#endif
b->buffer[b->writepos] = c;
b->writepos++;
if (b->writepos >= BUFFERSIZE)
b->writepos = 0;
#ifdef DEBUG
printf("[P] %s about to leave critical section...\n", (char *)str);
#endif
if (sem_post(mutex))
perror("sem_post");
if (sem_post(b->sem_read))
perror("sem_post");
#ifdef DEBUG
printf("[P] %s outside critical section...\n", (char *)str);
#endif
}
}
}
void *consumer(void *str) {
#ifdef DEBUG
printf("[C] %s started...\n", (char *)str);
#endif
char c;
for (;;) {
#ifdef DEBUG
printf("[C] %s trying to enter critical section ...\n", (char *)str);
#endif
if (sem_wait(b->sem_read))
perror("sem_wait");
if (sem_wait(mutex))
perror("sem_wait");
#ifdef DEBUG
printf("[C] %s in critical section...\n", (char *)str);
#endif
c = b->buffer[b->readpos];
b->readpos++;
if (b->readpos >= BUFFERSIZE)
b->readpos = 0;
if (sem_post(mutex))
perror("sem_post");
if (sem_post(b->sem_write))
perror("sem_post");
#ifdef DEBUG
printf("[C] %s outside critical section...\n", (char *)str);
#endif
printf(" %s: ", (char *)str);
putchar(c);
putchar('\n');
}
}
int main(void) {
char str0[] = "producer";
char str1[] = "consumer1";
char str2[] = "consumer2";
#ifdef DEBUG
printf("[M] start...\n");
#endif
b = (prot_buffer_t *)malloc(sizeof(prot_buffer_t));
b->readpos = 0;
b->writepos = 0;
b->sem_read =
sem_open("/AAU_T3_READ_SS2019", O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 0);
b->sem_write = sem_open("/AAU_T3_WRITE_SS2019", O_CREAT | O_EXCL,
S_IRUSR | S_IWUSR, BUFFERSIZE - 1);
mutex =
sem_open("/AAU_T3_MUTEX_SS2019", O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 1);
if (b->sem_read == SEM_FAILED || b->sem_write == SEM_FAILED ||
mutex == SEM_FAILED) {
perror("sem_open");
return EXIT_FAILURE;
}
#ifdef DEBUG
printf("[M] prot_buffer created...\n");
#endif
pthread_create(&producer_thread, NULL, producer, str0);
pthread_create(&consumer_thread1, NULL, consumer, str1);
pthread_create(&consumer_thread2, NULL, consumer, str2);
#ifdef DEBUG
printf("[M] threads started and now waiting for threads to join...\n");
#endif
// join waits for the threads to finish
pthread_detach(consumer_thread1);
pthread_detach(consumer_thread2);
pthread_join(producer_thread, NULL);
#ifdef DEBUG
printf("[M] close all semaphores...\n");
#endif
if (sem_close(b->sem_read))
perror("sem_close");
if (sem_close(b->sem_write))
perror("sem_close");
if (sem_close(mutex))
perror("sem_close");
#ifdef DEBUG
printf("[M] unlink all semaphores...\n");
#endif
if (sem_unlink("/AAU_T3_READ_SS2019"))
perror("sem_unlink");
if (sem_unlink("/AAU_T3_WRITE_SS2019"))
perror("sem_unlink");
if (sem_unlink("/AAU_T3_MUTEX_SS2019"))
perror("sem_unlink");
free(b);
#ifdef DEBUG
printf("[M] ready to termine...\n");
#endif
return EXIT_SUCCESS;
}