Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8824397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:37:29+00:00 2026-06-14T06:37:29+00:00

From a parent process, I am trying to control a child process through standard

  • 0

From a parent process, I am trying to control a child process through standard input/output.
I used the following MSDN example to create the child process and redirect its standard input & output : http://msdn.microsoft.com/en-us/library/windows/desktop/ms682499%28v=vs.100%29.aspx

On the parent side I send the same “command” twice using the following code:

const char cmd[] = "test\n";
DWORD written, read;
BOOL success;
success = WriteFile(g_hChildStd_IN_Wr, cmd, sizeof(cmd), &written, NULL);
success = WriteFile(g_hChildStd_IN_Wr, cmd, sizeof(cmd), &written, NULL);

On the child side, I read those commands through a fgets call.
The first command is read as expected. But when the second command is sent, the fgets call returns with an empty string and the next fgets call does not return. Here is the child process code:

char *retStrP;
char str[256];
size_t strLen;

retStrP = fgets(str, 256, stdin);
strLen = strlen(str);
if (retStrP != str)
{
    char errorStr[256];
    int a = feof(stdin);
    int b = ferror(stdin);
    sprintf(errorStr, "Fgets error, %d %d\n", a, b);
    OutputDebugString(errorStr);
}
else if (strLen == 0)
{
    char errorStr[256];
    int a = feof(stdin);
    int b = ferror(stdin);
    sprintf(errorStr, "Strlen is 0, %d %d\n", a, b);
    OutputDebugString(errorStr);
}

From the previous code, the second call to fgets behaves as follow: the returned pointer is equal to the str buffer but the strlen is 0.

When reading the MSDN fgets documentation ( http://msdn.microsoft.com/en-us/library/c37dh6kf%28v=vs.100%29.aspx ), I don’t see any way it could return the pointer to the buffer passed as the first argument with the first buffer’s byte being set to 0 (as far as the buffer size is greater than 1).
If an error occurs or the end of file is reached, the fgets should return NULL and either feof or ferror should return something not 0.

I suspect something is going wrong with the pipes handling but I can’t figure out what… From the parent side, I don’t do anything more with the stdin write pipe than sending the same buffer twice.
Any idea what could cause such a behaviour?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:37:31+00:00Added an answer on June 14, 2026 at 6:37 am

    I found out the problem!

    From the parent process, the WriteFile calls send the NULL terminating character to the pipe.

    So after the first WriteFile call, the fgets reads the string upto the newline character.
    Then on the second WriteFile call, the fgets reads the string starting from the previous NULL terminating character upto the second newline character. It results in a returned string with the first character being the NULL terminating character.
    This is why the fgets return without error but the string length is 0.

    The solution is then to not send the NULL terminating character by substracting 1 from the cmd buffer size.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to redirect the stdin from the parent to the child through the
I've been trying desperately to kill a child process from the parent. I've tried:
AFAICS, the child process inherits stdout/stdin from the parent process on fork(). This leaves
Want to run javascript function from parent window in child window Example I have
Hai am trying to load the variables from parent(flash) to child(flash).Its working fine,. parent
I'm working my way through displaying various data elements from parent & child data,
I'm trying to get output from a python multiprocessing Process displayed in a Tkinter
I am trying to get the text from a specific node's parent. For example:
I'm trying to create a background process for some heavy calculations from the main
I'm trying to use a socketpair to have a parent process provide input to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.