-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadswitch.c
More file actions
34 lines (31 loc) · 848 Bytes
/
threadswitch.c
File metadata and controls
34 lines (31 loc) · 848 Bytes
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
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
int main()
{
int f1[2], f2[2];
int n = 1000000;
pipe(f1);
pipe(f2);
pid_t childPid = fork();
if (childPid == 0) {
int i;
for (i = 0; i < n; i++) {
read(f1[0], &i, sizeof(i));
write(f2[1], &i, sizeof(i));
}
} else {
struct timeval tval_before, tval_after, tval_result;
gettimeofday(&tval_before, NULL);
int i;
for (i = 0; i < n; i++) {
write(f1[1], &i, sizeof(i));
read(f2[0], &i, sizeof(i));
}
gettimeofday(&tval_after, NULL);
timersub(&tval_after, &tval_before, &tval_result);
printf("Time elapsed: %ld.%06ld\n", (long int)tval_result.tv_sec, (long int)tval_result.tv_usec);
}
return 0;
}