Created
October 28, 2014 22:41
-
-
Save anonymous/c49b00ce8135d1e290f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <conio.h> | |
#include <iostream> | |
#define MAX_CHILD_COUNT 10 | |
using namespace std; | |
int pids[MAX_CHILD_COUNT]; | |
volatile int childCount = 0; | |
int status; | |
int i; | |
char k[100]; | |
STARTUPINFO si[MAX_CHILD_COUNT]; | |
PROCESS_INFORMATION piApp[MAX_CHILD_COUNT]; | |
HANDLE hdProcess[MAX_CHILD_COUNT]; | |
HANDLE hOutEvent[MAX_CHILD_COUNT], hAddEvent[MAX_CHILD_COUNT]; | |
void ExitProgramm(void); | |
void InitialProcess(char *path); | |
int main(int argc, char *argv[]){ | |
if (argc >= 2){//child's process | |
int n = atoi(argv[0]); | |
cout << n << "sdfgsdfg"; | |
while (1){ | |
cout << argv[0] << "\n"; | |
//cout << "\n"<<piApp[childCount].hProcess<<"\n"; | |
Sleep(1000); | |
SetEvent(hOutEvent); | |
WaitForSingleObject(hAddEvent[n], INFINITE); | |
} | |
} | |
else{//parent's process | |
char *path = argv[0]; | |
InitialProcess(argv[0]); | |
char c; | |
char * commandLine = "C://Users/k/Documents/Visual Studio 2013/Projects/spovm_lab2_win/Debug"; | |
char *lpzAppName = "spovm_lab2_win.exe"; | |
hOutEvent[childCount] = CreateEvent(NULL, //атрибуты защиты | |
FALSE, //тип состояния | |
TRUE, //начальное состояние события | |
NULL); //имя события | |
if (hOutEvent[childCount] == NULL) | |
return GetLastError(); | |
hAddEvent[childCount] = CreateEvent(NULL, FALSE, FALSE, NULL); | |
if (hAddEvent[childCount] == NULL) | |
return GetLastError(); | |
while (1){ | |
//cout << "\nParent\n"; | |
Sleep(1000); | |
if (_kbhit()){ | |
switch (c = _getch()){ | |
case '+': { | |
cout << path; | |
if (childCount < 10){ | |
ZeroMemory(&si[childCount], sizeof(STARTUPINFO)); | |
si[childCount].cb = sizeof(STARTUPINFO); | |
//ZeroMemory(&piAp p[childCount], sizeof(PROCESS_INFORMATION)); | |
_itoa_s(childCount, k, 10); | |
strcat_s(k, " "); | |
strcat_s(k, path); | |
cout << k << "\n"; | |
if (!CreateProcessA( | |
path, //имя исполняемого модуля | |
k, //командная строка | |
NULL, //атрибуты защиты процесса | |
NULL, //атрибуты защиты потока | |
TRUE, //наследуемый ли дескриптор | |
NULL, //флаги создания процесса | |
NULL, //блок новой среды окружения | |
NULL, //текущий каталог | |
&si[childCount], //вид главного окна | |
&piApp[childCount] //информация о процессе | |
)){ | |
cout << "The new process is not created\n"; | |
} | |
else{ | |
WaitForSingleObject(hOutEvent[childCount], INFINITE); | |
hdProcess[childCount] = piApp[childCount].hProcess; | |
childCount++; | |
cout << "\nOk! Number of childs: " << childCount << "\n"; | |
//SetEvent(hAddEvent); | |
} | |
} | |
else{ | |
cout << "\n\nToo much child process! Delete it!\n\n"; | |
} | |
break; | |
} | |
case '-': {cout << c; break; } | |
case 'q': {ExitProgramm; return 0; } | |
//default: {}; | |
} | |
if (childCount > 0){ // Пока событие печати последнего процесса | |
// не будет сброшено | |
for (int j = 0; j < childCount; j++){ | |
SetEvent(hAddEvent[j]); | |
} | |
} | |
} | |
} | |
} | |
return 0; | |
} | |
void ExitProgramm(void){ | |
for (int i = 0; i < childCount; i++){ | |
CloseHandle(piApp[i].hThread); | |
CloseHandle(piApp[i].hProcess); | |
} | |
return; | |
} | |
void InitialProcess(char *path){ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment