一、实验目的
设计并实现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:
- #include <iostream>
- #include<windows.h>
- using namespace std;
- int main(int argc,char **argv)
- {
- int year,month,day,hour,minutes,seconds,milliseconds;
- SYSTEMTIME start,end;
- STARTUPINFO si; //一些必备参数设置
- memset(&si, 0, sizeof(STARTUPINFO));
- si.cb = sizeof(STARTUPINFO);
- si.dwFlags = STARTF_USESHOWWINDOW;
- si.wShowWindow = SW_SHOW;
- PROCESS_INFORMATION pi; //必备参数设置结束
- if(!CreateProcess(argv[1],NULL,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi))
- {
- cout<<"Create Fail!"<<endl;
- exit(1);
- }
- else
- {
- GetSystemTime(&start);
- cout<<"Success!"<<endl;
- }
- WaitForSingleObject(pi.hProcess,INFINITE);
- GetSystemTime(&end);
- milliseconds=end.wMilliseconds-start.wMilliseconds;
- seconds=end.wSecond-start.wSecond;
- minutes=end.wMinute-start.wMinute;
- hour=end.wHour-start.wHour;
- day=end.wDay-start.wDay;
- month=end.wMonth-start.wMonth;
- year=end.wYear-start.wYear;
- if ( milliseconds < 0)
- {
- seconds--;
- milliseconds+=1000;
- }
- if ( seconds < 0 )
- {
- minutes--;
- seconds+=60;
- }
- if ( hour < 0 )
- {
- day--;
- hour+=24;
- }
- if ( day < 0 )
- {
- month--;
- day+=30;
- }
- if ( month < 0 )
- {
- year--;
- month+=12;
- }
- if ( year > 0 )
- {
- printf("%d天",year);
- }
- if ( month > 0 )
- {
- printf("%d月",month);
- }
- if ( day > 0 )
- {
- printf("%d天",day);
- }
- if ( hour > 0 )
- {
- printf("%d小时",hour);
- }
- if ( minutes > 0 )
- {
- printf("%d分钟",minutes);
- }
- if ( seconds > 0 )
- {
- printf("%d秒",seconds);
- }
- if ( milliseconds > 0 )
- {
- printf("%d微秒",milliseconds);
- }
- printf("\n");
- return 0;
- }
linux:
- #include <math.h>
- #include <stdio.h>
- #include <sys/time.h>
- int main(int argc,int **argv)
- {
- int i;
- struct timeval start;
- struct timeval end;
- double timeuse;
- char *exec_argv[4];
- if (argc==1)
- {
- printf("Error!\n");
- exit(0);
- }
- if (fork()==0)
- {
- for (i=0; i<argc; i++)
- {
- exec_argv[i]=argv[i+1];
- printf("[%d]:%s\n", i,exec_argv[i]);
- }
- printf("Child Create\n");
- execv(argv[1],exec_argv);
- }
- else
- {
- gettimeofday( &start, NULL );
- wait(NULL);
- gettimeofday( &end, NULL );
- timeuse = (1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec)/1000000;
- printf("time:%lfs\n",timeuse);
- }
- return 0;
- }