Linkin_Park

OS lab4

Oct 10th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1.  
  2.  
  3. // 3.   Основной процесс порождает 5 дополнительных процессов из одного и того же EXE - файла,
  4. // передавая каждому процессу его номер как параметр командной строки.
  5. // Каждый из этих процессов открывает отдельное окно, выдает в него свой номер,
  6. // а по истечении случайного интервала времени от 5 до 60 с подает сигнал,
  7. // по которому один из порожденных процессов(неважно, какой именно) должен завершиться после нажатия <Enter>.
  8. // Основной процесс завершается, когда завершены все 5 порожденных.
  9.  
  10. #include "stdafx.h"
  11. #include "windows.h"
  12. #include <stdlib.h>
  13. #include <locale.h>
  14. #include <iostream>
  15. #include <ctime>
  16. #include <string>
  17.  
  18. using namespace std;
  19.  
  20. int randomIndex(int* array) {
  21.     int i;
  22.  
  23.     do {
  24.         i = rand() % 5;
  25.     } while (array[i] == NULL);
  26.    
  27.     return i;
  28. }
  29.  
  30. int _tmain(int argc, TCHAR *argv[]) {
  31.     srand(time(0));
  32.     int processesQty = 0;
  33.  
  34.     PROCESS_INFORMATION processesInfo[5];
  35.     HANDLE hKillEvent;
  36.  
  37.     hKillEvent = CreateEvent(NULL, FALSE, FALSE, _T("LAB4_KillEvent"));
  38.     if (!hKillEvent) {
  39.         cout << "Kill Me creation opening failed" << endl;
  40.         return 1;
  41.     }
  42.  
  43.     for (int i = 1; i <= 5; i++) {
  44.         STARTUPINFO firstWindowInfo = { sizeof(firstWindowInfo) };
  45.         char index[2], name[10] = "Process  ";
  46.         index[0] = i + '0';
  47.         index[1] = ' ';
  48.         name[8] = index[0];
  49.  
  50.         wchar_t wIndex[2], wName[10];
  51.         mbstowcs(wIndex, index, 2);
  52.         mbstowcs(wName, name, 10);
  53.         firstWindowInfo.lpTitle = wName;
  54.  
  55.         BOOL isProcessCreated = CreateProcess(_T("C:\\D++\\OS_lab42\\Debug\\OS_lab42.exe"), wIndex, NULL,
  56.             NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &firstWindowInfo, &processesInfo[i - 1]);
  57.  
  58.         if (!isProcessCreated) {
  59.             cout << "Process " << i << " creation failed. Error " << GetLastError() << endl;
  60.             return i;
  61.         }
  62.        
  63.         processesQty++;
  64.     }
  65.  
  66.     int processes[5] = { 1, 2, 3, 4, 5 };
  67.    
  68.     while (processesQty) {
  69.         int index = randomIndex(processes);
  70.         processes[index] = NULL;
  71.         processesQty--;
  72.  
  73.         WaitForSingleObject(hKillEvent, INFINITE);
  74.         ResetEvent(hKillEvent);
  75.         while (true) {
  76.             BringWindowToTop(GetConsoleWindow());
  77.            
  78.             cout << "Please press Enter to close one of the processes." << endl;
  79.  
  80.             if (getchar() == '\n') {
  81.                 TerminateProcess(processesInfo[index].hProcess, 0);
  82.                 break;
  83.             }
  84.         }
  85.     }
  86.  
  87.     CloseHandle(hKillEvent);
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment