日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

任务调度与上下文切换时间测试

發(fā)布時間:2024/4/18 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 任务调度与上下文切换时间测试 小編覺得挺不錯的,現(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
  • //方法一:使用gettimeofday()獲取當(dāng)前時間??
  • ??
  • #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];??//用于進(jìn)程間數(shù)據(jù)通信??
  • ????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)?{?????????//子進(jìn)程??
  • ????????sched_setscheduler(getpid(),?SCHED_FIFO,?&param);??
  • ????????gettimeofday(&tv_start,?NULL);??
  • ????????write(tv[1],?&tv_start,?sizeof(tv_start));//保存數(shù)據(jù),以便傳遞到父進(jìn)程中進(jìn)行計(jì)算??
  • ????????//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?{??????//父進(jìn)程??
  • ????????sched_setscheduler(getpid(),?SCHED_FIFO,?&param);??
  • ????????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;??
  • } ?
  • 總結(jié)

    以上是生活随笔為你收集整理的任务调度与上下文切换时间测试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。