-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPthreads_Mutexes.c
More file actions
170 lines (159 loc) · 4.03 KB
/
Pthreads_Mutexes.c
File metadata and controls
170 lines (159 loc) · 4.03 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
Author: Taisia Zhizhina
Date: May 15 2024
This code uses mutexes to synchronize pthreads
*/
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
// bounds for rand
#define PADDLE_LOWER 1
#define PADDLE_UPPER 2
#define CANOE_LOWER 5
#define CANOE_UPPER 7
#define PKG_LOWER 4
#define PKG_UPPER 6
// global vars
static int T;
static int totalPaddle = 0;
static int totalCanoe = 0;
static int paddleStock = 0;
static int canoeStock = 0;
static int totalPkgs = 0;
// mutexes
static pthread_mutex_t paddleLock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t canoeLock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t pkgLock = PTHREAD_MUTEX_INITIALIZER;
void *createPaddle(void *arg)
{
time_t initTime;
time(&initTime);
while (difftime(time(NULL), initTime) < T)
{
pthread_mutex_lock(&paddleLock);
++totalPaddle;
++paddleStock;
pthread_mutex_unlock(&paddleLock);
printf("We have a paddle.\n");
int time = rand() % (PADDLE_UPPER - PADDLE_LOWER + 1) + PADDLE_LOWER;
sleep(time);
}
}
void *createCanoe(void *arg)
{
time_t initTime;
time(&initTime);
while (difftime(time(NULL), initTime) < T)
{
pthread_mutex_lock(&canoeLock);
++totalCanoe;
++canoeStock;
pthread_mutex_unlock(&canoeLock);
printf("We have a canoe.\n");
int time = rand() % (CANOE_UPPER - CANOE_LOWER + 1) + CANOE_LOWER;
sleep(time);
}
}
void *createShippment(void *arg)
{
time_t initTime;
time(&initTime);
while (difftime(time(NULL), initTime) < T)
{
if (canoeStock < 1 || paddleStock < 2)
continue;
pthread_mutex_lock(&pkgLock);
pthread_mutex_lock(&paddleLock);
paddleStock -= 2;
pthread_mutex_unlock(&paddleLock);
pthread_mutex_lock(&canoeLock);
--canoeStock;
pthread_mutex_unlock(&canoeLock);
++totalPkgs;
pthread_mutex_unlock(&pkgLock);
printf("We now have a shipment!\n");
int time = rand() % (PKG_UPPER - PKG_LOWER + 1) + PKG_LOWER;
sleep(time);
}
}
void *inventoryManager(void *arg)
{
time_t initTime;
time(&initTime);
while (difftime(time(NULL), initTime) < T)
{
getchar();
printf("%d Paddles and %d Canoes made - %d packages shipped!\n", totalPaddle, totalCanoe, totalPkgs);
}
}
int main(int argc, char *argv[])
{
if (argc != 5)
{
printf("wrong number of args\n");
return 1;
}
int P, C, S;
P = atoi(argv[1]);
C = atoi(argv[2]);
S = atoi(argv[3]);
T = atoi(argv[4]);
pthread_t paddleThreads[P];
pthread_t canoeThreads[C];
pthread_t shipperThreads[S];
pthread_t invManThread;
// create all threads
for (int i = 0; i < P; i++)
{
if (pthread_create(&paddleThreads[i], NULL, &createPaddle, NULL))
{
return 1; // error in creating thread
}
}
for (int i = 0; i < C; i++)
{
if (pthread_create(&canoeThreads[i], NULL, &createCanoe, NULL))
{
return 1; // error in creating thread
}
}
for (int i = 0; i < S; i++)
{
if (pthread_create(&shipperThreads[i], NULL, &createShippment, NULL))
{
return 1; // error in creating thread
}
}
if (pthread_create(&invManThread, NULL, &inventoryManager, NULL))
{
return 1; // error in creating thread
}
// join all threads
for (int i = 0; i < P; i++)
{
if (pthread_join(paddleThreads[i], NULL))
{
return 1; // error in joining thread
}
}
for (int i = 0; i < C; i++)
{
if (pthread_join(canoeThreads[i], NULL))
{
return 1; // error in joining thread
}
}
for (int i = 0; i < S; i++)
{
if (pthread_join(shipperThreads[i], NULL))
{
return 1; // error in joining thread
}
}
if (pthread_join(invManThread, NULL))
{
return 1; // error in joining thread
}
}