程序計時

 

一、實驗目的
設計並實現Unix的“time”命令。“mytime”命令通過命令行參數接受要運行的程序,創建一個獨立的進程來運行該程序,並記錄程序運行的時間。
二、實驗內容
在Windows實現: 
  • 使用CreateProcess()來創建進程
  • 使用WaitForSingleObject()在“mytime”命令和新創建的進程之間同步
  • 調用GetSystemTime()來獲取時間
在Linux下實現: 
  • 使用fork()/execv()來創建進程運行程序
  • 使用wait()等待新創建的進程結束
  • 調用gettimeofday()來獲取時間
 
三、實驗環境
windows 7和ubuntu 10.10
四、程序設計與實現
      主要API函數說明:
windows:
BOOL CreateProcess
  (
  LPCTSTR lpApplicationName,
  LPTSTR lpCommandLine,
  LPSECURITY_ATTRIBUTES lpProcessAttributes。
  LPSECURITY_ATTRIBUTES lpThreadAttributes,
  BOOL bInheritHandles,
  DWORD dwCreationFlags,
  LPVOID lpEnvironment,
  LPCTSTR lpCurrentDirectory,
  LPSTARTUPINFO lpStartupInfo,
  LPPROCESS_INFORMATION lpProcessInformation
);
DWORD WaitForSingleObject(
  HANDLE hHandle,
  DWORD dwMilliseconds
);
linux:
int execv(char *pathname, char *argv[]);
這次實驗不論是linux還是windows,主要思路只有一個利用fork或者createprocess建立一個子進程,在建立成功之後記錄運行時間,然後利用wait或者waitforsingleobject函數等待子進程的同步,之後再記錄運行時間,用兩次時間相減即可。

    這裏值得說明的是,要求中windows下的計時函數GetSystemTime()不太好用了,函數返回的是系統的時間,我們要用兩次時間相減,這裏還要考慮到有負數的情況,所以還要考慮借位,其實windows下面有一個好用的函數clock(),這個函數對於計時很在行。

 

window:

  1. #include <iostream>  
  2. #include<windows.h>  
  3. using namespace std;  
  4. int main(int argc,char **argv)  
  5. {  
  6.     int year,month,day,hour,minutes,seconds,milliseconds;  
  7.     SYSTEMTIME start,end;  
  8.     STARTUPINFO si; //一些必備參數設置  
  9.     memset(&si, 0, sizeof(STARTUPINFO));  
  10.     si.cb = sizeof(STARTUPINFO);  
  11.     si.dwFlags = STARTF_USESHOWWINDOW;  
  12.     si.wShowWindow = SW_SHOW;  
  13.     PROCESS_INFORMATION pi; //必備參數設置結束  
  14.     if(!CreateProcess(argv[1],NULL,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi))  
  15.     {  
  16.         cout<<"Create Fail!"<<endl;  
  17.         exit(1);  
  18.     }  
  19.     else 
  20.     {  
  21.         GetSystemTime(&start);  
  22.         cout<<"Success!"<<endl;  
  23.     }  
  24.     WaitForSingleObject(pi.hProcess,INFINITE);  
  25.     GetSystemTime(&end);  
  26.     milliseconds=end.wMilliseconds-start.wMilliseconds;  
  27.     seconds=end.wSecond-start.wSecond;  
  28.     minutes=end.wMinute-start.wMinute;  
  29.     hour=end.wHour-start.wHour;  
  30.     day=end.wDay-start.wDay;  
  31.     month=end.wMonth-start.wMonth;  
  32.     year=end.wYear-start.wYear;  
  33.     if ( milliseconds < 0)  
  34.     {  
  35.         seconds--;  
  36.         milliseconds+=1000;  
  37.     }  
  38.     if ( seconds < 0 )  
  39.     {  
  40.         minutes--;  
  41.         seconds+=60;  
  42.     }  
  43.     if ( hour < 0 )  
  44.     {  
  45.         day--;  
  46.         hour+=24;  
  47.     }  
  48.     if ( day < 0 )  
  49.     {  
  50.         month--;  
  51.         day+=30;  
  52.     }  
  53.     if ( month < 0 )  
  54.     {  
  55.         year--;  
  56.         month+=12;  
  57.     }  
  58.     if ( year > 0 )  
  59.     {  
  60.         printf("%d天",year);  
  61.     }  
  62.     if ( month > 0 )  
  63.     {  
  64.         printf("%d月",month);  
  65.     }  
  66.     if ( day > 0 )  
  67.     {  
  68.         printf("%d天",day);  
  69.     }  
  70.     if ( hour > 0 )  
  71.     {  
  72.         printf("%d小時",hour);  
  73.     }  
  74.     if ( minutes > 0 )  
  75.     {  
  76.         printf("%d分鐘",minutes);  
  77.     }  
  78.     if ( seconds > 0 )  
  79.     {  
  80.         printf("%d秒",seconds);  
  81.     }  
  82.     if ( milliseconds > 0 )  
  83.     {  
  84.         printf("%d微秒",milliseconds);  
  85.     }  
  86.     printf("\n");  
  87.     return 0;  

linux:

  1. #include <math.h>  
  2. #include <stdio.h>  
  3. #include <sys/time.h>  
  4.  
  5. int main(int argc,int **argv)  
  6. {  
  7.     int i;  
  8.     struct timeval start;  
  9.     struct timeval end;  
  10.     double timeuse;  
  11.     char *exec_argv[4];  
  12.     if (argc==1)  
  13.     {  
  14.         printf("Error!\n");  
  15.         exit(0);  
  16.     }  
  17.     if (fork()==0)  
  18.     {  
  19.         for (i=0; i<argc; i++)  
  20.         {  
  21.             exec_argv[i]=argv[i+1];  
  22.             printf("[%d]:%s\n", i,exec_argv[i]);  
  23.         }  
  24.  
  25.         printf("Child Create\n");  
  26.         execv(argv[1],exec_argv);  
  27.     }  
  28.     else 
  29.     {  
  30.         gettimeofday( &start, NULL );  
  31.         wait(NULL);  
  32.         gettimeofday( &end, NULL );  
  33.         timeuse = (1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec)/1000000;  
  34.         printf("time:%lfs\n",timeuse);  
  35.     }  
  36.     return 0;  
  37. }  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章