-
Notifications
You must be signed in to change notification settings - Fork 0
/
task1.c
164 lines (138 loc) · 4.57 KB
/
task1.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
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#define PIPE_BUF 4096
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
void printSelect(int fdWS, int fdMW, int fdDB) {
fd_set rfds;
int res;
char buf[PIPE_BUF];
/* Initialize the file descriptor set. */
FD_ZERO(&rfds);
FD_SET(fdWS, &rfds);
FD_SET(fdMW, &rfds);
FD_SET(fdDB, &rfds);
// highest fifo
int maxfd = MAX(fdWS, MAX(fdMW, fdDB));
// wait for a IO operation
if (select(maxfd + 1, &rfds, NULL, NULL, NULL) == -1) {
perror("select");
}
if (FD_ISSET(fdWS, &rfds)) {
res = read(fdWS, buf, sizeof(buf));
if (res > 0) printf("Received: %s from [web]\n", buf);
}
if (FD_ISSET(fdMW, &rfds)) {
res = read(fdMW, buf, sizeof(buf));
if (res > 0) printf("Received: %s from [middle-ware]\n", buf);
}
if (FD_ISSET(fdDB, &rfds)) {
res = read(fdDB, buf, sizeof(buf));
if (res > 0) printf("Received: %s from [database]\n", buf);
}
}
int main(void) {
pid_t pid;
int dbServerFd, mwServerFd, wsServerFd;
char *fifoDb = "/tmp/ex04_db";
char *fifoMw = "/tmp/ex04_mw";
char *fifoWs = "/tmp/ex04_ws";
// remove file
unlink(fifoDb);
unlink(fifoMw);
unlink(fifoWs);
// create FIFO's
mkfifo(fifoDb, 0666);
mkfifo(fifoMw, 0666);
mkfifo(fifoWs, 0666);
// init rand
srand((unsigned int)time(NULL));
// create fork
pid = fork();
if (pid == 0) {
/* Child process closes up input side of pipe */
int randNum, randServer;
int bufsz;
int counterDb = 1;
int counterMw = 1;
int counterWs = 1;
// create fifo file
dbServerFd = open(fifoDb, O_WRONLY);
mwServerFd = open(fifoMw, O_WRONLY);
wsServerFd = open(fifoWs, O_WRONLY);
while (1) {
char *buf;
// get random time between 0 and 2 seconds
randServer = rand() % 3;
// get random time between 2 and 4 seconds
randNum = rand() % 5 + 2;
// write message
switch (randServer) {
case 0:; // bypass: a label can only be part of a statement and
// a declaration is not a statement
bufsz = snprintf(NULL, 0, "message %d", counterDb);
buf = malloc(bufsz + 1);
snprintf(buf, bufsz + 1, "message %d", counterDb++);
write(dbServerFd, buf, sizeof(buf));
break;
case 1:; // bypass: a label can only be part of a statement and
// a declaration is not a statement
bufsz = snprintf(NULL, 0, "message %d", counterMw);
buf = malloc(bufsz + 1);
snprintf(buf, bufsz + 1, "message %d", counterMw++);
write(mwServerFd, buf, sizeof(buf));
break;
case 2:; // bypass: a label can only be part of a statement and
// a declaration is not a statement
bufsz = snprintf(NULL, 0, "message %d", counterWs);
buf = malloc(bufsz + 1);
snprintf(buf, bufsz + 1, "message %d", counterWs++);
write(wsServerFd, buf, sizeof(buf));
break;
}
sleep(randNum);
free(buf);
}
// close file
close(dbServerFd);
close(mwServerFd);
close(wsServerFd);
// delete file
unlink(fifoDb);
unlink(fifoMw);
unlink(fifoWs);
exit(EXIT_SUCCESS);
} else if (pid > 0) {
// read fifo
if ((dbServerFd = open(fifoDb, O_RDONLY)) < 0)
perror("ERRROR: Cant open fifo.\n");
if ((mwServerFd = open(fifoMw, O_RDONLY)) < 0)
perror("ERRROR: Cant open fifo.\n");
if ((wsServerFd = open(fifoWs, O_RDONLY)) < 0)
perror("ERRROR: Cant open fifo.\n");
while (1) {
printSelect(wsServerFd, mwServerFd, dbServerFd);
}
// wait for child terminate
if ((waitpid(pid, NULL, 0)) < 0) {
perror("waitpid");
exit(EXIT_FAILURE);
}
// close file
close(dbServerFd);
close(mwServerFd);
close(wsServerFd);
// delete file
unlink(fifoDb);
unlink(fifoMw);
unlink(fifoWs);
}
return EXIT_SUCCESS;
}