-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
98 lines (79 loc) · 2.23 KB
/
main.c
File metadata and controls
98 lines (79 loc) · 2.23 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
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
/*
* _____P4____
* / \
* P3------P5-----P8
* \_____P6____/
*/
pthread_mutex_t mutex;
pthread_cond_t cond_p3_done, cond_p4p5p6_done;
void simulate_job(int seconds, char process) {
while (seconds) {
printf("(%ds) Working from P%c...\n", seconds, process);
seconds--;
sleep(1);
}
printf("(%ds) Working from P%c...\n", seconds, process);
}
void *P3(void *arg) {
printf("P3: Starting\n");
simulate_job(3, '3'); // Simulate P3 work 3sec
printf("P3: Done\n");
pthread_cond_signal(&cond_p3_done);
return NULL;
}
void *P4(void *arg) {
printf("P4: Starting\n");
simulate_job(5, '4'); // Simulate P4 work 5sec
printf("P4: Done\n");
pthread_cond_signal(&cond_p4p5p6_done);
return NULL;
}
void *P5(void *arg) {
printf("P5: Starting\n");
simulate_job(3, '5'); // Simulate P5 work 3sec
printf("P5: Done\n");
pthread_cond_signal(&cond_p4p5p6_done);
return NULL;
}
void *P6(void *arg) {
printf("P6: Starting\n");
simulate_job(10, '6'); // Simulate P6 work 10sec
printf("P6: Done\n");
pthread_cond_signal(&cond_p4p5p6_done);
return NULL;
}
void *P8(void *arg) {
printf("P8: Starting\n");
simulate_job(2, '8'); // Simulate P8 work 2sec
printf("P8: Done\n");
return NULL;
}
int main() {
pthread_t p3, p4, p5, p6, p8;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond_p3_done, NULL);
pthread_cond_init(&cond_p4p5p6_done, NULL);
pthread_create(&p3, NULL, P3, NULL);
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond_p3_done, &mutex);
pthread_mutex_unlock(&mutex);
pthread_join(p3, NULL);
pthread_create(&p4, NULL, P4, NULL);
pthread_create(&p5, NULL, P5, NULL);
pthread_create(&p6, NULL, P6, NULL);
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond_p4p5p6_done, &mutex);
pthread_mutex_unlock(&mutex);
pthread_join(p4, NULL);
pthread_join(p5, NULL);
pthread_join(p6, NULL);
pthread_create(&p8, NULL, P8, NULL);
pthread_join(p8, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond_p3_done);
pthread_cond_destroy(&cond_p4p5p6_done);
return 0;
}