Re: Running in the background (via &)



On Jul 28, 7:42 am, "Peter J. Holzer" <hjp-usen...@xxxxxx> wrote:
On 2008-07-28 11:55, Mark Seger <Mark.Se...@xxxxxx> wrote:

\
Would it help to have an indication whether you're in the foreground or
in the background?
Usually background processes aren't connected to a terminal, most
important, STDIN won't be connected to a terminal

This is wrong in general.

as then two processes (the current "foreground" process and the
"background" process) contend for the input from the keyboard,

That's why about 20 years ago, Unixes started to stop background
processes from reading from the terminal (see the SIGTTIN signal).

so you could check whether STDIN is a
terminal.

Won't work.

that's what I thought and so did a -t STDIN, but it shows true whether
in the foreground or background.

Right. STDIN doesn't change. You can flip a process between foreground
and background (in most shells with the conveniently named commands "fg"
and "bg"), but it would be rather messy if that changed an open file
descriptor. What changes is whether the process group is the foreground
process group of the controlling terminal. The place to look for
POSIX (=UNIX) specific functionality is the POSIX module:

| getpgrp This is identical to Perl’s builtin "getpgrp()" function for
| returning the process group identifier of the current process,
| see "getpgrp" in perlfunc.
[...]
| tcgetpgrp
| This is identical to the C function "tcgetpgrp()" for returning
| the process group identifier of the foreground process group of
| the controlling terminal.

If they are the same we should be in the foreground:

% perl -MPOSIX -le 'print getpgrp() == tcgetpgrp(STDIN) ? "foreground" : "background"'
foreground


And, POSIX misses if stdin/stdout are redirected:

perl -MPOSIX -le 'warn getpgrp()==tcgetpgrp(STDIN)
? foreground" : "background"' </dev/null >/dev/null
background at -e line 1.

--
Charles DeRykus




.



Relevant Pages

  • Re: Program is "stopped" when sent to background
    ... > This is caused by job control in the shell. ... > use nohup for that provided you redirect stdin as well as stdout, ... > are necessary (and overnet is broken in that respect). ... foreground on another tty? ...
    (comp.os.linux)
  • Re: Running in the background (via &)
    ... STDIN doesn't change. ... You can flip a process between foreground ... POSIX specific functionality is the POSIX module: ...
    (comp.lang.perl.misc)
  • Re: Running in the background (via &)
    ... If I run it in the foreground it works just fine, but if I run it in the background it hangs until I foreground it. ... The terms "foreground" and "background" are used to distinguish between processes that the shell waits for and those that the shell doesn't wait for. ... Usually background processes aren't connected to a terminal, most important, STDIN won't be connected to a terminal as then two processes contend for the input from the keyboard, so you could check whether STDIN is a terminal. ...
    (comp.lang.perl.misc)
  • Re: Running in the background (via &)
    ... Usually background processes aren't connected to a terminal, most important, STDIN won't be connected to a terminal as then two processes (the current "foreground" process and the "background" process) contend for the input from the keyboard, so you could check whether STDIN is a terminal. ...
    (comp.lang.perl.misc)