-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Executing asynchronous command #92
Comments
POSIX employs the notion of process IDs known in the current shell execution environment. The relevant specifications are follows:
To sum up, whether a job's process ID is known in the current shell execution environment or not affects whether Note that, according to POSIX, the process ID remains unknown when a foreground job is stopped. That means you cannot |
To understand the process ID reuse problem, consider the following script: exit 1&
pid1=$!
exit 2&
pid2=$!
wait $pid1
echo $?
wait $pid2
echo $? This should print
The only known reliable way to prevent such reuse is to keep the child process pending by not performing |
The current plan is that the shell Rationale:
|
POSIX allows the shell to forget jobs whose process ID is not known to the user as a result of expanding (Update: To support this, the shell has to update a flag when expanding |
POSIX only requires the shell to include the process IDs of live asynchronous tasks in the list of known process IDs in the current shell execution environment. This implies a process may be removed from the list when put in the foreground and then be listed again when moved to the background. The shell must retain such processes in the list because they are recently added entries. When we have more than CHILD_MAX jobs, it is not necessarily the earliest started job that we can drop from the list. |
POSIX is silent on how many jobs the shell must retain. Maybe we should interpret it as the shell is required to retain infinite number of jobs it may spawn. If so, we effectively have no chance to prune asynchronous tasks from the internal list. Even if the shell remembers tasks without limit, process IDs will eventually wrap around and get reused, so the shell's internal list would not overflow in practice. |
If an asynchronous task is not job-controlled and its process ID is not expanded with |
If the shell immediately removes an asynchronous task from its internal list just because its process ID has not been expanded before another asynchronous task is started, the Conclusion: The shell should not remove any tasks from the job list unless jobs are properly waited for or disowned. |
Env
because we don't need to.Env
Expand special parameters $? and $! #95$!
.Env
Manage jobs in JobSet #153jobs -l
.Forgetting old jobs when there are too many jobs to rememberQuestions:
waitpid
for any child process immediately after the child's state changes, or only when the user explicitly invokes thewait
orfg
built-in?waitpid
immediately reduces zombie processes that occupy the process ID space, but incurs a possibility of the child process ID reused beforewait
ed for.waitpid
selectively for child processes that cannot bewait
ed for by the user?waitid
withWNOWAIT
?waitid
for child processes with the following combinations of options:WSTOPPED|WCONTINUED|WNOHANG
withoutWNOWAIT
: to report the latest job status as to whether it is running or stopped. WithWNOWAIT
, we would keep receiving the old status even after a new status gets available.WEXITED|WNOHANG|WNOWAIT
: to report exited or signaled jobs. WithoutWNOWAIT
, we would consume the process ID of an exited job and allow another process to reuse the same process ID, thereby disabling the user to specify which job towait
for.waitpid(-1, ...)
orwaitid(P_ALL, ...)
occasionally? If not, how does it consume its unknown child that had forked before the shell started?$!
?waitpid
to allow the OS to reuse the process ID.jobs
built-in.$!
expansion.Optional todos:
These are needed to support the
PIPESTATUS
special variable. They also allows extension ofjobs -l
to print the process IDs of all pipeline components. [1]The text was updated successfully, but these errors were encountered: