Konark

Untitled

Apr 5th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. // _os_calulate_Pi.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4.  
  5. #include "stdafx.h"
  6. #include <iostream>
  7. #include <Windows.h>
  8. #include <string>
  9. #include <iomanip>
  10. #include <vector>
  11. #include <istream>
  12. #include <cmath>
  13.  
  14.  
  15. using namespace std;
  16.  
  17. int _tmain(int argc, _TCHAR* argv[])
  18. {
  19.     setlocale(LC_ALL, "Russian");
  20.     int procCount;
  21.     cout << "Количество одновременно работающих процессов: ";
  22.     cin >> procCount;
  23.     int n;
  24.     cout << "Общее количество процессов: ";
  25.     cin >> n;
  26.     if ((procCount < n) && (procCount > 0)){
  27.         int workingProc = 0;
  28.         int countProc = 0;
  29.         int endedProc = 0;
  30.         double calcPi = 0;
  31.         wstring pipeName = L"\\\\.\\pipe\\MyPipe";
  32.         STARTUPINFO si;
  33.         ZeroMemory(&si, sizeof(STARTUPINFO));
  34.         PROCESS_INFORMATION pi;
  35.         wstring pathToClient = L"C:\\Users\\Влад\\Desktop\\_os_calulate_Pi\\Release\\calc_pi.exe";
  36.         vector<HANDLE> pipeArray;
  37.         while(true){
  38.             if ((countProc < n) && (workingProc < procCount))
  39.             {
  40.                     workingProc++;
  41.                     countProc++;
  42.                     wstring bPipeName = pipeName + to_wstring(countProc);
  43.                     wstring  lpCommandLine = L" " + bPipeName + L" " + to_wstring(countProc);
  44.                     HANDLE bf = CreateNamedPipe((LPWSTR)bPipeName.c_str(), PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,PIPE_UNLIMITED_INSTANCES, BUFSIZ, BUFSIZ, 0, NULL);
  45.                     pipeArray.push_back (bf);
  46.                     CreateProcess(pathToClient.c_str(), (LPWSTR)lpCommandLine.c_str(), NULL, NULL, FALSE, NULL, NULL, NULL, &si, &pi); 
  47.             }
  48.             else{
  49.                 double buf_val = 0;
  50.                 DWORD buf;
  51.                 if (ReadFile(pipeArray.at(endedProc), &buf_val, sizeof(buf_val), &buf, NULL)){
  52.                     calcPi += buf_val;
  53.                     CloseHandle(pipeArray.at(endedProc));
  54.                     cout<<"Процесс "<<(endedProc+1)<<" закончил работу. Результат = "<<buf_val<<endl;
  55.                     endedProc++;
  56.                     workingProc--; 
  57.                     if (endedProc >= n)
  58.                         break;
  59.                 }
  60.             }
  61.         }
  62.         cout<<"Посчитанное число Pi = "<<fixed<<setprecision(9)<<calcPi<<endl;
  63.         pipeArray.clear();
  64.     }
  65.     system("pause");
  66.     return 0;
  67. }
Add Comment
Please, Sign In to add comment