任务调度与上下文切换时间测试
2012-03-30 18:03
190 查看
创建两个进程(实时进程)并在它们之间传送一个令牌,如此往返传送一定的次数。其中一个进程在读取令牌时就会引起阻塞。另一个进程发送令牌后等待其返回时也处于阻塞状态。发送令牌带来的开销与上下文切换带来的开销相比,可以忽略不计(经测试,一次管道传递约用时3ns左右)。 (利用管道传递令牌)
方法一:使用gettimeofday()
方法一:使用gettimeofday()
//方法一:使用gettimeofday()获取当前时间 #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]; int tv[2]; //用于进程间数据通信 char send = 's'; char receive; pipe(fd); pipe(p); pipe(tv); struct timeval tv_start,tv_end; struct sched_param param; param.sched_priority = 0; while ((x = fork()) == -1); if (x==0) { //子进程 sched_setscheduler(getpid(), SCHED_FIFO, ¶m); gettimeofday(&tv_start, NULL); write(tv[1], &tv_start, sizeof(tv_start));//保存数据,以便传递到父进程中进行计算 //printf("Before Context Switch Time1.sec %u s\n", tv_start.tv_sec); //printf("Before Context Switch Time1.usec %u us\n", tv_start.tv_usec); for (i = 0; i < 10000; i++) { read(fd[0], &receive, 1); //printf("Child read!\n"); write(p[1], &send, 1); //printf("Child write!\n"); } exit(0); } else { //父进程 sched_setscheduler(getpid(), SCHED_FIFO, ¶m); for (i = 0; i < 10000; i++) { write(fd[1], &send, 1); //printf("Parent write!\n"); read(p[0], &receive, 1); //printf("Parent read!\n"); } gettimeofday(&tv_end, NULL); //printf("After Context SWitch Time1.sec %u s\n", tv_end.tv_sec); //printf("After Context SWitch Time1.usec %u us\n", tv_end.tv_usec); } read(tv[0], &tv_start, sizeof(tv_start)); //printf("Before Context Switch Time2.sec %u s\n", tv_start.tv_sec); //printf("Before Context Switch Time2.usec %u us\n", tv_start.tv_usec); //printf("Before Context Switch Time %u us\n", tv_start.tv_usec); //printf("After Context SWitch Time2.sec %u s\n", tv_end.tv_sec); //printf("After Context SWitch Time2.usec %u us\n", tv_end.tv_usec); printf("Task Switch Time: %f us\n", (1000000*(tv_end.tv_sec-tv_start.tv_sec) + tv_end.tv_usec - tv_start.tv_usec)/20000.0); return 0; }
相关文章推荐
- 任务调度与上下文切换时间测试
- Linux任务调度及crontab用法详解、进程的介绍和管理、时间、网络状态信息、路由信息等
- Spring Quartz任务调度示例(指定一个时间触发调度任务)
- Spring中的时间调度,定时任务
- 任务调度Quartz框架中 时间跨度表达式cron-expression
- Spring Quartz任务调度示例(指定一个时间触发调度任务)
- 实时操作系统的任务调度示例之时间片
- Mapreduce任务调度中的任务时间评估策略
- Spring调度任务cronjob 精确指定任务的运行时间
- Quartz.net 动态调整任务的调度时间
- 任务调度时间 (某公司实习生招聘笔试试题)
- Linux任务调度crontab时间规则介绍
- Spring中的时间调度,定时任务
- Spring 定时任务时间调度相关设置
- Quartz任务调度 时间设置
- 任务调度 -----> quartz 不同时间间隔调度任务
- Spring调度任务cronjob 精确指定任务的运行时间
- 多线程应用:用TimerTask于Timer任意时间调度任务
- SpringQtz 时间任务调度
- quartz2.2.1-测试02-通过servlet动态修改定时任务运行时间