summaryrefslogtreecommitdiff
path: root/Year_2/OS/lookup-database.c
blob: d600898c5ab9acee6870ee8e4981ddea19c6d5a3 (plain)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#define DIM_MSG 1024

typedef struct {
    char name[256];
    int value;
} pairs;

typedef struct {
    int n;
    long value;
} out_row;

int
WAIT(int id_sem, int n)
{
    struct sembuf sops[1] = { { n, -1, 0 } };
    return semop(id_sem, sops, 1);
}

int
SIGNAL(int id_sem, int n)
{
    struct sembuf sops[1] = { { n, 1, 0 } };
    return semop(id_sem, sops, 1);
}

pairs*
find_row(char* name, pairs* rows, int n)
{
    for (int i = 0; i < n; ++i) {
        if (strcmp(rows[i].name, name) == 0)
            return &rows[i];
    }

    return NULL;
}

void
parse_rows(FILE* f, pairs* rows)
{
    int i, ci; /* ci stands for character index */
    char* token;
    char buf[1024];
    int pi; /* pi stands for pair index */
    i = ci = 0;
    char ch;

    while ((ch = (char)fgetc(f)) != EOF) {
        if (ch != '\n') {
            buf[ci++] = ch;
        } else {
            buf[ci] = '\0';
            token = strtok(buf, ";");
            if (token == NULL) {
                goto free_and_return;
            }

            pi = 0;
            while (token != NULL) {
                if (pi == 0) {
                    strcpy(rows[i].name, token);
                } else if (pi == 1) {
                    rows[i].value = atoi(token);
                } else {
                    goto free_and_return;
                }
                pi++;
                token = strtok(NULL, ";");
            }

            ci = 0;
            i++;
        }
    }

    return;

free_and_return:
    perror("<db-file> has a bad format");
    free(rows);
    exit(1);
}

void
db(pairs* rows, char* db_path, int id_shm_db_in, int id_shm_db_out, int id_sem, int fin_n)
{
    FILE* f;
    int n; /* It represents the number of lines of the file <db-file> */
    char ch;
    char* msg;
    char* msgout;
    int count_newline;
    char* token;
    int in_n;
    pairs* row;

    if ((f = fopen(db_path, "r")) == NULL) {
        perror("fopen");
        exit(1);
    }

    n = 0;
    while ((ch = (char)fgetc(f)) != EOF) {
        if (ch == '\n')
            n++;
    }

    fseek(f, 0, SEEK_SET);
    rows = malloc(sizeof(pairs) * n);
    parse_rows(f, rows);
    fclose(f);

    printf("DB: letti n. %d record da file\n", n);

    if ((msg = (char*)shmat(id_shm_db_in, NULL, 0)) == (char*)-1) {
        perror("shmat");
        exit(1);
    }

    if ((msgout = (char*)shmat(id_shm_db_out, NULL, 0)) == (char*)-1) {
        perror("shmat");
        exit(1);
    }

    count_newline = 0;
    while (count_newline < fin_n) {
        WAIT(id_sem, 0);
        if (strcmp(msg, "") == 0) {
            count_newline++;
        } else {
            token = strtok(msg, ";");
            in_n = atoi(token);
            token = strtok(NULL, ";");
            row = find_row(token, rows, n);
            if (row == NULL) {
                printf("DB: query '%s' da IN%d non trovata\n", token, in_n);
            } else {
                WAIT(id_sem, 3);
                sprintf(msgout, "%d;%s;%d", in_n, row->name, row->value);
                SIGNAL(id_sem, 2);
                printf("DB: query '%s' da IN%d trovata con valore %d\n", token, in_n, row->value);
            }
        }
        SIGNAL(id_sem, 1);
    }

    WAIT(id_sem, 3);
    strcpy(msgout, "");
    SIGNAL(id_sem, 2);
}

void
out(int id_shm_db_out, int id_sem, int fin_n)
{
    char* msg;
    out_row* processes; /* at i-index there is the data of process-i */
    char* token;
    int* n = &fin_n;
    int index;
    int i;

    processes = malloc(sizeof(out_row) * (*n));
    for (i = 0; i < *n; ++i) {
        processes[i].n = 0;
        processes[i].value = 0;
    }

    if ((msg = (char*)shmat(id_shm_db_out, NULL, 0)) == (char*)-1) {
        perror("shmat");
        exit(1);
    }

    while (1) {
        WAIT(id_sem, 2);

        if (strcmp(msg, "") == 0)
            break;

        token = strtok(msg, ";");
        index = atoi(token);
        strtok(NULL, ";"); /* Ignore the `name` param */
        token = strtok(NULL, ";");
        processes[index - 1].n++;
        processes[index - 1].value += atol(token);

        SIGNAL(id_sem, 3);
    }

    for (i = 0; i < *n; ++i) {
        printf("OUT: ricevuti n.%d valori validi per IN%d con totale %lu\n", processes[i].n, i + 1, processes[i].value);
    }

    free(processes);
}

