How can I find out if a child is requesting stdin? How can I say this to stop it?

In bash, when I run a command like wc & or cat & that immediately wants to set a standard, it returns immediately with

[1] + Stopped cat

How is this achieved? How to stop the program that I started with exec, and how do I know to stop these programs in the first place? Is there any way to say that these programs want stdin?

Thanks!

PS also, what is + o? I always wondered, but it is very difficult for Google ...

+6
source share
4 answers

The setpgid() manual page explains how this works:

A session may have a management terminal. At any moment, one (and only one one) of the process groups in the session may be in the foreground a group of processes for the terminal; other process groups are in the background. If a signal is generated from the terminal (for example, entering an interrupt key to generate SIGINT ), this signal is sent to the foreground process group. (See termios(3) for a description of the symbols that generate the signals.) Only the foreground process group can read(2) from the terminal; if the background process group tries to read(2) from the terminal, then the group is sent to SIGTSTP , which pauses it. tcgetpgrp(3) and tcgetpgrp(3) Functions are used to get / set the foreground process of the control terminal group.

So what do you want to do:

  • When creating a new pipeline, call setpgid() to place all pipeline members in the new process group (with the PID of the first process in the pipeline as PGID).

  • Use tcsetpgrp() to control which process group is in the foreground — if you put the pipeline in the background with & , you must again make the process group for the foreground group.

  • Call waitpid() with the WNOHANG and WUNTRACED flags to check the status of the child processes - this will tell you when they will stop at SIGTSTP , which will allow you to print a message such as bash.

+1
source

If you want the generated programs to behave the way the shell works, call setpgrp() after forking your child. This will cause the background program to run in its own process group and therefore tty will be disabled. When it tries to do I / O to the console, it will receive SIGTTIN or SIGTTOU signals. The default behavior of SIGTTIN or SIGTTOU is to stop a process like SIGSTOP.

As a parent, you can find out if you stopped child processes with waitpid() and WUNTRACED .

+2
source

[Edited - see other answers to answer the main question]

The + sign simply refers to the current job. Each command pipeline (for example, foo | bar | baz ) is a job that can be referenced using the jobs specification starting with the % character. %1 - job number 1, %+ - current job, and %- - previous job.

For more information about jobs, see the Job Management Section of the Bash Guide.

+2
source

You can use system("command &") in a forked child process, and then manually output it if there are no time and priority restrictions.

0
source

Source: https://habr.com/ru/post/908584/


All Articles