Rather than forcing my program to the foreground, my program wants to know 
whether it was executed in the background with &, or whether it is in the 
foreground.

I am testing tcgetpgrp(fileno(stdin) against getpgid(getpid()).  If they are 
different, my program avoids calling tcgetattr() and tcsetattr(), thus avoiding 
an unnecessary SIGTTOU if terminal I/O is not needed.

Is there a better way to determine if a program is running in the background?


Also, it looks like SUSv3 posix_spawn() can set the process group:
If the POSIX_SPAWN_SETPGROUP flag is set in the spawn-flags attribute of the 
object referenced by attrp, and the spawn-pgroup attribute of the same object 
is non-zero, then the child's process group shall be as specified in the 
spawn-pgroup attribute of the object referenced by attrp.

As a special case, if the POSIX_SPAWN_SETPGROUP flag is set in the spawn-flags 
attribute of the object referenced by attrp, and the spawn-pgroup attribute of 
the same object is set to zero, then the child shall be in a new process group 
with a process group ID equal to its process ID.

If the POSIX_SPAWN_SETPGROUP flag is not set in the spawn-flags attribute of 
the object referenced by attrp, the new child process shall inherit the 
parent's process group.



Thanks,
Brian Ginn



-----Original Message-----
From: ast-developers-boun...@research.att.com 
[mailto:ast-developers-boun...@research.att.com] On Behalf Of David Korn
Sent: Tuesday, June 14, 2011 1:32 PM
To: ast-developers@research.att.com; Brian Ginn
Subject: Re: [ast-developers] ksh process group problem

cc: ast-developers@research.att.com
Subject: Re: [ast-developers] ksh process group problem
--------


ksh93 uses posix_spawn() rather than fork/exec to run a simple command for
performance reasons.  However, posix_spawnv() doesn't have a way to
set the terminal group before the exec().  The parent process sets the
terminal group which leads to the race.

There is a simple change you can make to a program to guarentee that
the terminal will get set at the beginning of the program.
You can add two lines.  One line is
        #include        <termios.h>

and for the other line  
                tcsetpgrp(2,getpgrp());
Alternatively, you can write a program that does these two lines and
then execs the original program.

> I am seeing an intermittent problem on Red Hat Enterprise Linux 5.4 and 5.6, 
> and
>  HP-UX 11.0 and 11.31.
> The processes launched from the AST ksh sometimes "own" the terminal, and 
> someti
> mes don't.
> 
> 
> Output from the test program below demonstrates the issue:
> 
> 
> [fbg@sturgeon ~]$ /opt/ast/bin/ksh
> 
> $ /tmp/testprocessgroup_linux64
> 
> PID: 8678  MY_pgrp=8678 STDIN_PGRP:8674 --DO NOT own terminal
> 
> $ /tmp/testprocessgroup_linux64
> 
> PID: 8679  MY_pgrp=8679 STDIN_PGRP:8679 --own terminal
> 
> $ /tmp/testprocessgroup_linux64
> 
> PID: 8680  MY_pgrp=8680 STDIN_PGRP:8680 --own terminal
> 
> $ /tmp/testprocessgroup_linux64
> 
> PID: 8681  MY_pgrp=8681 STDIN_PGRP:8681 --own terminal
> 
> $ /tmp/testprocessgroup_linux64
> 
> PID: 8682  MY_pgrp=8682 STDIN_PGRP:8674 --DO NOT own terminal
> 
> $ /tmp/testprocessgroup_linux64
> 
> PID: 8683  MY_pgrp=8683 STDIN_PGRP:8683 --own terminal
> 
> $ /tmp/testprocessgroup_linux64
> 
> PID: 8684  MY_pgrp=8684 STDIN_PGRP:8674 --DO NOT own terminal
> 
> $
> 
> 
> 
> 
> [fbg@sturgeon pgrp]$ cat testprocessgroup.c
> #include <stdio.h>
> #include <sys/types.h>
> #include <unistd.h>
> #include <sys/stat.h>
> #include <fcntl.h>
> 
> int main(int argc, char **argv)
> {
>     int devtty;
> 
> 
>     printf("PID:%5lu  MY_pgrp=%d STDIN_PGRP:%d --%s\n",
>                getpid(),
>                getpgid(getpid()),
>                tcgetpgrp(fileno(stdin)),
>                tcgetpgrp(fileno(stdin)) == getpgid(getpid()) ? "own terminal" 
> : 
> "DO NOT own terminal" );
> 
> 
>     return(0);
> }
> 
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email 
> ______________________________________________________________________
> (attachment  1   171/10586              text/html "1.att")
> 
> _______________________________________________
> ast-developers mailing list
> ast-developers@research.att.com
> https://mailman.research.att.com/mailman/listinfo/ast-developers
> 

David Korn
d...@research.att.com
_______________________________________________
ast-developers mailing list
ast-developers@research.att.com
https://mailman.research.att.com/mailman/listinfo/ast-developers

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________

_______________________________________________
ast-developers mailing list
ast-developers@research.att.com
https://mailman.research.att.com/mailman/listinfo/ast-developers

Reply via email to