Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass STDIN to child process?

cmd_more

void WriteToPipe(void)

// Read from a file and write its contents to the pipe for the child's STDIN.
// Stop when there is no more data. 
{
    DWORD dwRead, dwWritten;
    CHAR chBuf[BUFSIZE];
    BOOL bSuccess = FALSE;
    char * name = malloc(100);


fgets(name, 100, stdin);


    bSuccess = WriteFile(g_hChildStd_IN_Wr, name, 10, &dwWritten, NULL);
    if (!bSuccess)
        ErrorExit("");

}

void ReadFromPipe(void)

// Read output from the child process's pipe for STDOUT
// and write to the parent process's pipe for STDOUT. 
// Stop when there is no more data. 
{
    DWORD dwRead, dwWritten;
    CHAR chBuf[BUFSIZE];
    BOOL bSuccess = FALSE;
    HANDLE hParentStdOut = GetStdHandle(STD_OUTPUT_HANDLE);


    bSuccess = ReadFile(g_hChildStd_OUT_Rd, chBuf, BUFSIZE, &dwRead, NULL);
    if (!bSuccess || dwRead == 0)
        return 100;

    bSuccess = WriteFile(hParentStdOut, chBuf,
        dwRead, &dwWritten, NULL);
    if (!bSuccess) 
        return 101;

}

The main (not full):

while (1)
{
    Sleep(500);

    ReadFromPipe(); 

    WriteToPipe();


}

I am trying to open the cmd as a child process and to pass the parent input to the child STDIN stream, and than to print the STDOUT of the child.

As you can see, it works at the first time but then I get this "more?" back from the child process (the cmd) and then it gets stuck waiting for output.

Why am I getting this "more?" back?

What is that "more?"

like image 888
whiteberryapps Avatar asked May 06 '14 08:05

whiteberryapps


People also ask

How do you spawn a child process?

You'll create processes with the child_process module by retrieving the results of a child process via a buffer or string with the exec() function, and then from a data stream with the spawn() function. You'll finish by using fork() to create a child process of another Node.

What is Stdin process?

stdin property is an inbuilt application programming interface of the process module which listens for the user input. The stdin property of the process object is a Readable Stream. It uses on() function to listen for the event.

How does Nodejs handle child process?

Usually, Node. js allows single-threaded, non-blocking performance but running a single thread in a CPU cannot handle increasing workload hence the child_process module can be used to spawn child processes. The child processes communicate with each other using a built-in messaging system.


1 Answers

The only way of the redirection of parent input which I found - is creation an additional thread. The common algorithm is:

  1. Create pipe - (hPipeRead, hPipeWrite)
  2. Create child process with standard input - hPipeRead
  3. Create new thread in parent process that is reading GetStdHandle(STD_INPUT_HANDLE) and is writing read buffer to hPipeWrite immediately. Thread is completed when stdInput is over.
  4. Parent process is waiting additional thread

This way is described in Microsoft support article.

like image 153
Evgeniy Mironov Avatar answered Oct 05 '22 06:10

Evgeniy Mironov