 |
|
 |
|
05-21-07 12:22 PM
Hi,
I have the following program to demostrate the behaviour of orphaned
process group. However, when the parent completed, it is unknown the
child's process group (orphaned) will change from foreground to
background. In my box, I have got the following output,
$ ./a.out
parent: pid = 4050, ppid = 3865, pgrp = 4050, tpgrp = 4050
child: pid = 4051, ppid = 4050, pgrp = 4050, tpgrp = 4050
(after 5 seconds)
$ SIGHUP received, pid = 4051
child: pid = 4051, ppid = 1, pgrp = 4050, tpgrp = 4050
So, it looks like the child's process group remains as foreground.
If I changed one line in the code,
- kill (getpid (), SIGTSTP);
+ sleep (10);
it is guaranteed that the child's process group will be changed to
backgroup, and attempt to read from STDIN of a process in a orphaned
background process group will get an error with errno 5 (EIO).
$ ./a.out
parent: pid = 4050, ppid = 3865, pgrp = 4050, tpgrp = 4050
child: pid = 4051, ppid = 4050, pgrp = 4050, tpgrp = 4050
(after 5 seconds)
$ SIGHUP received, pid = 4051
(after 10 seconds)
child: pid = 4051, ppid = 1, pgrp = 4050, tpgrp = 3865
read error from controlling TTY, errno = 5
Which is the correct behaviour, and was it mentioned in somewhere?
======= Program Code ========
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
static void
sig_hup (int signo)
{
printf ("SIGHUP received, pid = %d\n", getpid ());
}
static void
pr_ids (char *name)
{
printf ("%s: pid = %d, ppid = %d, pgrp = %d, tpgrp = %d\n",
name, getpid (), getppid (), getpgrp (),
tcgetpgrp (0));
fflush (stdout);
}
int
main (void)
{
char c;
pid_t pid;
pr_ids ("parent");
if ((pid = fork ()) < 0)
printf ("fork error");
/* Parent */
else if (pid > 0)
{
sleep (5);
exit (0);
}
/* Child */
else
{
pr_ids ("child");
signal (SIGHUP, sig_hup);
kill (getpid (), SIGTSTP);
pr_ids ("child");
if (read (0, &c, 1) != 1)
printf ("read error from controlling TTY, errno = %d\n",
errno);
exit (0);
}
}
Cai Qian
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Orphaned Process Groups |
 |
 |
|
|
05-21-07 06:19 PM
One correction,
On May 21, 10:36 am, Cai Qian <caiq...@gmail.com> wrote:
> Hi,
>
> I have the following program to demostrate the behaviour of orphaned
> process group. However, when the parent completed, it is unknown the
> child's process group (orphaned) will change from foreground to
> background. In my box, I have got the following output,
>
> $ ./a.out
> parent: pid = 4050, ppid = 3865, pgrp = 4050, tpgrp = 4050
> child: pid = 4051, ppid = 4050, pgrp = 4050, tpgrp = 4050
> (after 5 seconds)
> $ SIGHUP received, pid = 4051
> child: pid = 4051, ppid = 1, pgrp = 4050, tpgrp = 4050
>
> So, it looks like the child's process group remains as foreground.
>
> If I changed one line in the code,
>
> - kill (getpid (), SIGTSTP);
> + sleep (10);
>
> it is guaranteed that the child's process group will be changed to
> backgroup, and attempt to read from STDIN of a process in a orphaned
> background process group will get an error with errno 5 (EIO).
>
> $ ./a.out
> parent: pid = 4050, ppid = 3865, pgrp = 4050, tpgrp = 4050
> child: pid = 4051, ppid = 4050, pgrp = 4050, tpgrp = 4050
> (after 5 seconds)
> $ SIGHUP received, pid = 4051
This line should be removed. No SIGHUP received.
> (after 10 seconds)
> child: pid = 4051, ppid = 1, pgrp = 4050, tpgrp = 3865
> read error from controlling TTY, errno = 5
>
> Which is the correct behaviour, and was it mentioned in somewhere?
>
> ======= Program Code ========
>
> #include <errno.h>
> #include <stdio.h>
> #include <signal.h>
> #include <sys/types.h>
> #include <unistd.h>
>
> static void
> sig_hup (int signo)
> {
> printf ("SIGHUP received, pid = %d\n", getpid ());
>
> }
>
> static void
> pr_ids (char *name)
> {
> printf ("%s: pid = %d, ppid = %d, pgrp = %d, tpgrp = %d\n",
> name, getpid (), getppid (), getpgrp (),
> tcgetpgrp (0));
> fflush (stdout);
>
> }
>
> int
> main (void)
> {
> char c;
> pid_t pid;
>
> pr_ids ("parent");
> if ((pid = fork ()) < 0)
> printf ("fork error");
> /* Parent */
> else if (pid > 0)
> {
> sleep (5);
> exit (0);
> }
> /* Child */
> else
> {
> pr_ids ("child");
> signal (SIGHUP, sig_hup);
> kill (getpid (), SIGTSTP);
> pr_ids ("child");
> if (read (0, &c, 1) != 1)
> printf ("read error from controlling TTY, errno = %d\n",
> errno);
> exit (0);
> }
>
> }
>
> Cai Qian
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
|
Sponsored Links |
 |
 |
|
|
 |
All times are GMT. The time now is 05:53 AM. |
 |
|
|
 |
|
 |
|
|
 |
|
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
|
|
|
|
Medical and Health forum | Computer Games Reviews | Graphics design forum
|
 |
|
 |
|