-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpword.c
165 lines (124 loc) · 3.9 KB
/
pword.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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <mqueue.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include "tree.h"
#define MIN_ARGS 4
#define MAX_FILES 8
#define MAX_SIZE 64
#define MQNAME "/MQNAME"
#define STOP_SIGNAL "EOF"
char files[MAX_FILES][255];
char *toUpperCase(char *str);
int main(int argc, char const *argv[])
{
int file_num;
int message_size;
mqd_t mq;
struct mq_attr mq_attr;
struct timeval time1, time2;
if (argc < MIN_ARGS || argc > MIN_ARGS + MAX_FILES)
{
fprintf(stderr, "Argument number is inappropriate!\n");
exit(-1);
}
else
{
file_num = atoi(argv[3]);
message_size = atoi(argv[1]);
if (argc != MIN_ARGS + file_num)
{
fprintf(stderr, "File number and existing files are incompatible!\n");
exit(-1);
}
else
{
for (int i = 0; i < file_num; i++)
{
strcpy(files[i], argv[MIN_ARGS + i]); // storing the file names
}
gettimeofday(&time1, NULL);
mq = mq_open(MQNAME, O_RDWR | O_CREAT, 0666, NULL);
mq_getattr(mq, &mq_attr);
int bufflen = mq_attr.mq_msgsize;
char *bufferp;
if (mq == -1)
{
perror("Parent) Message Queue cannot be opened!\n");
exit(1);
}
for(int i = 0; i < file_num; i++)
{
pid_t n = fork();
if(n == 0) // child process execution
{
mq = mq_open(MQNAME, O_RDWR);
bufferp = (char *) malloc(bufflen);
char str[message_size];
strcpy(str, "");
FILE *fp = fopen(files[i], "r");
char file_str[MAX_SIZE] = "";
while ( fscanf(fp, "%s", file_str) != EOF )
{
if (strlen(str) + strlen(file_str) + 1 > message_size)
{
mq_send(mq, str, message_size, 0);
strcpy(str, "");
}
strcat(str, file_str);
strcat(str, " ");
}
mq_send(mq, str, message_size, 0);
mq_send(mq, STOP_SIGNAL, message_size, 0);
mq_close(mq);
fclose(fp);
exit(0);
}
}
int stop_count = 0;
struct Node *root = NULL;
while (stop_count != file_num)
{
int bufferlen = mq_attr.mq_msgsize;
bufferp = (char *) malloc(bufferlen);
mq_receive(mq, bufferp, bufferlen, NULL);
if (strcmp(bufferp, STOP_SIGNAL) == 0)
{
stop_count++;
}
else
{
char *token = strtok(bufferp, " ");
while( token != NULL )
{
insert(&root, toUpperCase(token));
token = strtok(NULL, " ");
}
}
free(bufferp);
}
FILE *fp = fopen(argv[2], "w");
writeInorder(root, fp);
fclose(fp);
mq_close(mq);
mq_unlink(MQNAME);
gettimeofday(&time2, NULL);
printf("Total time = %ld miliseconds\n", (time2.tv_usec - time1.tv_usec) );
}
}
return 0;
}
char *toUpperCase(char *str)
{
char *result = str;
for (int i = 0; i < strlen(str); i++)
{
result[i] = toupper(result[i]);
}
return result;
}