对于我的任务,我应该"让孩子生成a、b、c和amp;计时,然后让他们生成每个字母的倍数,以查看并发性的作用。使用嵌套循环可以达到,比如每个字母500个">
目前,我的letter_generator.exe
程序只是随机选择一封信并打印该信。我下面的程序(主要是我教授的程序,我只是添加了一些东西(创建了1500
子进程,这根本不是我认为作业希望我做的。所以,我的问题是如何让每个子进程打印一封特定的信?
#include <windows.h>
#include <iostream>
#include <ctime>
#include <chrono>
using namespace std;
int main(VOID){
STARTUPINFO si;
PROCESS_INFORMATION pi;
// allocate memory
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
volatile int i = 0;
auto start = chrono::steady_clock::now();
int const CHILD_PROCESS_LIMIT = 1500;
for (i = 0; i < CHILD_PROCESS_LIMIT; i++){
// create child process
if (!CreateProcess(NULL, // use command line
"C:\Users\Coughing\Desktop\letter_generator.exe", // app name
NULL, // don't inherit process handle
NULL, // don't inherit thread handle
FALSE, // disable handle inheritance
0, // no creation flags
NULL, // use parent's environment block
NULL, // use parent's existing directory
&si,
&pi)){
fprintf(stderr, "create process failed");
return -1;
}
}
// parent waits for child to complete
WaitForSingleObject(pi.hProcess, INFINITE);
auto end = chrono::steady_clock::now();
chrono::duration<double> elapsed_seconds = end - start;
cout << "Children Complete in " << elapsed_seconds.count() << " seconds!";
// close handles
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
下面是启动子进程并通过命令行向其传递参数的基本示例。
我会把你剩下的家庭作业留给你来解决。
另外请注意,我在这里使用了一些C++17
功能,因此,如果您无法编译,请将Visual Studio中的语言标准设置为C++17
,或者更改代码以针对您使用的标准版本进行编译。
父进程
#include <iostream>
#include <string>
#include <chrono>
#include <Windows.h>
int main( )
{
std::cout << "Opening processesn";
for( int i = 0; i < 10; i++ )
{
STARTUPINFO startup_info{ };
PROCESS_INFORMATION process_info{ };
// To pass arguments to the child process replace 'test'
// with whatever argument you want to pass.
std::wstring args{ L"Child.exe test" };
// create child process
if( !CreateProcessW(
LR"(C:PathChild.exe)", // use command line
args.data( ),
nullptr, // don't inherit process handle
nullptr, // don't inherit thread handle
false, // disable handle inheritance
0, // no creation flags
nullptr, // use parent's environment block
nullptr, // use parent's existing directory
&startup_info,
&process_info ) )
{
std::error_code ec{ static_cast<int>(
GetLastError( ) ), std::system_category( ) };
std::cerr << "Failed to start process: " << ec.message( ) << 'n';
return -1;
}
std::cout << "Waiting for process to completen";
auto start{ std::chrono::high_resolution_clock::now( ) };
if( auto code{ WaitForSingleObject(
process_info.hProcess, INFINITE ) }; code != WAIT_OBJECT_0 )
{
std::error_code ec{ static_cast<int>(
GetLastError( ) ), std::system_category( ) };
std::cerr << "Failed to wait for process: " << ec.message( ) << 'n';
}
auto elapsed{ std::chrono::high_resolution_clock::now( ) - start };
auto duration{ std::chrono::duration_cast<std::chrono::microseconds>( elapsed ) };
std::cout << "Time: " << duration.count( ) << " usn";
CloseHandle( process_info.hProcess );
CloseHandle( process_info.hThread );
}
}
子进程
#include <iostream>
int main( int argc, char* argv[ ] )
{
if( argc > 1 )
{
// The argument we passed will be located at index
// 1 in argv.
auto arg{ argv[ 1 ] };
// Prints 'test'.
std::cout << "Argument: " << arg << 'n';
}
}