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...
If you want spawned programs to behave similarly to how the shell works, call setpgrp()
after forking your child. This will cause the background program to run in its own process group, and therefore have a detached tty. When it tries to do I/O to the console, it will receive SIGTTIN or SIGTTOU signals. The default behaviour of SIGTTIN or SIGTTOU is to stop the process just like SIGSTOP.
As the parent, you can find out whether you have stopped child processes using waitpid()
and WUNTRACED
.
[Edited -- see other answers for the answer to the main question]
The +
sign simply refers to the current job. Each pipeline of commands (such as foo | bar | baz
) is a job, which can be referred to using a jobspec beginning with the %
character. %1
is job number 1, %+
is the current job, and %-
is the previous job.
For more information about jobs, see the Job Control section of the Bash manual.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With