-
Notifications
You must be signed in to change notification settings - Fork 0
/
Conditional_Variables.c
executable file
·68 lines (63 loc) · 1.64 KB
/
Conditional_Variables.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
#include "pthread.h"
#include "stdio.h"
#include "unistd.h"
#include "string.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <semaphore.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#define RUNS 5
int fuel = 0;
pthread_mutex_t mutexFuel;
pthread_cond_t condFuel;
void* fuelFilling() {
for(int i = 0; i < RUNS; i++){
pthread_mutex_lock(&mutexFuel);
fuel += 15;
printf("Fuel filled: %d\n", fuel);
pthread_mutex_unlock(&mutexFuel);
pthread_cond_broadcast(&condFuel);
sleep(1);
}
}
void* car() {
pthread_mutex_lock(&mutexFuel);
while(fuel < 40){
printf("No Fuel. Waiting...\n");
pthread_cond_wait(&condFuel, &mutexFuel);
// Equivalent to:
// pthread_mutex_unlock(&mutexFuel);
// wait for signal on condFuel
// pthread_mutex_lock(&mutexFuel);
}
fuel -= 40;
printf("Got Fuel. Now left: %d\n", fuel);
pthread_mutex_unlock(&mutexFuel);
}
int main() {
pthread_t th[6];
pthread_mutex_init(&mutexFuel, NULL);
pthread_cond_init(&condFuel, NULL);
for(int i = 0; i < 6; i++){
if(i < 4){
if(pthread_create(&th[i], NULL, &car, NULL) != 0){
perror("Failed to create thread");
}
} else {
if(pthread_create(&th[i], NULL, &fuelFilling, NULL) != 0){
perror("Failed to create thread");
}
}
}
for(int i = 0; i < 6; i++){
pthread_join(th[i], NULL);
}
pthread_mutex_destroy(&mutexFuel);
pthread_cond_destroy(&condFuel);
return 0;
}