From c71458d20f17ef747cb314f2526701454e35335a Mon Sep 17 00:00:00 2001 From: Sathya Narayanan Date: Sun, 31 Jan 2016 21:31:51 -0800 Subject: [PATCH] Context switch working --- osproject_cswitch.c | 68 ++++++++++++++++++++++------------------------------- 1 file changed, 28 insertions(+), 40 deletions(-) diff --git a/osproject_cswitch.c b/osproject_cswitch.c index 2bcdf62..33df7e3 100644 --- a/osproject_cswitch.c +++ b/osproject_cswitch.c @@ -19,62 +19,50 @@ * express or implied warranty. */ -#ifndef lint -static char rcsid[] = "$Header: /sprite/src/benchmarks/cswitch/RCS/cswitch.c,v 1.1 89/08/31 13:19:37 ouster Exp $ SPRITE (Berkeley)"; -#endif not lint - #include -#include - -main(argc,argv) - int argc; - char **argv; +int main(int argc, char **argv) { int count, i, toSlave[2], fromSlave[2]; int pid; char buffer[10]; - struct timeval begin ,end; - int micros; + unsigned int begin ,end; double timePer; - extern int errno; + unsigned int regr; + asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r" (regr)); + printf("regr: %x\n", regr); - if (argc != 2 ) { - fprintf(stderr, "Ping takes one argument: count.\n"); - return; - } - count = 0; - sscanf(argv[1], "%d", &count); + count = 10; pipe(toSlave); pipe(fromSlave); pid = fork(); - if (pid == 0) { - write(fromSlave[1], "a", 1); - while (1) { - read(toSlave[0], buffer, 1); - if (buffer[0] != 'a') { - exit(0); - } - write(fromSlave[1], "a", 1); - } - } - if (pid == -1) { - fprintf(stderr, "Couldn't fork slave process: error %d.\n", - errno); - return; + if (pid == 0) { + write(fromSlave[1], "a", 1); + while (1) { + read(toSlave[0], buffer, 1); + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (end)); + if (buffer[0] != 'a') { + printf("Exiting\n"); + return -1; + } + //write(fromSlave[1], "a", 1); + printf("end: %u\n", end); + } } + if (pid == -1) { + printf("Couldn't fork slave process: error %d.\n"); + return; + } read(fromSlave[0], buffer, 1); - gettimeofday(&begin, (struct timezone *) NULL); for (i = 0; i < count; i++) { - write(toSlave[1], "a", 1); - read(fromSlave[0], buffer, 1); + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (begin)); + write(toSlave[1], "a", 1); + //read(fromSlave[0], buffer, 1); + usleep(10000); + printf("Start: %u\n", begin); } - gettimeofday(&end, (struct timezone *) NULL); - micros = 1000000*(end.tv_sec - begin.tv_sec) - + end.tv_usec - begin.tv_usec; write(toSlave[1], "b", 1); - timePer = micros; - timePer /= count * 1000; + timePer = (end - begin)/count; printf("Elapsed time per ping (2 context switches): %.2f milliseconds.\n", timePer); } -- 1.9.1