dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Synchronization between process execution and the output of this process

  1. #1
    Join Date
    Mar 2019
    Posts
    11

    Synchronization between process execution and the output of this process

    Hi,

    I'm having trouble on synchronization between the CreateProcess output and reading of the redirected pipe.

    The problem is, I do not know after the WaitForSingleObject (in the while loop) whether the process
    has written all the data to the pipe or not. If the process has written everything I hang on the second readfile.
    I've tried it with INFINITE, but in such case with big output, the process stops (reason pipe buffer is full).
    For information: Processes are for examples "ipconfig /displaydns" (short running, big output) or "defrag d: /a" (long running, small output)

    Code:
    	try {
    		b_success = CreateProcess(nullptr,
    			const_cast<LPWSTR>(command_w.c_str()),		// command line 
    			nullptr,									// process security attributes 
    			nullptr,									// primary thread security attributes 
    			TRUE,										// handles are inherited 
    			CREATE_NO_WINDOW,							// creation flags 
    			nullptr,									// use parent's environment 
    			nullptr,									// use parent's current directory 
    			&si_start_info,								// STARTUPINFO pointer 
    			&pi_proc_info);								// receives PROCESS_INFORMATION 
    	}
    	catch (std::exception&)	{return false;}
    	if (!b_success) {return false;}
    	
    	auto wait_state = WaitForSingleObject(pi_proc_info.hProcess, 2000);
    
    	DWORD dw_read;
    	char ch_buf[BUFSIZE];
    	auto b_success = FALSE;
    	while (wait_state == WAIT_TIMEOUT) {
    		ZeroMemory(&ch_buf, sizeof(ch_buf));
    		try {
    			b_success = ReadFile(g_hChildStd_OUT_Rd, ch_buf, BUFSIZE - 1, &dw_read, nullptr);
    			if ((b_success) && (dw_read > 0)) {
    				result += std::string(ch_buf);
    			}
    		}
    		catch (std::exception&)
    		{
    			return false;
    		}
    		wait_state = WaitForSingleObject(pi_proc_info.hProcess, 2000);
    	}
    	if (wait_state == WAIT_OBJECT_0) {
    		ZeroMemory(&ch_buf, sizeof(ch_buf));
    		try {
    			b_success = ReadFile(g_hChildStd_OUT_Rd, ch_buf, BUFSIZE - 1, &dw_read, nullptr);
    			if ((b_success) && (dw_read > 0)) {
    				result += std::string(ch_buf);
    			}
    		}
    		catch (std::exception&)
    		{
    			return false;
    		}
    	}
    Is the chosen solution completely wrong or did I miss something?

    Thanks in advance.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,562

    Re: Synchronization between process execution and the output of this process

    You seem to waiting for exiting the pi_proc_info.hProcess. It is what this line
    Code:
    	auto wait_state = WaitForSingleObject(pi_proc_info.hProcess, 2000);
    is supposed to do.
    But is it what you want?
    Victor Nijegorodov

  3. #3
    Join Date
    Mar 2019
    Posts
    11

    Re: Synchronization between process execution and the output of this process

    Hi Victor,

    yes, i must wait on the end of the process, otherwise the output from the process is not complete. with

    Code:
    	auto wait_state = WaitForSingleObject(pi_proc_info.hProcess, INFINITE);
    normally i would wait on the process that they will finish. But if the process output (for the redirected pipe) is to big (full pipe) - the process can not write into the pipe, will not be finished and i will wait on the WaitForSingleObject for ever. On this point i found out i must read out the pipe (ReadFile) that the process can continue and finish. That is the reason why i also wait only 2000ms. But later on i come in trouble so see if is something in the pipe to read or not.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,562

    Re: Synchronization between process execution and the output of this process

    <thread was moved from C++ (Non Visual C++ Issues) forum>
    Victor Nijegorodov

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,925

    Re: Synchronization between process execution and the output of this process

    Using a specified timeout value is usually a sign of a flawed design.

    I do not know after the WaitForSingleObject (in the while loop) whether the process
    has written all the data to the pipe or not
    Then use an event as a signaller - set in the process and checked here.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.0)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)