linux与Windows进程控制
2019-11-22 10:52
阅读目录
-
系统程序测试
-
将timer加入环境变量
这里仅进行了临时变量修改。
Windows
在Windows下进行父子进程的创建和管理在api调用上相较Linux有一定难度,但实际上在使用管理上比Linux容易的多。
CreateProcess
#include <Windows.h> BOOL CreateProcessA( LPCSTR lpApplicationName, LPSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, LPCSTR lpCurrentDirectory, LPSTARTUPINFOA lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation );
源代码实现
timer程序
// 进程管理.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <iostream> #include <wchar.h> #include <Windows.h> #include <tchar.h> using namespace std; void printTime(SYSTEMTIME* start, SYSTEMTIME* end); void newProcess(TCHAR* cWinDir, double duration); int _tmain(int argc, TCHAR *argv[]) { TCHAR* cWinDir = new TCHAR[MAX_PATH]; memset(cWinDir, sizeof(TCHAR) * MAX_PATH, 0); printf("argc: %d\n", argc); int step = 1; double duration = 0; if (argc > 1) { if (argv[1][0] == TCHAR('-') && argv[1][1] == TCHAR('t') && argv[1][2] == TCHAR('\0')) { step = 3; duration = atof((char*)argv[2]); } } //printf("printf content start: %ls\n", argv[1]); int j = 0; for (int i = 0, h = 0; i < argc - step; i++) { wcscpy_s(cWinDir + j, MAX_PATH - j, argv[i + step]); for (h = 0; argv[i + step][h] != TCHAR('\0'); h++); j += h; cWinDir[j++] = ' '; //printf("%d : %d\n", i, j); //printf("printf content start: %ls\n", cWinDir); } cWinDir[j - 2] = TCHAR('\0'); //printf("printf content start: %ls\n", cWinDir); newProcess(cWinDir,duration); return 0; } void printTime(SYSTEMTIME* start, SYSTEMTIME* end) { int hours = end->wHour - start->wHour; int minutes = end->wMinute - start->wMinute; int seconds = end->wSecond - start->wSecond; int ms = end->wMilliseconds - start->wMilliseconds; if (ms < 0) { ms += 1000; seconds -= 1; } if (seconds < 0) { seconds += 60; minutes -= 1