Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // _os_calulate_Pi.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <Windows.h>
- #include <string>
- #include <iomanip>
- #include <vector>
- #include <istream>
- #include <cmath>
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- setlocale(LC_ALL, "Russian");
- int procCount;
- cout << "Количество одновременно работающих процессов: ";
- cin >> procCount;
- int n;
- cout << "Общее количество процессов: ";
- cin >> n;
- if ((procCount < n) && (procCount > 0)){
- int workingProc = 0;
- int countProc = 0;
- int endedProc = 0;
- double calcPi = 0;
- wstring pipeName = L"\\\\.\\pipe\\MyPipe";
- STARTUPINFO si;
- ZeroMemory(&si, sizeof(STARTUPINFO));
- PROCESS_INFORMATION pi;
- wstring pathToClient = L"C:\\Users\\Влад\\Desktop\\_os_calulate_Pi\\Release\\calc_pi.exe";
- vector<HANDLE> pipeArray;
- while(true){
- if ((countProc < n) && (workingProc < procCount))
- {
- workingProc++;
- countProc++;
- wstring bPipeName = pipeName + to_wstring(countProc);
- wstring lpCommandLine = L" " + bPipeName + L" " + to_wstring(countProc);
- 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);
- pipeArray.push_back (bf);
- CreateProcess(pathToClient.c_str(), (LPWSTR)lpCommandLine.c_str(), NULL, NULL, FALSE, NULL, NULL, NULL, &si, &pi);
- }
- else{
- double buf_val = 0;
- DWORD buf;
- if (ReadFile(pipeArray.at(endedProc), &buf_val, sizeof(buf_val), &buf, NULL)){
- calcPi += buf_val;
- CloseHandle(pipeArray.at(endedProc));
- cout<<"Процесс "<<(endedProc+1)<<" закончил работу. Результат = "<<buf_val<<endl;
- endedProc++;
- workingProc--;
- if (endedProc >= n)
- break;
- }
- }
- }
- cout<<"Посчитанное число Pi = "<<fixed<<setprecision(9)<<calcPi<<endl;
- pipeArray.clear();
- }
- system("pause");
- return 0;
- }
Add Comment
Please, Sign In to add comment