-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRanying.c
142 lines (102 loc) · 4.74 KB
/
Ranying.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <string.h>
#include <time.h>
#define BUFFER_SIZE 32
#define READ_END 0
#define WRITE_END 1
int main() {
char write_msg[BUFFER_SIZE] = "Hello";
char read_msg[BUFFER_SIZE];
char Message[BUFFER_SIZE]; // used for fifth child process
pid_t pid, waitPid; // child process id
int fd[5][2]; // file descriptors for the pipe
int i, status = 0, totalRandomTime = 0;
int startTime, finishTime, startTimeMsec, finishTimeMsec;
// get the time
struct timeval tv;
// Fork five children processes.
for (i = 0; i < 5; i++) {
// Create the pipe.
if (pipe(fd[i]) == -1) {
fprintf(stderr,"pipe() failed");
return 1;
}
// Fork a child process.
pid = fork();
if (pid > 0) {
// PARENT PROCESS.
// the parent need to wait for each child process to finish
while ((waitPid = wait(&status)) > 0);
// Close the unused WRITE end of the pipe.
close(fd[i][WRITE_END]);
// Read to the READ end of the pipe.
read(fd[i][READ_END], read_msg, strlen(read_msg)+1);
printf("Parent: Read '%s' from the pipe. PID: %d\n", read_msg, getppid());
// Close the READ end of the pipe.
close(fd[i][READ_END]);
}
else if (pid == 0) {
// CHILD PROCESS.
gettimeofday(&tv, NULL);
startTime = (int)tv.tv_sec;
startTimeMsec = (int)((tv.tv_usec) / 1000);
finishTime = (int)tv.tv_sec;
startTimeMsec = (int)((tv.tv_usec) / 1000);
if ( i < 4 ){
for (;;) {
// random 0 1 2s
srand(time(NULL));
int randomNum = rand() % 3;
// Close the unused READ end of the pipe.
close(fd[i][READ_END]);
// Write from the WRITE end of the pipe.
write(fd[i][WRITE_END], write_msg, BUFFER_SIZE);
// sleep 0, 1, 2s
sleep(randomNum);
gettimeofday(&tv, NULL);
finishTime = (int)tv.tv_sec;
finishTimeMsec = (int)((tv.tv_usec) / 1000);
// TODO: need to remove this output
printf("Time: %ld - Child %d: Wrote '%s' to the pipe. PID: %d\n", tv.tv_sec, i + 1, write_msg, getpid());
printf("0:%2d.%d: Child %d: message 1\n", finishTime - startTime - randomNum, startTimeMsec, i + 1);
// TODO: for test, need to remove
printf("ramdom = %d\n", randomNum);
if (finishTime - startTime > 30) break;
//printf("0:%2d.%d: Child %d: message 2\n", finishTime - startTime, finishTimeMsec, i + 1);
}
}
else {
for (;;) {
// Close the unused READ end of the pipe.
close(fd[i][READ_END]);
gettimeofday(&tv, NULL);
int startMessageTime = (int)tv.tv_sec;
int startMessageTimeMsec = (int)((tv.tv_usec) / 1000);
printf("For the 5th Child, Please input Message 1: \n");
scanf("%s", Message);
gettimeofday(&tv, NULL);
int MessageTime = (int)tv.tv_sec;
int MessageTimeMsec = (int)((tv.tv_usec) / 1000);
printf("0:%2d.%d: Child %d: %s\n", startMessageTime - startTime, startMessageTimeMsec, i + 1, Message);
printf("0:%2d.%d: Child %d: %s\n", MessageTime - startTime, MessageTimeMsec, i + 1, Message);
// Write from the WRITE end of the pipe.
write(fd[i][WRITE_END], Message, BUFFER_SIZE);
if (MessageTime - startTime > 30) break;
}
}
// Close the WRITE end of the pipe.
close(fd[i][WRITE_END]);
exit(status);
}
else {
fprintf(stderr, "fork() failed");
return 1;
}
}
return 0;
}