生活随笔
收集整理的這篇文章主要介紹了
任务调度与上下文切换时间测试
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
創(chuàng)建兩個進(jìn)程(實(shí)時進(jìn)程)并在它們之間傳送一個令牌,如此往返傳送一定的次數(shù)。其中一個進(jìn)程在讀取令牌時就會引起阻塞。另一個進(jìn)程發(fā)送令牌后等待其返回時也處于阻塞狀態(tài)。發(fā)送令牌帶來的開銷與上下文切換帶來的開銷相比,可以忽略不計(jì)(經(jīng)測試,一次管道傳遞約用時3ns左右)。 (利用管道傳遞令牌)?
方法一:使用gettimeofday()
[cpp]?view plaincopy
?? ?? #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));?? ?????????? ?????????? ????????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_end,?NULL);?? ?????????? ?????????? ?????????? ????}?? ????read(tv[0],?&tv_start,?sizeof(tv_start));????? ?????? ?????? ?????? ?????? ?????? ????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;?? } ?
總結(jié)
以上是生活随笔為你收集整理的任务调度与上下文切换时间测试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。