1

I have a code that in some point fork a child. The child only code is a scanf. The parent process needs to monitor the state of tis child process: 1) running 2) waiting for input in scanf 3) terminated

Check for termination or not is simples with waitpid and WNOHANG. The problem is that i dont know how to distinguish if the child process is waiting for input or not.

PS.: My case have a limitation that i cannot send information from child to its parent. Otherwise i could just use variables before and after the scanf.

PS.: I'm under linux

How can i check if a child process entered in a state of waiting for a scanf input ( or IO in general ) ?

Psyny
  • 119
  • 9
  • It would be better if you explained what you're trying to achieve. This is a strange thing to want to do, so there's probably a better solution for the actual problem you want to solve. – Crowman Apr 24 '16 at 01:27
  • I will edit. Thanks for the tip. – Psyny Apr 24 '16 at 01:31

2 Answers2

2

Are you using <Windows.h>? Is this what you're trying to find?

WaitForInputIdle

Waits until the specified process has finished processing its initial input and is waiting for user input with no input pending, or until the time-out interval has elapsed.

HANDLE hProc;
// get hProc here
WaitForInputIdle(hProc, INFINITE); // wait forever until input idle
// do something here

More info about the API can be found here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms687022(v=vs.85).aspx

Community
  • 1
  • 1
raymai97
  • 806
  • 7
  • 19
1

The concepts of processes and IO are generally disjunct. There are no signals that are exchanged between child and parent process, except for unix (kill) signals sent to the parent pid, that propagate to the children.

Waitpid just waits for termination of a child pid, returning its status code.

If you want to exchange data between parent and child you need to create a pipe (see man -s 2 pipe) between both processes, see the manual page for an example.

If you want to use scanf (input from stdin) in the child, you need to bind the pipefd[0] to the stdin file descriptor 0 (or STDIN_FILENO).

Now you can use select or poll in the parent process to check if the child is ready to read data sent by the parent to pipefd[1].

If you use printf or some other stdio.h method to write to the child (via STDOUT_FILENO for example), the IO on the parent might block anyway, even if select or poll told that the child is ready to receive the data, if the child reads too slow or stops reading too early and the output buffer is full (which has a default size of 4096 bytes, I think).

A unistd.h write call might return a value

nw = write(pipefd[1], buffer, nbytes);

nw < nbytes if the child did not read that many (nbytes) bytes of input.

So be carefull with the tripping hazards when doing asynchronous communication. Check the CSP (Communicating Sequential Processes) method as a different and more stable approach to communication, that uses synchronous communication, when you understood the asynchronous methods.

ikrabbe
  • 1,909
  • 12
  • 25
  • Thanks for the solution. I've run some testes with pipes and it seems to solve my problem. Also, my concept of IO and process was wrong, and this answer helped me to organize my thoughts. – Psyny Apr 24 '16 at 17:29