How to detect that foreground process is waiting for input?

Hi,

[LEFT]
I have to create a script (ksh or perl) that starts certain number of parallel jobs (another scripts), each of them runs as a foreground process in a separate session. Plus I start monitoring job that has to determine if any of those scripts is expecting input from operator, and switch to the corresponding session if necessary. My problem is that I have not found a good way to determine that process is expecting input. For the background process it's pretty easy: process state is "stopped" and this can be easily checked with 'ps' command. In case of foreground process this does not work. So far I tried to attach to the process with dbx or truss to see if it's hanging on 'read', but this approach seems too heavyweight. Could you suggest some better solution? Perl, shell, C, Java, etc. ... is ok as long as it's standard and does not require extra 3rd party or OS-specific stuff to install.
Thank you.[/LEFT]

Log each session to a flat file and check the end of the log file for a prompt?

I do not control what happens in those sessions. Sorry I didn't mention that.

If they can read from stdin without entering stopped (aka T) state, they're not actually background processes -- they are foreground ones, each in full control of their respective terminals. Proper background process will still be able to print but, when they try and read, will be suspended.

If you set -m in a script, you will be able to put a backgrounded process back into the foreground with the fg command, but how to do vice versa noninteractively, I'm not sure... C would be able to do it.

Not necessarily. To successfully write to the terminal from a background process, the process group must not be orphaned and either the terminal's tostop setting must be disabled or the process must ignore SIGTTOU.

Regards,
Alister