|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
求助一个C语言小程序主要要求是measuring context switch cost, 程序可以在ubuntu18.04下运行, 感谢
你试一试,下面这个程序是不是你想要的。程序能用就用,不能用,我也没办法了(程序是朋友写的,而我的系统不支持 #include <sys/time.h> 、#include <sys/types.h> 两个头文件)
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/time.h>
- #include <time.h>
- #include <sched.h>
- #include <sys/types.h>
- #include <unistd.h> //pipe()
-
- int main()
- {
- int x, i, fd[2], p[2];
- char send = 's';
- char receive;
- pipe(fd);
- pipe(p);
- struct timeval tv;
- struct sched_param param;
- param.sched_priority = 0;
- while ((x = fork()) == -1);
- if (x==0)
- {
- sched_setscheduler(getpid(), SCHED_FIFO, ¶m);
- gettimeofday(&tv, NULL);
- printf("Before Context Switch Time %u us\n", tv.tv_usec);
- for (i = 0; i < 10000; i++)
- {
- read(fd[0], &receive, 1);
- write(p[1], &send, 1);
- }
- exit(0);
- }
- else
- {
- sched_setscheduler(getpid(), SCHED_FIFO, ¶m);
- for (i = 0; i < 10000; i++)
- {
- write(fd[1], &send, 1);
- read(p[0], &receive, 1);
- }
- gettimeofday(&tv, NULL);
- printf("After Context SWitch Time %u us\n", tv.tv_usec);
- }
- return 0;
- }
复制代码
|
|