Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 3. Основной процесс порождает 5 дополнительных процессов из одного и того же EXE - файла,
- // передавая каждому процессу его номер как параметр командной строки.
- // Каждый из этих процессов открывает отдельное окно, выдает в него свой номер,
- // а по истечении случайного интервала времени от 5 до 60 с подает сигнал,
- // по которому один из порожденных процессов(неважно, какой именно) должен завершиться после нажатия <Enter>.
- // Основной процесс завершается, когда завершены все 5 порожденных.
- #include "stdafx.h"
- #include "windows.h"
- #include <stdlib.h>
- #include <locale.h>
- #include <iostream>
- #include <ctime>
- #include <string>
- using namespace std;
- int randomIndex(int* array) {
- int i;
- do {
- i = rand() % 5;
- } while (array[i] == NULL);
- return i;
- }
- int _tmain(int argc, TCHAR *argv[]) {
- srand(time(0));
- int processesQty = 0;
- PROCESS_INFORMATION processesInfo[5];
- HANDLE hKillEvent;
- hKillEvent = CreateEvent(NULL, FALSE, FALSE, _T("LAB4_KillEvent"));
- if (!hKillEvent) {
- cout << "Kill Me creation opening failed" << endl;
- return 1;
- }
- for (int i = 1; i <= 5; i++) {
- STARTUPINFO firstWindowInfo = { sizeof(firstWindowInfo) };
- char index[2], name[10] = "Process ";
- index[0] = i + '0';
- index[1] = ' ';
- name[8] = index[0];
- wchar_t wIndex[2], wName[10];
- mbstowcs(wIndex, index, 2);
- mbstowcs(wName, name, 10);
- firstWindowInfo.lpTitle = wName;
- BOOL isProcessCreated = CreateProcess(_T("C:\\D++\\OS_lab42\\Debug\\OS_lab42.exe"), wIndex, NULL,
- NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &firstWindowInfo, &processesInfo[i - 1]);
- if (!isProcessCreated) {
- cout << "Process " << i << " creation failed. Error " << GetLastError() << endl;
- return i;
- }
- processesQty++;
- }
- int processes[5] = { 1, 2, 3, 4, 5 };
- while (processesQty) {
- int index = randomIndex(processes);
- processes[index] = NULL;
- processesQty--;
- WaitForSingleObject(hKillEvent, INFINITE);
- ResetEvent(hKillEvent);
- while (true) {
- BringWindowToTop(GetConsoleWindow());
- cout << "Please press Enter to close one of the processes." << endl;
- if (getchar() == '\n') {
- TerminateProcess(processesInfo[index].hProcess, 0);
- break;
- }
- }
- }
- CloseHandle(hKillEvent);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment