-
Notifications
You must be signed in to change notification settings - Fork 0
/
26-semaphores-ordering.c
71 lines (56 loc) · 1.48 KB
/
26-semaphores-ordering.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
// Run Using "gcc -pthread 26_semaphores_ordering.c && ./a.out"
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define PRO 5
int order[] = {2, 4, 3, 1, 0};
sem_t sem[PRO];
typedef struct str {
sem_t* wait;
sem_t* signal;
int id;
} str;
void* process(void* vargp) {
str* s = (str*)vargp;
sem_t* wait = s->wait;
sem_t* signal = s->signal;
int id = s->id;
free(s);
if (wait != NULL)
sem_wait(wait);
printf("Process: %d\n", id);
if (signal != NULL)
sem_post(signal);
return NULL;
}
int main() {
pthread_t tid[PRO];
for (int i = 0; i < PRO; i++) {
sem_init(&sem[i], 0, 0);
}
printf("Default Order:\n");
for (int i = 0; i < PRO; i++) {
str* s = (str*)malloc(sizeof(str));
s->wait = i == 0 ? NULL : &sem[i - 1];
s->signal = i == PRO - 1 ? NULL : &sem[i];
s->id = i;
pthread_create(&tid[i], NULL, process, (void*)s);
}
for (int i = 0; i < PRO; i++) {
pthread_join(tid[i], NULL);
}
printf("Predefined Order:\n");
for (int i = 0; i < PRO; i++) {
str* s = (str*)malloc(sizeof(str));
s->wait = order[i] == 0 ? NULL : &sem[order[i] - 1];
s->signal = order[i] == PRO - 1 ? NULL : &sem[order[i]];
s->id = i;
pthread_create(&tid[i], NULL, process, (void*)s);
}
for (int i = 0; i < PRO; i++) {
pthread_join(tid[i], NULL);
}
return 0;
}