-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpb.c
72 lines (68 loc) · 1.65 KB
/
pb.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
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
#include "kernel/fs.h"
// 510 instead of 512 for testing that non-PIPESIZE writes work
#define CHUNK_LEN 510
#define WRITE_AMT (10 * 1024 * 1024)
int
main(int argc, char *argv[])
{
char chunk[CHUNK_LEN];
int i;
for (i = 0; i < CHUNK_LEN; i++) {
chunk[i] = 'a' + (i % 26);
}
printf("Running benchmark...\n");
int fds[2];
if (pipe(fds) != 0) {
printf("failed pipe call\n");
exit(1);
}
int pid = fork();
if (pid != 0) {
// parent
close(fds[1]); // close writer
int read_buf[CHUNK_LEN];
int n_read = 0;
int t_before = uptime();
while (n_read < WRITE_AMT) {
int read_amt = read(fds[0], &read_buf, CHUNK_LEN);
if (read_amt < 0) {
printf("read failed\n");
exit(1);
}
n_read += read_amt;
if (memcmp(read_buf, chunk, CHUNK_LEN) != 0) {
printf("The byte stream read did not match written stream!\n");
exit(1);
}
}
int t_after = uptime();
printf("Elapsed ticks: %d\n", t_after - t_before);
int exit_status;
if (pid != wait(&exit_status)) {
printf("Unexpected child PID for wait\n");
exit(1);
}
if (exit_status != 0) {
printf("Child returned bad exit status: %d\n", exit_status);
exit(1);
}
close(fds[0]);
} else {
// child
close(fds[0]); // close reader
int n_written = 0;
while (n_written < WRITE_AMT) {
int write_amt = write(fds[1], &chunk, CHUNK_LEN);
if (write_amt < 0) {
printf("write failed\n");
exit(1);
}
n_written += write_amt;
}
close(fds[1]);
}
exit(0);
}