[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Does anyone understand terminal job control?



  Hello list,

  I've been banging my head on this one for a while.

  I have a need to write some code that can manage job control on a
terminal.  More specifically, I need to run a single process and stuff
it into the background at will, so that it gets suspended when it tries
to read from the terminal.  So, there's a "controller" process and
a "subprocess" process.

     controller  ----------> subprocess
                   manages

  The man-pages and glibc info documentation make this look simple:
disable TOSTOP if it's enabled via tc[gs]etattr(), then start the
subprocess in a new process group (using setpgid); once it's going,
you can put it in the "foreground" or "background" by calling
tcsetpgrp() to set the terminal's foreground process group.  If you
set it to the subprocess, the subprocess will be the foreground group;
otherwise, the controller will be the foreground group.  Whenever the
subprocess is in the background, it will be sent SIGTTIN if it tries to
read from the terminal.

  I have everything working -- except for the very last sentence of
that last paragraph.  I can see my processes being put into the right
process group, and I can see them going into the foreground and the
background, e.g.:

STAT CMD                           PID  PGID TPGID  PPID   SID
Ss+  ./src/aptitude              26989 26989 26989 26980 26989
S    ./src/aptitude              26991 26991 26989 26989 26989
S    /bin/sh -c /usr/sbin/dpkg-p 27027 26991 26989 26991 26989
S    /usr/bin/perl -w /usr/sbin/ 27028 26991 26989 27027 26989
Z    [dpkg-preconfigu] <defunct> 27034 26991 26989 27028 26989
S    /bin/sh -e /tmp/lynx-cur.co 27039 26991 26989 27028 26989
S    whiptail --backtitle Packag 27043 26991 26989 27028 26989

  and then if I move the background process into the foreground:

STAT CMD                           PID  PGID TPGID  PPID   SID
Ss   ./src/aptitude              26989 26989 26991 26980 26989
S+   ./src/aptitude              26991 26991 26991 26989 26989
S+   /bin/sh -c /usr/sbin/dpkg-p 27027 26991 26991 26991 26989
S+   /usr/bin/perl -w /usr/sbin/ 27028 26991 26991 27027 26989
Z+   [dpkg-preconfigu] <defunct> 27034 26991 26991 27028 26989
S+   /bin/sh -e /tmp/lynx-cur.co 27039 26991 26991 27028 26989
S+   whiptail --backtitle Packag 27043 26991 26991 27028 26989

  Note that the background process is not suspended.  If I manually
suspend the process group with "kill -TTIN -26991", it stops as
expected:

TAT CMD                           PID  PGID TPGID  PPID   SID
Ss   ./src/aptitude              26989 26989 26991 26980 26989
T+   ./src/aptitude              26991 26991 26991 26989 26989
T+   /bin/sh -c /usr/sbin/dpkg-p 27027 26991 26991 26991 26989
T+   /usr/bin/perl -w /usr/sbin/ 27028 26991 26991 27027 26989
Z+   [dpkg-preconfigu] <defunct> 27034 26991 26991 27028 26989
T+   /bin/sh -e /tmp/lynx-cur.co 27039 26991 26991 27028 26989
T+   whiptail --backtitle Packag 27043 26991 26991 27028 26989

  So the signal isn't being blocked or ignored.  I can also run
programs in the shell (e.g., "links &") and watch them
auto-suspend, but the same thing doesn't happen when I start them
directly under my controller process.  It's not even that they're
starting as foreground processes: I can start them without access to
the controlling terminal, and they never see a SIGTTIN.



  Does anyone have a clue what's going on?  Hopefully it's as simple
as a flag I have to set somewhere...

  Oh, and for extra fun, this is all happening inside a VTE terminal
widget.  That shouldn't make a difference (after all, it's what
gnome-terminal uses, and TTIN behaved as expected when I tested it
there), but who knows, it might be relevant.

     Thanks,
  Daniel


Reply to: