osproject_pipe.c
1.82 KB
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
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
/* Read characters from the pipe and echo them to stdout. */
unsigned int time1, time2;
unsigned int avg = 0;
int read_from_pipe (int file)
{
unsigned int c;
FILE *stream = fdopen (file, "r");
fread(&c, sizeof(c), 1, stream);
//while ((c = fgetc (stream)) != EOF)
// putchar (c);
fclose (stream);
return c;
}
/* Write some random text to the pipe. */
void write_to_pipe (int file)
{
unsigned int c;
asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (c));
FILE *stream;
stream = fdopen (file, "w");
fwrite(&c, sizeof(c), 1, stream);
// fprintf (stream, "hello, world!\n");
// fprintf (stream, "goodbye, world!\n");
fclose (stream);
}
int main (void)
{
pid_t pid;
int mypipe[2];
int i = 0;
unsigned int regr;
/* Create the pipe. */
if (pipe (mypipe)) {
printf ("Pipe failed.\n");
return EXIT_FAILURE;
}
asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r" (regr));
printf("regr: %x\n", regr);
asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (regr));
/* Create the child process. */
if ((pid = fork()) == -1)
exit(1);
if (pid) {
while(i < 100) {
/* This is the child process. Close other end first. */
printf("parent process\n");
close (mypipe[1]);
time1 = read_from_pipe (mypipe[0]);
asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (time2));
printf("time2: %u, time1: %u, avg: %u\n", time2, time1, time2 - time1);
avg += (time2 - time1);
i++;
}
//kill(pid, SIGKILL);
} else {
while( i < 100) {
/* This is the parent process. Close other end first. */
printf("child process\n");
close (mypipe[0]);
//asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (time1));
write_to_pipe (mypipe[1]);
usleep(1000);
i++;
}
}
printf("Avg switching time: %u\n", avg);
return 0;
}