Click here to get back home

Running in the background (via &)

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Running in the background (via &) Mark Seger 07-28-2008
Posted by Mark Seger on July 28, 2008, 7:26 am
Please log in for more thread options
I have the following simple script.

#!/usr/bin/perl -w
print `resize`;

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. Is there a way to tell if
you're running in the background - it may be a UNIX feature that you
cannot. Otherwise, is there some way to diddle the environment (perhaps
redirecting file descriptors) so it won't hang?

-mark

Posted by Josef Moellers on July 28, 2008, 7:50 am
Please log in for more thread options
Mark Seger wrote:
> I have the following simple script.
>
> #!/usr/bin/perl -w
> print `resize`;
>
> 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. Is there a way to tell if
> you're running in the background - it may be a UNIX feature that you
> cannot.

In a sense: you cannot as in *ix there is actually no conceptual
difference between a "foreground" and a "background" process.

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 (e.g. thos started with an ampersand). So it's the parent who
defines whether a process is a background process or a foreground
process and then, even that may change, e.g. if you start a process, do
quite a lot of different stuff (the new process could then be viewed as
running in the background), then wait for the process (it might then be
seen as a foreground process) until you are taken out of the wait() by a
signal, and continue (which would turn your process into a background
process again).
Also with some shells you can actively change processes between fore-
and background.

> Otherwise, is there some way to diddle the environment (perhaps
> redirecting file descriptors) so it won't hang?

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 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.
This will fail if you have a foreground process that has stdin
redirected from a file or from a pipe, though.

HTH,

Josef
--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
        If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html

Posted by Mark Seger on July 28, 2008, 7:55 am
Please log in for more thread options
\
> 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 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.
that's what I thought and so did a -t STDIN, but it shows true whether
in the foreground or background.
-mark

Posted by Mark Seger on July 28, 2008, 9:45 am
Please log in for more thread options
I have a solution, based on an example in the perl cookbook by calling
iloctl directly to get the terminal height and it seems to work in both
foreground and background. Can I count on the ioctls.ph file being
present in asm? Is there somewhere else I should look for it?

#!/usr/bin/perl -w
require "asm/ioctls.ph";
my $winsize="" x 8;
if (ioctl(STDOUT, TIOCGWINSZ(), $winsize)) {
($rows)=(unpack('S4', $winsize))[0];
}
else {
print "ERROR\n";
}
print "rows: $rows\n";

-mark

Posted by Peter J. Holzer on July 28, 2008, 10:42 am
Please log in for more thread options
> \
>> 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

% perl -MPOSIX -le 'print getpgrp() == tcgetpgrp(STDIN) ? "foreground" :
"background"' & sleep 1
[1] 23462
background
[1] + done perl -MPOSIX -le

Looks good.

        hp

Similar ThreadsPosted
CGI: Running a forked background process July 17, 2006, 2:00 am
Script does not want to run in background ?!? August 23, 2007, 4:28 pm
FAQ 8.12: How do I start a process in the background? November 8, 2004, 6:03 pm
FAQ 8.12 How do I start a process in the background? March 13, 2005, 6:03 am
FAQ 8.12 How do I start a process in the background? June 11, 2005, 5:03 pm
FAQ 8.12 How do I start a process in the background? October 10, 2005, 4:03 pm
FAQ 8.12 How do I start a process in the background? November 5, 2005, 5:03 am
FAQ 8.12 How do I start a process in the background? November 29, 2005, 11:03 am
FAQ 8.12 How do I start a process in the background? January 18, 2006, 5:03 am
FAQ 8.12 How do I start a process in the background? September 17, 2006, 3:03 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap