osproject_pipe.c 1.82 KB
#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;
}