-
Notifications
You must be signed in to change notification settings - Fork 0
/
semaphores.c
80 lines (56 loc) · 1.34 KB
/
semaphores.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
/*
* Compiler flags: -Wall -Werror -Wextra -std=gnu11 -pthread
*/
#include <stdlib.h>
#include <unistd.h>
#include <semaphore.h>
#include <fcntl.h>
#include <wait.h>
#include <sys/stat.h>
#define SEMAPHORE1_NAME "/sem1"
#define SEMAPHORE2_NAME "/sem2"
#define BUFFER_SIZE 10
#define ITERATIONS 50
//setting up semaphores
sem_t * semaphore1;
sem_t * semaphore2;
//initializing buffer
char buffer[BUFFER_SIZE];
void producer( void ) {
for (int i = 0; i <= ITERATIONS; i++) {
sem_wait(semaphore1);
//add stuff to buffer via buffer[i % BUFFER_SIZE]
sem_post(semaphore2);
}
exit(EXIT_SUCCESS);
}
void consumer( void ) {
for (int i = 0; i <= ITERATIONS; i++) {
sem_wait(semaphore2);
//remove stuff from buffer via buffer[i % BUFFER_SIZE]
sem_post(semaphore1);
}
}
int main(int argc, char const *argv[]) {
//initializing semaphores
semaphore1 = sem_open(SEMAPHORE1_NAME, O_CREAT, 0666, BUFFER_SIZE);
semaphore2 = sem_open(SEMAPHORE2_NAME, O_CREAT, 0666, 0);
int pid;
switch (( pid = fork() )) {
case -1:
//fork error
break;
case 0:
producer();
break;
default:
consumer();
}
waitpid(pid, NULL, 0);
//cleaning semaphores up
sem_close(semaphore1);
sem_close(semaphore2);
sem_unlink(SEMAPHORE1_NAME);
sem_unlink(SEMAPHORE2_NAME);
return EXIT_SUCCESS;
}