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

Re: What's a superjob?



On Wed, 26 Sep 2018 15:31:23 +0000
Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx> wrote:
> Could someone explain in a sentence what a 'superjob' is?

Not in a sentence, no.

> I'm guessing that it's the 'bodyguard shell' demonsrated in
> Joey's 43506 and explained in 43514.  Is that correct?

Yes, basically, if you mean what I think you mean.

% e() {
  vi
  echo "There might be some more code here, for the sake of example"
}
% e

Now we have just one process running: the main shell, and vi.  Suppose
you hit ^Z.  Then vi suspends.  The main shell is in the middle of
running e, but it's not supposed to finish doing that till vi exits.
But without special action we're now back at the top-level shell prompt
and it never will execute.

So when it detects this case the shell forks.  Now we have two processes
apart from the main shell itself: the forked shell is in the superjob, and
will eventually finish running the function.  The vi process is in the
subjob.  The superjob may have more than one process if the structure of
the code is more complicated e.g. a pipeline that needs to finish only
when the subjob is done.

Why two different jobs?  Because we have two processes separately forked
from the main shell.  When the subjob exits, the main shell will tell
the superjob to carry on, so it will print that echo --- that's why the
forked shell is the "super"job, not the other way around, and why we
show the superjob to the user rather than the subjob, because it's the
longer lived of the two.

We are having to jump through hoops because the user has been told there
is only the superjob.

On

% bg

both jobs are put into the background, though the forked shell
(superjob) will stay suspended for the time being.  In this case, the
subjob gets SIGTTOU and we now report *this* suspension (we suppress the
fact that the superjob's forked shell process is suspended as it's not
relevant to what the user thinks is going on).

On

% fg

both jobs are put back into the foreground, but again the forked shell
stays suspended.

When vi (subjob) exits, the forked shell is woken up and runs the rest
of the function.

pws



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