任务调度与上下文切换时间测试
2015-05-13 20:40
141 查看
创建两个进程(实时进程)并在它们之间传送一个令牌,如此往返传送一定的次数。其中一个进程在读取令牌时就会引起阻塞。另一个进程发送令牌后等待其返回时也处于阻塞状态。发送令牌带来的开销与上下文切换带来的开销相比,可以忽略不计(经测试,一次管道传递约用时3ns左右)。 (利用管道传递令牌)
方法一:使用gettimeofday()
[cpp] view
plaincopy
//方法一:使用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;
}
方法一:使用gettimeofday()
[cpp] view
plaincopy
//方法一:使用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;
}
相关文章推荐
- 任务调度与上下文切换时间测试
- Spring中的时间调度,定时任务
- Linux任务调度crontab时间规则介绍
- Spring调度任务cronjob 精确指定任务的运行时间
- Quartz任务调度 时间设置
- 任务调度 -----> quartz 不同时间间隔调度任务
- SpringQtz 时间任务调度
- Spring 定时任务时间调度相关设置
- linux任务调度crontab时间规则
- 一个测试任务并发执行时间的方法
- Spring中的时间调度,定时任务
- 多线程应用:用TimerTask于Timer任意时间调度任务
- quartz2.2.1-测试02-通过servlet动态修改定时任务运行时间
- Quartz定时任务调度cron 表达式时间格式
- slurm任务调度系统部署和测试(一)
- Quartz.net 动态调整任务的调度时间
- 任务调度Quartz框架中 时间跨度表达式cron-expression
- uCOS中任务调度时的上下文切换
- 单位时间任务调度
- Spring学习:Spring调度任务--精确指定任务的运行时间