Created
October 28, 2014 22:41
-
-
Save anonymous/c9cf2d9afb71b1b4ef06 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> | |
#include <string> | |
#define MAX_CHILD_COUNT 10 | |
using namespace std; | |
STARTUPINFO si[MAX_CHILD_COUNT]; | |
PROCESS_INFORMATION piApp[MAX_CHILD_COUNT]; | |
HANDLE hdProcess[MAX_CHILD_COUNT]; | |
HANDLE hOutEvent, hAddEvent[MAX_CHILD_COUNT]; | |
volatile int childCount = 0; | |
int main(int argc, char *argv[]){ | |
if (argc >= 2){ //child's process | |
int n = atoi(argv[0]); //номер созданного процесса | |
hAddEvent[n] = OpenEvent(SYNCHRONIZE, FALSE, "hAddEvent"); | |
hOutEvent = OpenEvent(SYNCHRONIZE, FALSE, "hOutEvent"); | |
while (1){ | |
Sleep(100); | |
cout << argv[1]; | |
SetEvent(hOutEvent); | |
WaitForSingleObject(hAddEvent[n], INFINITE); | |
} | |
} | |
else{ //parent's process | |
string s; | |
int i; | |
hOutEvent = CreateEvent(NULL, //атрибуты защиты | |
FALSE, //тип состояния | |
TRUE, //начальное состояние события | |
"hOutEvent"); //имя события | |
if (hOutEvent == NULL) | |
return GetLastError(); | |
while (1){ | |
if (_kbhit()){ | |
switch (_getch()){ | |
case '+': { | |
if (childCount < 10){ | |
ZeroMemory(&si[childCount], sizeof(STARTUPINFO)); | |
si[childCount].cb = sizeof(STARTUPINFO); | |
s = argv[0]; | |
s1 = to_string(static_cast<long long>(childCount)); | |
s += " " + s1 + s1 +s1 + s1 | |
LPSTR ss = const_cast<char *>(s.c_str()); | |
if (!CreateProcessA( | |
NULL, //, //имя исполняемого модуля | |
ss, //командная строка + номер процесса | |
NULL, //атрибуты защиты процесса | |
NULL, //атрибуты защиты потока | |
TRUE, //наследуемый ли дескриптор | |
NULL, //флаги создания процесса | |
NULL, //блок новой среды окружения | |
NULL, //текущий каталог | |
&si[childCount], //вид главного окна | |
&piApp[childCount] //информация о процессе | |
)){ | |
cout << "The new process is not created\n"; | |
} | |
else{ | |
WaitForSingleObject(hOutEvent, INFINITE); | |
hdProcess[childCount] = piApp[childCount].hProcess; | |
childCount++; | |
cout << "\nOk! Number of childs: " << childCount << "\n"; | |
} | |
} | |
else{ | |
cout << "\n\nToo much child process! Delete it!\n\n"; | |
} | |
break; | |
} | |
}//end of switch | |
if (childCount > 0){ | |
for (int j = 0; j < childCount; j++){ | |
SetEvent(hAddEvent[j]); | |
WaitForSingleObject(hOutEvent, INFINITE); | |
} | |
} | |
} | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment