|
@@ -17,23 +17,26 @@ int main(int argc, char** argv) {
|
|
|
if (argc >= 2) {
|
|
|
times = atoi(argv[1]) / 2;
|
|
|
if (times > TEST_TIMES)
|
|
|
- return -1;
|
|
|
+ return 1;
|
|
|
}
|
|
|
|
|
|
- pipe(&pipes[0]);
|
|
|
- pipe(&pipes[2]);
|
|
|
- pipe(&pipes[4]);
|
|
|
+ if (pipe(&pipes[0]) < 0 || pipe(&pipes[2]) < 0 || pipe(&pipes[4]) < 0) {
|
|
|
+ perror("pipe error");
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
|
|
|
for (i = 0; i < times; i++) {
|
|
|
int pair_pipes[4];
|
|
|
- pipe(&pair_pipes[0]);
|
|
|
- pipe(&pair_pipes[2]);
|
|
|
+ if (pipe(&pair_pipes[0]) < 0 || pipe(&pair_pipes[2]) < 0) {
|
|
|
+ perror("pipe error");
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
|
|
|
pids[i][0] = fork();
|
|
|
|
|
|
if (pids[i][0] < 0) {
|
|
|
printf("fork failed\n");
|
|
|
- return -1;
|
|
|
+ return 1;
|
|
|
}
|
|
|
|
|
|
if (pids[i][0] == 0) {
|
|
@@ -48,11 +51,20 @@ int main(int argc, char** argv) {
|
|
|
|
|
|
char byte;
|
|
|
for (int i = 0; i < NTRIES; i++) {
|
|
|
- read(pair_pipes[0], &byte, 1);
|
|
|
- write(pair_pipes[3], &byte, 1);
|
|
|
+ if (read(pair_pipes[0], &byte, 1) != 1) {
|
|
|
+ perror("read error");
|
|
|
+ exit(1);
|
|
|
+ }
|
|
|
+ if (write(pair_pipes[3], &byte, 1) != 1) {
|
|
|
+ perror("write error");
|
|
|
+ exit(1);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- read(pipes[2], &byte, 1);
|
|
|
+ if (read(pipes[2], &byte, 1) != 1) {
|
|
|
+ perror("read error");
|
|
|
+ exit(1);
|
|
|
+ }
|
|
|
close(pipes[2]);
|
|
|
|
|
|
close(pair_pipes[0]);
|
|
@@ -65,7 +77,7 @@ int main(int argc, char** argv) {
|
|
|
|
|
|
if (pids[i][1] < 0) {
|
|
|
printf("fork failed\n");
|
|
|
- return -1;
|
|
|
+ return 1;
|
|
|
}
|
|
|
|
|
|
if (pids[i][1] == 0) {
|
|
@@ -77,14 +89,23 @@ int main(int argc, char** argv) {
|
|
|
close(pair_pipes[3]);
|
|
|
|
|
|
char byte;
|
|
|
- read(pipes[0], &byte, 1);
|
|
|
+ if (read(pipes[0], &byte, 1) != 1) {
|
|
|
+ perror("read error");
|
|
|
+ exit(1);
|
|
|
+ }
|
|
|
|
|
|
struct timeval timevals[2];
|
|
|
gettimeofday(&timevals[0], NULL);
|
|
|
|
|
|
for (int i = 0; i < NTRIES; i++) {
|
|
|
- write(pair_pipes[1], &byte, 1);
|
|
|
- read(pair_pipes[2], &byte, 1);
|
|
|
+ if (write(pair_pipes[1], &byte, 1) != 1) {
|
|
|
+ perror("write error");
|
|
|
+ exit(1);
|
|
|
+ }
|
|
|
+ if (read(pair_pipes[2], &byte, 1) != 1) {
|
|
|
+ perror("read error");
|
|
|
+ exit(1);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
gettimeofday(&timevals[1], NULL);
|
|
@@ -94,10 +115,17 @@ int main(int argc, char** argv) {
|
|
|
|
|
|
close(pipes[0]);
|
|
|
|
|
|
- write(pipes[5], timevals, sizeof(struct timeval) * 2);
|
|
|
+ if (write(pipes[5], timevals, sizeof(struct timeval) * 2)
|
|
|
+ != sizeof(struct timeval) * 2) {
|
|
|
+ perror("write error");
|
|
|
+ exit(1);
|
|
|
+ }
|
|
|
close(pipes[5]);
|
|
|
|
|
|
- read(pipes[2], &byte, 1);
|
|
|
+ if (read(pipes[2], &byte, 1) != 1) {
|
|
|
+ perror("read error");
|
|
|
+ exit(1);
|
|
|
+ }
|
|
|
close(pipes[2]);
|
|
|
|
|
|
exit(0);
|
|
@@ -110,14 +138,21 @@ int main(int argc, char** argv) {
|
|
|
|
|
|
sleep(1);
|
|
|
char bytes[times * 2];
|
|
|
- write(pipes[1], bytes, times);
|
|
|
+ if (write(pipes[1], bytes, times) != times) {
|
|
|
+ perror("write error");
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
close(pipes[1]);
|
|
|
|
|
|
unsigned long long start_time = 0;
|
|
|
unsigned long long end_time = 0;
|
|
|
struct timeval timevals[2];
|
|
|
for (int i = 0; i < times; i++) {
|
|
|
- read(pipes[4], timevals, sizeof(struct timeval) * 2);
|
|
|
+ if (read(pipes[4], timevals, sizeof(struct timeval) * 2) != sizeof(struct timeval) * 2) {
|
|
|
+ perror("read error");
|
|
|
+ exit(1);
|
|
|
+ }
|
|
|
+
|
|
|
unsigned long s = timevals[0].tv_sec * 1000000ULL + timevals[0].tv_usec;
|
|
|
unsigned long e = timevals[1].tv_sec * 1000000ULL + timevals[1].tv_usec;
|
|
|
if (!start_time || s < start_time)
|
|
@@ -127,7 +162,10 @@ int main(int argc, char** argv) {
|
|
|
}
|
|
|
close(pipes[4]);
|
|
|
|
|
|
- write(pipes[3], bytes, times * 2);
|
|
|
+ if (write(pipes[3], bytes, times * 2) != times * 2) {
|
|
|
+ perror ("write error");
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
close(pipes[3]);
|
|
|
|
|
|
for (i = 0; i < times; i++) {
|