Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: "echo | ps -j $(:) | cat | cat | cat" runs components in different process groups



On Mar 23,  4:16pm, Stephane Chazelas wrote:
}
} $ echo | ps -j $(:) | cat | cat | cat
}   PID  PGID   SID TTY          TIME CMD
}  4858  4858  4858 pts/4    00:00:02 zsh
} 23813 23813  4858 pts/4    00:00:00 zsh
} 23895 23895  4858 pts/4    00:00:00 ps
} 23896 23896  4858 pts/4    00:00:00 cat
} 23897 23897  4858 pts/4    00:00:00 cat
} 23898 23898  4858 pts/4    00:00:00 cat
} 
} See how they all run in different process groups (they should
} all be in the same one, same job).

Hmm:
% strace -ff -p 9850 -esetpgid
Process 9850 attached - interrupt to quit
Process 10029 attached
[pid 10029] setpgid(0, 10029)           = 0
Process 10030 attached
Process 10029 detached
[pid  9850] --- SIGCHLD (Child exited) @ 0 (0) ---
Process 10030 detached
--- SIGCHLD (Child exited) @ 0 (0) ---
Process 10031 attached
[pid 10031] setpgid(0, 10029)           = -1 EPERM (Operation not permitted)
[pid 10031] setpgid(0, 10031)           = 0
Process 10032 attached
[pid 10032] setpgid(0, 10029)           = -1 EPERM (Operation not permitted)
[pid 10032] setpgid(0, 10032)           = 0
Process 10031 detached
[pid  9850] --- SIGCHLD (Child exited) @ 0 (0) ---
Process 10032 detached
--- SIGCHLD (Child exited) @ 0 (0) ---

Here 10029 is "echo" (forked to the left, as zsh does) and 10030 is the
process substitution $(:).  10031 is "ps" and 10032 is "cat".

Zsh wants to use "echo" as the group leader because it was the first to
fork, but because $(:) delayed the execution of "ps" the "echo" process
has already exited and cannot be made group leader.  As the fallback the
other processes each get made into its own process group.

So it's a race condition, adding the process substitution just happens
to expose it.

I'm not sure whether it would work to postpone reaping the would-be
group leader when that first SIGCHILD is received; can a zombie lead
a group?

Otherwise we'd have to notice that the group leader is gone and change
to the second forked process, and so on, until we found a leader that
would stick for the remainder of the forks.



Messages sorted by: Reverse Date, Date, Thread, Author