-
Notifications
You must be signed in to change notification settings - Fork 0
/
master-worker.c
169 lines (132 loc) · 4.57 KB
/
master-worker.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
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <wait.h>
#include <pthread.h>
int item_to_produce, curr_buf_size;
int total_items, max_buf_size, num_workers, num_masters;
int *buffer;
pthread_mutex_t *lock;
pthread_cond_t *empty_cond, *fill_cond;
void print_produced(int num, int master) {
printf("Produced %d by master %d\n", num, master);
}
void print_consumed(int num, int worker) {
printf("Consumed %d by worker %d\n", num, worker);
}
//produce items and place in buffer
//modify code below to synchronize correctly
void *generate_requests_loop(void *data)
{
int thread_id = *((int *)data);
while(1)
{
pthread_mutex_lock(lock);
while (curr_buf_size == max_buf_size){
pthread_cond_wait(empty_cond, lock);
}
// it's required to check if all the items have been produced
// after lock has been acquired because, it might happen that
// while waiting for mutex lock, a producer has already
// produced it. So, without checking it again before producing might
// result in producing more items than expected.
if (item_to_produce >= total_items) {
pthread_mutex_unlock(lock);
return 0;
}
buffer[curr_buf_size++] = item_to_produce;
print_produced(item_to_produce, thread_id);
item_to_produce++;
pthread_cond_signal(fill_cond);
pthread_mutex_unlock(lock);
}
return 0;
}
//write function to be run by worker threads
//ensure that the workers call the function print_consumed when they consume an item
void *remove_requests_loop(void *data)
{
int thread_id = *((int *)data);
while (1) {
pthread_mutex_lock(lock);
while (curr_buf_size == 0) {
// It is required to check if all the items have already been produced
// before waiting for any producer. If we don't do this then the
// consumer will wait forever for a producer to produce something
// while there is nothing left to produce.
if (item_to_produce >= total_items) {
pthread_mutex_unlock(lock);
return 0;
}
pthread_cond_wait(fill_cond, lock);
}
int item_to_consume = buffer[--curr_buf_size];
print_consumed(item_to_consume, thread_id);
pthread_cond_signal(empty_cond);
pthread_mutex_unlock(lock);
}
return 0;
}
int main(int argc, char *argv[])
{
int *master_thread_id, *worker_thread_id;
pthread_t *master_thread, *worker_thread;
item_to_produce = 0;
curr_buf_size = 0;
int i;
if (argc < 5) {
printf("./master-worker #total_items #max_buf_size #num_workers #masters e.g. ./exe 10000 1000 4 3\n");
exit(1);
}
else {
num_masters = atoi(argv[4]);
num_workers = atoi(argv[3]);
total_items = atoi(argv[1]);
max_buf_size = atoi(argv[2]);
}
buffer = (int *)malloc (sizeof(int) * max_buf_size);
// create mutex and csv instances
lock = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
empty_cond = (pthread_cond_t *)malloc(sizeof(pthread_cond_t));
fill_cond = (pthread_cond_t *)malloc(sizeof(pthread_cond_t));
pthread_mutex_init(lock, NULL);
pthread_cond_init(empty_cond, NULL);
pthread_cond_init(fill_cond, NULL);
//create master producer threads
master_thread_id = (int *)malloc(sizeof(int) * num_masters);
master_thread = (pthread_t *)malloc(sizeof(pthread_t) * num_masters);
for (i = 0; i < num_masters; i++)
master_thread_id[i] = i;
for (i = 0; i < num_masters; i++)
pthread_create(&master_thread[i], NULL, generate_requests_loop, (void *)&master_thread_id[i]);
//create worker consumer threads
worker_thread_id = (int *)malloc(sizeof(int) * num_workers);
worker_thread = (pthread_t *)malloc(sizeof(pthread_t) * num_workers);
for (i = 0; i < num_workers; i++)
worker_thread_id[i] = i;
for (i = 0; i < num_workers; i++)
pthread_create(&worker_thread[i], NULL, remove_requests_loop, (void *)&worker_thread_id[i]);
//wait for all threads to complete
for (i = 0; i < num_masters; i++)
{
pthread_join(master_thread[i], NULL);
printf("master %d joined\n", i);
}
for (i = 0; i < num_workers; i++) {
pthread_join(worker_thread[i], NULL);
printf("worker %d joined\n", i);
}
/*----Deallocating Buffers---------------------*/
free(buffer);
free(worker_thread);
free(master_thread);
free(worker_thread_id);
free(master_thread_id);
free(lock);
free(empty_cond);
free(fill_cond);
return 0;
}