-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe.c
65 lines (54 loc) · 1.39 KB
/
pipe.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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
/* WARNING. No error handling */
char *read_entire_file(char *name)
{
FILE *fh = fopen(name, "rb");
fseek(fh, 0, SEEK_END);
long fsize = ftell(fh);
fseek(fh, 0, SEEK_SET);
char *buffer = malloc(fsize + 1);
fread(buffer, fsize, 1, fh);
fclose(fh);
buffer[fsize] = 0;
return buffer;
}
int main(void)
{
int pfds[2];
char *license = read_entire_file("LICENSE");
long fsize = strlen(license); /* Yes. I know */
fprintf(stderr, "Using file %d bytes long\n", fsize);
char *buffer = malloc(fsize + 1);
long runs = 1000000; /* 1 million dollars */
clock_t start, end;
start = clock();
pipe(pfds);
if (!fork())
{
printf(" CHILD: writing to the pipe\n");
for (long i = 0; i < runs; i++)
write(pfds[1], license, fsize + 1);
printf(" CHILD: exiting\n");
exit(0);
}
else
{
printf("PARENT: reading from pipe\n");
for (long i = 0; i < runs; i++)
read(pfds[0], buffer, fsize + 1);
printf("PARENT: read\n");
wait(NULL);
}
end = clock();
fprintf(stderr, "Runs | Time taken\n");
fprintf(stderr, "%8d %f\n", runs, (end - start) / (double)CLOCKS_PER_SEC);
fprintf(stderr, "per run: %f\n", (end - start) / (double)CLOCKS_PER_SEC / runs);
return 0;
}