tags:

views:

519

answers:

2

Writing my own toy shell, and have run into a bump trying to implement job control.

I am setting the process group of the child, both in the child and the parent with setpgid. My wait call is:

pid = waitpid(-pid, &status, 0)

However, waitpid returns -1 and perror says "No child process". However, it does seem to wait every time. Also, ps output looks right to mean when run from the shell. Since ps parent's process is kbsh like I would expect.

% ps -o pid,ppid,pgrp,session,tpgid,comm
Forking
In Parent: Setting process group to 20809 of process 20809 with setpgid
In Child Processes, pid of child process is 20809
in Child: Setting process group to 20809 of process 20809 with setpgid
Requesting that Process Group 20809 becomes the foreground process with tcsetpgrp
Waiting for job with process group 20809
  PID  PPID  PGRP  SESS TPGID COMMAND
12002 32573 12002 12002 20809 zsh
20808 12002 20808 12002 20809 kbsh
20809 20808 20809 12002 20809 ps
Wait Error: No child processes
Restoring Shell process group 20808 to forground

Anyone see what I am doing wrong? can post more code if need be...

A: 

You don't have to set the process group ID. The child inherits the parent's pid as group by default. When you wait, you need to wait for the parent's pid:

int main(int argc, char **argv)
{
    pid_t pid;
    int stat;

    if ((pid = fork()))
    {
        printf("PARENT: %d | CHILD: %d\n", getpid(), pid);
        waitpid(-getpid(), &stat, 0);
        printf("DONE: %m\n");
    }
    else
    {
        printf("CHILD: %d\n", getpid());
        sleep(3);
    }
    return 0;
}
eyalm
http://www.gnu.org/s/libc/manual/html_node/Implementing-a-Shell.html#Implementing-a-Shell -- Seems to say I do. "As each process is forked, it should put itself in the new process group by calling setpgid"
Kyle Brandt
I think because process groups are how foreground and background are controlled.
Kyle Brandt
A: 

I was ignoring sigchld, from the waitpid man page:

POSIX.1-2001 specifies that if the disposition of SIGCHLD is set to SIG_IGN or the SA_NOCLDWAIT flag is set for SIGCHLD (see sigaction(2)), then children that terminate do not become zombies and a call to wait() or waitpid() will block until all children have terminated, and then fail with errno set to ECHILD. (The original POSIX standard left the behaviour of setting SIGCHLD to SIG_IGN unspecified.) Linux 2.6 conforms to this specification. However, Linux 2.4 (and earlier) does not: if a wait() or waitpid() call is made while SIGCHLD is being ignored, the call behaves just as though SIGCHLD were not being ignored, that is, the call blocks until the next child terminates and then returns the process ID and status of that child.

Kyle Brandt