CreateProcess PROCESS_ALL_ACCESS. , PROCESS_QUERY_INFORMATION, CREATE_PROCESS, PROCESS_TERMINATE, PROCESS_SET_INFORMATION, DUPLICATE_HANDLE CREATETHREAD. , , , , PROCESS_TERMINATE . 15.
UNIX wait waitpid, , waitpid ( ). , , , , , . , , , wait waitpid , , GetExitCodeProcess.
, Windows, UNIX. getenv ( ) , GetEnvironmentVariable, . putenv, setenv unsetenv , , , SetEnvironmentVariable.
:
Windows . grepMP , . UNIX- grep, , . " " , , .
:
grepMP F1 F2 FN
6.1 :
, F1 FN, , . : grep FK.
hStdOut STARTUPINFO , .
|
|
, WaitForMultipleObjects.
( ). , cat ( 2.3).
WaitForMultipleObjects , MAXIMUM_WAIT_OBJECTS (64), .
grep.
6.1 . 6.3.
. 6..
6.1. grepMP:
/* 6. grepMP. */
/* grep, . */
#include "EvryThng.h"
int _tmain(DWORD argc, LPTSTR argv[])
/* , , . , . */
{
HANDLE hTempFile;
SECURITY_ATTRIBUTES StdOutSA = /* . */
{sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
TCHAR CommandLine[MAX_PATH + 100];
STARTUPINFO StartUpSearch, Startup;
PROCESS_INFORMATION ProcessInfo;
DWORD iProc, ExCode;
HANDLE *hProc; /* . */
typedef struct {TCHAR TempFile[MAX_PATH];} PROCFILE;
PROCFILE *ProcFile; /* . */
GetStartupInfo(&StartUpSearch);
GetStartupInfo(&StartUp);
ProcFile = malloc((argc 2) * sizeof(PROCFILE));
hProc = malloc((argc 2) * sizeof(HANDLE));
/* "grep". */
for (iProc = 0; iProc < argc 2; iProc++) {
_stprintf(CommandLine, _T("%s%s %s"), _T("grep "), argv[1], argv[iProc + 2]);
GetTempFileName(_T("."), _T("gtm"), 0, ProcFile[iProc].TempFile); /* .*/
hTempFile = /* */
CreateFile(ProcFile[iProc].TempFile, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, &StdOutSA, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
StartUpSearch.dwFlags = STARTF_USESTDHANDLES;
StartUpSearch.hStdOutput = hTempFile;
StartUpSearch.hStdError = hTempFile;
StartUpSearch.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
/* . */
CreateProcess(NULL, CommandLine, NULL, NULL, TRUE, 0, NULL, NULL, &StartUpSearch, &ProcessInfo);
/* . */
CloseHandle(hTempFile);
CloseHandle(ProcessInfo.hThread);
hProc[iProc] = ProcessInfo.hProcess;
}
/* . */
for (iProc = 0; iProc < argc 2; iProc += MAXIMUM_WAIT_OBJECTS) WaitForMultipleObjects(/* */
min(MAXIMUM_WAIT_OBJECTS, argc 2 iProc), &hProc [iProc], TRUE, INFINITE);
/* cat */
|
|
for (iProc = 0; iProc < argc 2; iProc++) {
if (GetExitCodeProcess(hProc[iProc], &ExCode) && ExCode==0) {
/* . */
if (argc > 3) _tprintf(_T("%s:\n"), argv [iProc + 2]);
fflush(stdout); /* . */
_stprintf(CommandLine, _T("%s%s"), _("cat "), ProcFile[iProc].TempFile);
CreateProcess(NULL, CommandLine, NULL, NULL, TRUE, 0, NULL, NULL, &StartUp, &ProcessInfo);
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
}
CloseHandle(hProc [iProc]);
DeleteFile(ProcFile[iProc].TempFile);
}
free(ProcFile);
free(hProc);
return 0;
}