In bash when I run a command like wc & or cat & that wants standard in right away, it returns immediately with
[1]+ Stopped cat
How is this accomplished? How do I stop a program that I started with exec, and how do I know to stop these programs in the first place? Is there some way to tell that these programs want stdin?
Thanks!
PS also, what is the + about? I’ve always wondered, but that’s really hard to google…
The
setpgid()manual page explains how this works:So what you want to do is this:
When you create a new pipeline, call
setpgid()to put all the members of the pipeline in a new process group (with the PID of the first process in the pipeline as the PGID).Use
tcsetpgrp()to manage which process group is in the foreground – if you put a pipeline in the background with&, you should make the shell’s own process group the foreground process group again.Call
waitpid()with theWNOHANGandWUNTRACEDflags to check on the status of child processes – this will inform you when they are stopped bySIGTSTP, which will allow you to print a message like bash does.