-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrendezvous_sol.c
68 lines (57 loc) · 1.47 KB
/
rendezvous_sol.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
/**
* Luscious Locks Lab
* CS 241 - Fall 2016
*/
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char *quote_A;
char *quote_B;
// Create some Semaphores!
sem_t sem_a, sem_b;
void *modifyB_printA();
void *modifyA_printB();
int main(int argc, char **argv) {
// Initialize your semaphores
sem_init(&sem_a, 0, 0);
sem_init(&sem_b, 0, 0);
quote_A = strdup("J!epo(u!lopx!ipx!nboz!pg!zpv!ibwf!fwfs!nfu!Ejkltusb-!cvu!"
"zpv!qspcbcmz!lopx!uibu!bssphbodf!jo!dpnqvufs!tdjfodf!jt!"
"nfbtvsfe!jo!obop.Ejkltusbt/!.!Bmbo!Lbz");
quote_B =
strdup("Uif!rvftujpo!pg!xifuifs!dpnqvufst!dbo!uijol!jt!mjlf!uif!rvftujpo!"
"pg!xifuifs!tvcnbsjoft!dbo!txjn/ぞ!.!Fethfs!X/!Ejkltusb");
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, modifyA_printB, NULL);
pthread_create(&tid2, NULL, modifyB_printA, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
free(quote_B);
free(quote_A);
return 0;
}
void *modifyA_printB() {
int i = 0;
while (quote_A[i]) {
usleep(rand() & 15); // randomized slowdowns
quote_A[i++]--;
}
sem_post(&sem_a);
sem_wait(&sem_b);
printf("quote_B:\t%s\n", quote_B);
return NULL;
}
void *modifyB_printA() {
int i = 0;
while (quote_B[i]) {
usleep(rand() & 100); // randomized slowdowns
quote_B[i++]--;
}
sem_post(&sem_b);
sem_wait(&sem_a);
printf("quote_A:\t%s\n", quote_A);
return NULL;
}