/*************************************************************************************/ /* File Name : timer.cpp */ /* Author : Meng Yi Yee */ /* Class : CS 431 */ /* Date : Oct 20, 1999 */ /* */ /* This program shows an approach to developing kernal object programs. This program */ /* read 2 parameter from user. One is run-time in second(s) and the other one is */ /* number of program(s). This program uses a waitable timer to catch a signal when */ /* the timer is up. The function of the main program will be to open a few */ /* windows that simulate doing background work. When the timer is up, all the */ /* program(s) that are running will be terminated. */ /* */ /* The main program first reads the user command from DOS. The user will input */ /* 'timer ' to run this program. The run-time will */ /* first convert to nanosecond(s) and then convert to LARGE_INTEGER for defining the */ /* quit time for the waitable timer. After that, 'CreateProcess' is called to run */ /* the background program(s) which the number of program(s) to be opened will be the */ /* same as the user's input. Then, the program check for time-up. When the time is */ /* up, all the program(s) will be closed. */ /* */ /* NOTE: Please change the path to the location where the background.exe located. */ /*************************************************************************************/ #define _WIN32_WINNT #include "stdafx.h" #include #include #include #include int main(int argc, char* argv[]) { //Declare local variables HANDLE wTimer; LARGE_INTEGER quitTime; _int64 endTime; SYSTEMTIME time; //Output instruction when there is an error executing the program and exit //the program if (argc!=3) { printf("timer \n"); printf("run-time must be in second(s), must be less than 215\n"); printf("number of program(s) cannot exceed 100\n"); return 0; } //Convert user input in second(s) to nanosecond(s) and the input must be less //than 215 seconds //Put the run time, K in the endTime (the units will have to be 100ns) endTime=-(atoi(argv[1])*10000000); //Convert the 64-bits integer 'endTime' to LARGE_INTEGER 'quitTime' quitTime.LowPart = (DWORD)(endTime&0xFFFFFFFF); quitTime.HighPart = (LONG)(endTime>>32); //Call 'CreateWaitableTime' to create a waitable timer wTimer=CreateWaitableTimer (NULL, FALSE, NULL); //define quitTime SetWaitableTimer (wTimer, &quitTime, 0, NULL, NULL, FALSE); //Declare variables for 'CreateProcess' STARTUPINFO startInfo; PROCESS_INFORMATION processInfo; HANDLE MyProcess[100]; int NumberOfClient=atoi(argv[2]); char cmdLine[100]; char TempCmdLine[2]; //Loop until the specified number of client(s) for (int i=1; i<=NumberOfClient; i++) { //Get the path and the current client's number for the 'CreateProcess' //NOTE: Change here if the path is not correct strcpy (cmdLine, "BACKGROUND.EXE "); _itoa (i, TempCmdLine, 10); strcat (cmdLine, TempCmdLine); ZeroMemory(&startInfo, sizeof(startInfo)); startInfo.cb=sizeof(startInfo); //Execute the program by calling 'CreateProcess' if (!CreateProcess(NULL, cmdLine, NULL, NULL, TRUE, HIGH_PRIORITY_CLASS|CREATE_NEW_CONSOLE, NULL, NULL, &startInfo, &processInfo)) { //If error occur, output message and exit the process printf("%s\n", cmdLine); fprintf(stderr, "CreateProcess failed on error %d \n", GetLastError()); ExitProcess(1); } else { //Else execute the 'CreateProcess' and keep the current handle for //'TerminateProces' MyProcess[i]=processInfo.hProcess; } } //Wait until time-out WaitForSingleObject (wTimer, INFINITE); //Terminate all opened program(s) for (int x=1; x<=NumberOfClient; x++) TerminateProcess (MyProcess[x], NULL); return 0; }