Repository navigation
Expand file tree
/
Copy pathpart2.c
More file actions
96 lines (79 loc) · 2.82 KB
/
Copy pathpart2.c
File metadata and controls
96 lines (79 loc) · 2.82 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
/*
Author: Marc Lee (Changhwan)
duckid: clee3
Title: CIS415 project2 part2
This is my own work except the making multiple threads is from
pthread_create man page
*/
#ifndef PART2_C
#define PART2_C
#include "threadfxn.h"
int main(int argc, char* argv[]) {
/* Pub and sub Thread Pool */
thread_info *pub_info, *sub_info;
pthread_t clean;
int tnum, i = 0;
char* string[NUMPROXIES + NUMPROXIES] = {"ABC", "DEF", "GHI", "JKL", "MNO", "PQR"};
pub_info = (thread_info *) calloc(NUMPROXIES, sizeof(thread_info));
sub_info = (thread_info *) calloc(NUMPROXIES, sizeof(thread_info));
if (pub_info == NULL) {
fprintf(stderr, "Create Pub thread pool Failed\n");
exit(EXIT_FAILURE);
}
if (sub_info == NULL) {
fprintf(stderr, "Create Sub thread pool Failed\n");
exit(EXIT_FAILURE);
}
/* Create Publisher thread pool */
for (tnum = 0; tnum < NUMPROXIES; tnum++) {
pub_info[tnum].thread_num = tnum+1;
pub_info[tnum].str = strdup(string[i]);
i++;
if (pthread_create(&pub_info[tnum].thread_id, NULL, &publisher, &pub_info[tnum]) != 0) {
fprintf(stderr, "Create publisher thread Failed! index: %d\n", tnum);
exit(EXIT_FAILURE);
}
}
/* Create Subscriber thread pool */
for (tnum = 0; tnum < NUMPROXIES; tnum++) {
sub_info[tnum].thread_num = tnum+1;
sub_info[tnum].str = strdup(string[i]);
i++;
if (pthread_create(&sub_info[tnum].thread_id, NULL, &subscriber, &sub_info[tnum]) != 0) {
fprintf(stderr, "Create subscriber thread Failed! index: %d\n", tnum);
exit(EXIT_FAILURE);
}
}
if (pthread_create(&clean, NULL, &cleanup, NULL) != 0) {
fprintf(stderr, "Create cleanup thread failed!\n");
exit(EXIT_FAILURE);
}
init();
printf("Start!\n");
flag = 1;
for (tnum = 0; tnum < NUMPROXIES; tnum++) {
/* If thread is done, wait for other threads to terminate */
if (pthread_join(pub_info[tnum].thread_id, NULL) != 0) {
fprintf(stderr, "Join pub thread is failed! Thread pool index: %d\n", tnum);
exit(EXIT_FAILURE);
}
}
for (tnum = 0; tnum < NUMPROXIES; tnum++) {
if (pthread_join(sub_info[tnum].thread_id, NULL) != 0) {
fprintf(stderr, "Join sub thread is failed! Thread pool index: %d\n", tnum);
exit(EXIT_FAILURE);
}
}
done = 1;
if (pthread_join(clean, NULL) != 0) {
fprintf(stderr, "Join CleanUp Thread is failed!\n");
exit(EXIT_FAILURE);
}
free(pub_info);
free(sub_info);
/* destroy mutex locks */
pthread_mutex_destroy(&plock);
pthread_mutex_destroy(&slock);
return 0;
}
#endif