summaryrefslogtreecommitdiff
path: root/Year_2/OS/my-dus-c.c
blob: 5961c779e03c09209b8110ed9b3595825c45679a (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
/*
 * Test the equivalent execution of this propgram with `du`:
 *
 * du --block-size=512 -s <path>
 *
 */
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#define MAX_PATH_LEN 1024

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);
}

void
handle_scanner_end(int id_shm, int id_sem, int id_scanner, char* pathname)
{
    char* msg;

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

    WAIT(id_sem, 0);
    sprintf(msg, "%d;END;%s", id_scanner, pathname);
    SIGNAL(id_sem, 1);
}

void
stater(int id_shm, int id_sem, int* scanners, int scanners_n)
{
    char* msg;
    int n;
    struct stat sb;
    char* token;
    char* pathname;
    int sid;

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

    n = 0;
    while (n < scanners_n) {
        WAIT(id_sem, 1);
        /* I previously opened the file with `open` so I know I can read
             * that. So now I open that file with stat, because `msg` contains
             * the patname as string.
             * But first I have to parse the `msg` receiving the scanner id.
             */
        token = strtok(msg, ";");
        sid = atoi(token);
        pathname = strtok(NULL, ";");

        if (strcmp(pathname, "END") == 0) {
            pathname = strtok(NULL, ";");
            printf("%d\t%s\n", scanners[sid], pathname);
            n++;
        } else {
            stat(pathname, &sb);
            scanners[sid] += sb.st_blocks;
        }
        SIGNAL(id_sem, 0);
    }
}

void
scanner(int id_shm, int id_sem, int scanner_id, char* pathname)
{
    char* msg;
    struct stat sb;
    struct dirent* db;
    int fd;
    DIR* dp;
    char buffer[MAX_PATH_LEN];

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

    if ((fd = open(pathname, O_RDONLY)) == -1) {
        perror("open");
        exit(1);
    }

    fstat(fd, &sb);

    if ((sb.st_mode & S_IFMT) == S_IFDIR) {
        if ((dp = opendir(pathname)) == NULL) {
            perror("opendir");
            exit(1);
        }

        while ((db = readdir(dp)) != NULL) {
            if ((strcmp(db->d_name, ".") == 0) || strcmp(db->d_name, "..") == 0)
                continue;
            sprintf(buffer, "%s/%s", pathname, db->d_name);
            scanner(id_shm, id_sem, scanner_id, buffer);
        }

        closedir(dp);
    }

    WAIT(id_sem, 0);
    sprintf(msg, "%d;%s", scanner_id, pathname);
    SIGNAL(id_sem, 1);
}

int
main(int argc, char* argv[])
{
    int id_shm;
    int i;
    pid_t pid;
    pid_t pid_scanners[argc - 1];
    int scanners[argc - 1];
    int id_sem;

    for (i = 0; i < argc - 1; ++i)
        scanners[i] = 0;

    if ((id_shm = shmget(IPC_PRIVATE, 1024, IPC_CREAT | IPC_EXCL | 0644)) == -1) {
        perror("shmget");
        exit(1);
    }

    if ((id_sem = semget(IPC_PRIVATE, 2, IPC_CREAT | IPC_EXCL | 0644)) == -1) {
        perror("semget");
        exit(1);
    }

    semctl(id_sem, 0, SETVAL, 1);
    semctl(id_sem, 1, SETVAL, 0);

    if (argc < 2) {
        exit(0);
    }

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

    if (pid == 0) {
        stater(id_shm, id_sem, scanners, argc - 1);
        exit(0);
    }

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

        if (pid_scanners[i - 1] == 0) {
            scanner(id_shm, id_sem, i - 1, argv[i]);
            handle_scanner_end(id_shm, id_sem, i - 1, argv[i]);
            exit(0);
        }
    }

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

    if (shmctl(id_shm, IPC_RMID, NULL) == -1) {
        perror("shmctl");
        exit(1);
    }

    if (semctl(id_sem, 2, IPC_RMID) == -1) {
        perror("semctl");
        exit(1);
    }

    return 0;
}