zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: "echo | ps -j $(:) | cat | cat | cat" runs components in different process groups
Date: Fri, 23 Mar 2018 16:36:14 -0700	[thread overview]
Message-ID: <180323163614.ZM10192@torch.brasslantern.com> (raw)
In-Reply-To: <20180323161612.GB4857@chaz.gmail.com>

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.


  reply	other threads:[~2018-03-23 23:36 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-23 16:16 Stephane Chazelas
2018-03-23 23:36 ` Bart Schaefer [this message]
2018-03-24  5:19   ` Bart Schaefer
2018-03-24  8:05     ` Joey Pabalinas
2018-03-24 17:34       ` Stephane Chazelas
2018-03-24 22:23         ` Bart Schaefer
2018-03-25  7:00           ` Stephane Chazelas
2018-03-25  8:26             ` Stephane Chazelas
2018-03-25  7:48           ` Stephane Chazelas
2018-03-25 18:09             ` Stephane Chazelas
2018-03-27 10:17         ` Stephane Chazelas
2018-03-24 22:09       ` Bart Schaefer
2018-04-10 11:45         ` Peter Stephenson
2018-04-10 13:59           ` Peter Stephenson
2018-04-11 22:10             ` Bart Schaefer
2018-04-12 16:23               ` Peter Stephenson
2018-04-15 16:23                 ` Stephane Chazelas
2018-04-15 17:38                   ` Bart Schaefer
2018-04-15 18:58                     ` Stephane Chazelas
2018-04-17  5:39                       ` Bart Schaefer
2018-04-17  9:19                         ` Peter Stephenson
2018-04-17 16:09                           ` Bart Schaefer
2018-04-17 16:27                             ` Bart Schaefer
2018-04-17 16:35                             ` Peter Stephenson
2018-04-17 17:52                               ` Bart Schaefer
2018-04-19  9:40                                 ` Peter Stephenson
2018-04-20  9:28                                   ` Forking earlier for background code Peter Stephenson
2018-04-20 10:01                                     ` Peter Stephenson
2018-04-20 12:54                                       ` Vin Shelton
     [not found]                                         ` <CGME20180420132008eucas1p1c297624e870975cd892c74254970faab@eucas1p1.samsung.com>
2018-04-20 13:20                                           ` Peter Stephenson
2018-04-20 16:01                                             ` Vin Shelton
2018-04-23  8:29                                     ` Peter Stephenson
2018-05-01  9:23                                       ` Peter Stephenson
     [not found]                                   ` <F3A62E38-24E2-4A62-8E19-F54C9B81E9E5@kba.biglobe.ne.jp>
2018-04-23 13:52                                     ` "echo | ps -j $(:) | cat | cat | cat" runs components in different process groups Peter Stephenson
2018-04-23 14:03                                       ` Peter Stephenson
     [not found]                                         ` <CGME20180423140859eucas1p2591bf1422614209979d4890383268c37@eucas1p2.samsung.com>
2018-04-23 14:08                                           ` Peter Stephenson
2018-04-23 15:29                                         ` Jun T.
2018-04-18  6:29                           ` Stephane Chazelas
2018-04-18 16:33                             ` Bart Schaefer
2018-04-10  9:53   ` Peter Stephenson
2018-03-25  7:38 ` Stephane Chazelas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=180323163614.ZM10192@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).