-
Notifications
You must be signed in to change notification settings - Fork 0
/
psearch3slave.c
168 lines (139 loc) · 3.96 KB
/
psearch3slave.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <linux/mman.h>
#include <fcntl.h>
#include <memory.h>
#define ROOT_DIR "./"
#define WORD_BUFFER 100
#define LINE_BUFFER 4096
#define MAX_MATCHED_LINES 2048
#define SMEM_BUFFER 10240
#define SHD_FNAME "./shared_output.txt"
int main(int argc, int **argv)
{
if (argc < 1)
{
printf("[SLAVE] Number of input files must be greater than 1!\n");
return 0;
}
char *searchKeyword = argv[1];
char *inputFile = argv[2];
char currentInputFileDir[100];
// Make a complete directory for the input file
sprintf(currentInputFileDir, "%s%s", ROOT_DIR, inputFile);
// printf("Current file dir: %s\n", currentInputFileDir);
if (currentInputFileDir == NULL)
{
perror("[SLAVE] Current file directory is NULL!\n");
return 1;
}
FILE *inputFileStream; /* single stream for a file */
// Open file content
if ((inputFileStream = fopen(currentInputFileDir, "r")) == NULL)
{
perror("[SLAVE] Error opening input file!\n");
exit(-1);
}
// Create matched lines indices array
int matchedLinesIndices[MAX_MATCHED_LINES];
for (int b = 0; b < MAX_MATCHED_LINES; b++)
{
matchedLinesIndices[b] = 0;
}
int matchedLinesCount = 0;
int myChar;
int lineCount = 0;
char word[WORD_BUFFER] = {0x0};
int wordChCount = 0;
// Read file by character to find matched line indices
while (((myChar = fgetc(inputFileStream)) != EOF))
{
if (myChar == '\n')
{
lineCount++;
}
// Before beginning a new word
if (myChar == ' ' || myChar == '\n' || myChar == '\0' || myChar == '\t')
{
// Is it the keyword?
if (strcmp(word, searchKeyword) == 0)
{
matchedLinesIndices[matchedLinesCount] = lineCount + 1;
matchedLinesCount++;
}
// Reset word
word[0] = '\0';
wordChCount = 0;
}
else
{
word[wordChCount++] = (char)myChar;
word[wordChCount] = '\0';
}
}
char matchedLines[MAX_MATCHED_LINES][WORD_BUFFER];
char line[LINE_BUFFER] = {0x0};
// Reset input stream
rewind(inputFileStream);
// GET lines by the matched lines indices
int j = 1;
while ((fgets(line, LINE_BUFFER, inputFileStream)) != NULL)
{
strcpy(matchedLines[j], line);
j++;
}
fclose(inputFileStream);
/*
COMPOSE A MESSAGE TO SEND SHARED MEMORY
format: <input_file>, <matched_line_index>: <matched_line>
*/
int l = 0;
char msg[SMEM_BUFFER] = {0x0};
// printf("Message sent to SHARED MEMORY:\n");
for (int k = 0; k < MAX_MATCHED_LINES; k++)
{
if (k == matchedLinesIndices[l])
{
char line[LINE_BUFFER] = {0x0};
sprintf(line, "%s, %d: %s", inputFile, k, matchedLines[k]);
strcat(msg, line);
// printf("%s\n", msg);
l++;
}
}
int fd = open(SHD_FNAME, O_CREAT | O_RDWR, (mode_t)0777);
if (fd < 0)
{
perror("[MASTER]Error opening file descriptor!\n");
exit(-1);
}
//sleep(1);
struct stat fstatus;
fstat(fd, &fstatus);
off_t fstatus_s = fstatus.st_size;
fallocate(fd, 0, fstatus_s, strlen(msg));
char *shdmem = (char *)mmap(0, fstatus_s + strlen(msg), PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
if (shdmem == MAP_FAILED)
{
close(fd);
perror("[SLAVE] Error opening shared memory!\n");
exit(-1);
}
for (int i = 0; i < strlen(msg); i++)
{
shdmem[fstatus_s + i] = msg[i];
}
if (munmap(shdmem, strlen(shdmem)) == -1)
{
close(fd);
perror("[SLAVE] Error freeing shared memory!\n");
exit(-1);
}
close(fd);
return 0;
}