On 9/19/2013 9:39 PM, Keith Rosenberg wrote:
> It would be great if there was like an "exit and do callback" mechanism or
> something, like, process all these procedures and then Exit(3, func(){ /*
> cd to dir */ }) but it would be strange that you would tell a program to
> terminate and then do something.
Although it's not relevant to the original poster's problem, it's
worth nothing that asymmetry in UNIX/Linux. When you invoke a program,
you get to pass an array of parameters, the command line args, and a
collection of name-value pairs, the environment variables. It's like
a function call with positional parameters and variables imported
from an outer scope.
But when a process exits, all it sends back is one integer value,
the return code. "Exit" doesn't have parameters like "argc" or
"argv", and there's no way to return or affect environment variables.
This is a lack. It's one of the things that tends to lead to rather
"blind" shell scripts, makefiles, and GUIs, where the controlling
process has very limited info about what the subprocess did.
A common Unix/Linux GUI element is something that does something
using a command-line tool based on GUI actions, but displays what
happened to the user as a text stream in a scroll box. That bit of
inelegance derives from the one-way model of subprocess communication.
I know why it was done that way. It's a quirk of the PDP-11 UNIX
implementation of process launch. That's what also produced the rather
strange fork/exec approach to starting a subprocess. Fork was
originally implemented by swapping out the program to disk, then, rather
than releasing the memory of the in-memory copy, the process table
entry was duplicated, with one entry referring to the in-memory
process and one referring to the swapped-out process. In that
model, there was no way to get variable-length data back.
This bit of legacy is so deeply embedded in the minds of UNIX/Linux
programmers that they don't even notice it.
John Nagle