void
fn_in(int id_in, char* input_file, int id_shm_db_in, int id_sem)
{
    FILE* f;
    char buf[DIM_MSG];
    char ch;
    int ci;
    char* msg;
    int i;
    char id_in_str[5]; /* Max process is 99999 */

    sprintf(id_in_str, "%d", id_in);

    if ((f = fopen(input_file, "r")) == NULL) {
        perror("fopen");
        exit(1);
    }

    if ((msg = (char*)shmat(id_shm_db_in, NULL, 0)) == (char*)-1) {
        perror("shmat");
        exit(1);
    }

    ci = strlen(id_in_str) + 1;
    for (i = 0; i < strlen(id_in_str); ++i) {
        buf[i] = id_in_str[i];
    }
    buf[i] = ';';

    i = 1;

    while ((ch = fgetc(f)) != EOF) {
        buf[ci++] = ch;
        if (ch == '\n') {
            buf[ci - 1] = '\0';

            WAIT(id_sem, 1);
            /* Write the `buf` into the shared memory buffer with db */
            strncpy(msg, buf, ci);

            printf("IN%d: inviata query n.%d '%s'\n", id_in, i, msg + strlen(id_in_str) + 1);

            ci = strlen(id_in_str) + 1;
            SIGNAL(id_sem, 0);
            i++;
        }
    }
    WAIT(id_sem, 1);
    strncpy(msg, "", 1);
    SIGNAL(id_sem, 0);

    fclose(f);
}

int
main(int argc, char** argv)
{
    int i;
    int id_shm_db_in;
    int id_shm_db_out;
    int id_sem;

    pid_t pid_db;
    pid_t pid_out;
    pid_t pid_in[argc - 2];

    pairs* rows;
    rows = NULL;

    if (argc < 3) {
        fprintf(stderr, "Usage: %s <db-file> <query-file-1> [<query-file-2>] .. [<query-file-n>]", argv[0]);
        exit(1);
    }

    /* Create the shared memory blocks */
    if ((id_shm_db_in = shmget(IPC_PRIVATE, DIM_MSG, IPC_CREAT | 0644)) == -1) {
        perror("shmget");
        exit(1);
    }

    if ((id_shm_db_out = shmget(IPC_PRIVATE, DIM_MSG, IPC_CREAT | 0644)) == -1) {
        perror("shmget");
        exit(1);
    }

    /* These are two semaphore used as mutexs */
    if ((id_sem = semget(IPC_PRIVATE, 4, IPC_CREAT | IPC_EXCL | 0644)) == -1) {
        perror("semget");
        exit(1);
    }
    /* Set values to 0 */
    semctl(id_sem, 0, SETVAL, 0);
    semctl(id_sem, 1, SETVAL, 1);
    semctl(id_sem, 2, SETVAL, 0);
    semctl(id_sem, 3, SETVAL, 1);

    pid_db = fork();
    if (pid_db == -1) {
        perror("fork");
        exit(1);
    }

    if (pid_db == 0) {
        db(rows, argv[1], id_shm_db_in, id_shm_db_out, id_sem, argc - 2);
        exit(0);
    }

    pid_out = fork();
    if (pid_out == -1) {
        perror("fork");
        exit(1);
    }

    if (pid_out == 0) {
        out(id_shm_db_out, id_sem, argc - 2);
        exit(0);
    }

    for (i = 0; i < argc - 2; ++i) {
        pid_in[i] = fork();
        if (pid_in[i] == -1) {
            perror("fork");
            exit(1);
        }

        if (pid_in[i] == 0) {
            fn_in(i + 1, argv[i + 2], id_shm_db_in, id_sem);
            exit(0);
        }
    }

    for (i = 0; i < argc; ++i)
        wait(NULL);

    shmctl(id_shm_db_in, IPC_RMID, 0);
    shmctl(id_shm_db_out, IPC_RMID, 0);
    semctl(id_sem, 4, IPC_RMID);
    free(rows);

    return 0;
}