Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if a child is asking for stdin? How do I tell it to stop that?

Tags:

c

fork

exec

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...

like image 826
Ziggy Avatar asked Feb 16 '12 05:02

Ziggy


2 Answers

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.

like image 71
Greg Hewgill Avatar answered Oct 18 '22 02:10

Greg Hewgill


[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.

like image 2
Adam Rosenfield Avatar answered Oct 18 '22 02:10

Adam Rosenfield