forked from PanagiotisNtymenos/POSIX-Threads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprodcons2.c
46 lines (39 loc) · 1.3 KB
/
prodcons2.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
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "prodcons.h"
circular_buffer cb;
int main(int argc, char *argv[]) {
if(argc == 6) {
f2 = fopen("prod_in.txt", "w");
f1 = fopen("cons_out.txt", "w");
producerscount=atoi(argv[1]);
endLoop = atoi(argv[1]) * atoi(argv[4]);
cb_init(&cb, atoi(argv[3]), sizeof(int));
pthread_mutex_init(&mu, NULL);
pthread_cond_init(¬Ready, NULL);
pthread_t pro[atoi(argv[1])];
PRODUCER_ARGUMENTS producerArgs[atoi(argv[1])];
for (int i=1; i<=atoi(argv[1]); i++){
producerArgs[i-1].prod_id = i;
producerArgs[i-1].seed = atoi(argv[5]);
producerArgs[i-1].total = atoi(argv[4]);
rc = pthread_create(&pro[i-1], NULL, producer, &producerArgs[i-1]);
}
CONSUMER_ARGUMENTS consumerArgs[atoi(argv[2])];
pthread_t con[atoi(argv[2])];
for (int i=1; i<=atoi(argv[2]); i++) {
consumerArgs[i-1].con_id = i;
rc = pthread_create(&con[i-1], NULL, consumer, &consumerArgs[i-1]);
}
for(int i=1; i<=atoi(argv[1]); i++) pthread_join(pro[i-1], NULL);
for(int i=1; i<=atoi(argv[2]); i++) pthread_join(con[i-1], NULL);
pthread_mutex_destroy(&mu);
pthread_cond_destroy(¬Ready);
cb_free(&cb);
return 0;
} else {
printf("Wrong argument format!\n");
}
}