Module 2 · Lesson 10 of 30

Job control; mysh complete (mini project)

Build reliable shell job control with process groups, terminal ownership, fg/bg, signal-safe child tracking, and pipeline stress tests.

  • Beginner to advanced
  • 32 min read
  • 60 min lab
What you will need

Ubuntu 24.04 in WSL2 or a VM; an interactive Linux terminal, Bash, gcc, strace, and your mysh v3 sources; optional ARM board

By the end you can 7 objectives
  • Identify process IDs, process groups, sessions, and a terminal's foreground group.
  • Explain how terminal modes and control keys affect foreground and background jobs.
  • Use jobs, fg, and bg to stop and resume a pipeline in Bash.
  • Implement parent-side builtins and a job table that tracks every pipeline member.
  • Integrate setpgid, tcsetpgrp, and waitpid without losing child state changes.
  • Diagnose terminal-access failures, hangups, and orphaned process groups.
  • Evaluate mysh against Bash and design extensions for command lists and substitution.
Read the lesson below; the video will appear here when published.
01

One job can contain several processes

3 min read

Your previous shell connected programs with pipes and redirected their input and output. Now imagine running sleep 60 | cat. You press Ctrl-Z, expect a prompt, and later type fg. Finishing this mini project means making that whole sequence reliable.

A groups a command's processes for shell bookkeeping. One pipeline may have several children, each with its own process ID, or PID. A lets the kernel address those processes together. Its identifier is the PGID. The shell normally creates one group for each interactive job.

The has a PID equal to its PGID. It need not remain alive for the group to exist. Other members can continue after it exits. Therefore, waiting only for the leader cannot tell you whether a pipeline has finished.

Try background execution in an interactive Bash terminal. The ampersand asks Bash to start the job without waiting for completion. The next commands inspect Bash's records and the kernel's process information.

bash
sleep 60 | cat &
jobs -l
ps -o pid,ppid,pgid,sid,tpgid,stat,args -p "$$" --ppid "$$"

Do not expect fixed numbers. PID identifies each process; PPID identifies its parent. Both pipeline children should share a PGID. SID identifies their session, explained next. TPGID identifies the terminal's foreground group. In STAT, T indicates a stopped process; other letters describe other process states. This ps format uses procps on Ubuntu; BusyBox ps may offer fewer columns.

getpgrp() returns the caller's PGID. getpgid(pid) queries another process, and getpgid(0) queries the caller. setpgid(0, 0) makes the caller a group leader. setpgid(child_pid, job_pgid) places a child in the job's group, subject to session and execution restrictions.

These are process-management operations, not pipe operations. Connecting two file descriptors does not place their owners in a common group. Your launcher must do both jobs separately. Likewise, putting processes into a common group does not connect their data streams.

Sending kill(-pgid, SIGCONT) targets the process group whose positive identifier is pgid. Require a tracked job PGID greater than one before negating it. Zero and minus one have special meanings to kill(). A stale or uninitialized group identifier must never become a broad signal target.

On an ARM board the model is the same. A small shell still needs group-wide control, especially when a diagnostic pipeline includes a long-running sensor reader. Memory limits change your table size, not the meaning of a PGID.

A pipeline is one jobSessionShell groupmyshJob groupsleepfirst stage; group leadercatsecond stage; same PGIDSessionShell groupmyshJob groupsleepfirst stage; group leadercatsecond stage; same PGID
The shell stays in its own group while every stage of a pipeline shares another group.
02

Sessions and the terminal's foreground owner

3 min read

A gathers related groups under a session ID, or SID. The session leader has a PID equal to the SID. A login or terminal-session setup program establishes this structure. An interactive shell launched inside another shell can belong to an existing session without leading it.

The connects a session to interactive terminal control. A terminal may control at most one session. Within that session it records one foreground PGID. Other groups in that session are background groups relative to this terminal.

Foreground does not mean using more CPU time or running first. It describes terminal access and signal routing. A background job still runs normally when it has work. It can read a file or pipe without asking for terminal ownership.

tcgetpgrp(tty_fd) returns the terminal's foreground PGID. tcsetpgrp(tty_fd, pgid) changes it. The descriptor must refer to the caller's controlling terminal, and the target group must belong to the same session. Errors matter: a terminal descriptor alone does not prove that this relationship holds. See the terminal group API.

Use /dev/tty when you specifically need the controlling terminal. Opening it gives you a descriptor even when standard output points to a file. Opening it fails if you have no controlling terminal. Keep mysh's terminal descriptor separate from the descriptors used for command redirection. Mark it close-on-exec so launched programs do not inherit an unnecessary descriptor.

isatty(fd) tests whether a descriptor refers to a terminal. It does not establish a controlling terminal or prove foreground ownership. For this project, enable interactive job control only when input is interactive and the controlling-terminal checks succeed. A script with piped input must avoid prompts and terminal handoffs.

setsid() creates a new session and process group, with the caller leading both. The new session initially has no controlling terminal. It does not automatically close existing descriptors. A group leader cannot call it successfully. A fresh child is commonly used when deliberately creating a detached session; ordinary interactive job launch should use setpgid() instead. The setsid manual documents those boundaries.

For a nested interactive mysh, first compare tcgetpgrp(tty_fd) with getpgrp(). If they differ, stop the current group with SIGTTIN and retry after the parent foregrounds it. During this startup check, SIGTTIN must be unblocked with its default action. Only after gaining the foreground should mysh establish its own group if needed and claim the terminal.

A session leader is already a group leader; do not blindly call setpgid() on it. Save the shell's final PGID and terminal settings for later restoration. A board started directly from a service or an incorrectly configured serial launcher may lack a controlling terminal. Report that condition and use noninteractive behavior instead of repeatedly trying failed handoffs.

Foreground ownership changesShell groupControllingterminalJob grouptcgetpgrptcsetpgrp: jobforeground accessstop or exittcsetpgrp: shell1. Shell group → Controllingterminaltcgetpgrp2. Shell group → Controllingterminaltcsetpgrp: job3. Controlling terminal → Jobgroupforeground access4. Job group → Shell groupstop or exit5. Shell group → Controllingterminaltcsetpgrp: shell
Several groups share a session, while the controlling terminal selects only one foreground group.
03

Control keys and terminal modes

4 min read

Normally Ctrl-C interrupts the foreground job and Ctrl-Z suspends it. The terminal generates SIGINT and SIGTSTP, respectively, for the foreground process group. SIGINT terminates by default. SIGTSTP stops by default, but a program can catch or ignore it. SIGSTOP also stops processes and cannot be caught or ignored.

The shell should not forward a second copy of each terminal-generated signal. Once the job owns the foreground, the terminal already targets its group. Forwarding from the shell can duplicate a signal or reach the wrong job after a handoff. Programmatic forwarding, when explicitly required by another input source, must target the whole validated group.

A processes characters inside the kernel's terminal layer. Linux's usual discipline is N_TTY. It handles editing, echo, and signal characters above physical serial or virtual terminal transport. Kernel documentation describes its input processing.

1Inspect the settings

Run this in your working terminal before changing anything. It displays the terminal's current settings rather than a fixed configuration.

bash
stty -a

Find intr, susp, icanon, isig, and echo. The interrupt and suspend characters are configurable. With ISIG enabled, VINTR and VSUSP select signal-generating characters. Turning off ICANON alone does not disable those signals.

2Separate line input from raw input

collects editable input lines before delivering them. Noncanonical mode clears ICANON and uses VMIN and VTIME to control read completion. With VMIN=1 and VTIME=0, a blocking read waits for at least one byte, within the requested size.

With both values zero, a read returns immediately, possibly with zero bytes. With VMIN=0 and positive VTIME, a timer starts with the read. A timeout can return zero without a terminal hangup. With both positive, the timer starts after the first byte and measures gaps between bytes. It is not an overall deadline while waiting for the first byte.

largely disables terminal interpretation of input bytes. It includes more changes than clearing ICANON. In glibc, the non-POSIX helper cfmakeraw() also disables ISIG and echo, among other transformations. Under strict compilation, glibc exposes that helper with _DEFAULT_SOURCE.

3Save and restore terminal state

tcgetattr() fills a struct termios. Its c_iflag controls input processing; c_oflag controls output processing. c_cflag controls hardware-related settings, while c_lflag contains local behavior such as ICANON, ISIG, and ECHO. c_cc holds special characters and read thresholds. Change a saved copy rather than constructing an empty structure.

tcsetattr() applies settings. TCSANOW applies immediately, TCSADRAIN waits for pending output, and TCSAFLUSH also discards unread input. Check every return. Choose deliberately: waiting for a serial transmitter to drain can delay terminal recovery. The termios reference specifies these actions.

After a foreground job stops, reclaim the terminal, capture its current modes into that job's record, then restore shell modes. Before fg resumes it, give it the foreground and restore its saved modes. Otherwise an editor may return to the wrong input settings. This is the bridge to M2-S11A: serial devices use the same interface, with additional line-speed and hardware choices.

Recover a damaged prompt

If input no longer echoes, you can often type stty sane and press Enter blindly. This restores conventional settings, not your exact previous configuration. Exact restoration requires your saved struct termios.

Input interpretationInput bytesN_TTYlinedisciplineISIGcharacter?VINTR orVSUSPInput datacanonicallines or noncanonicalbytesExit from step 3: signal characterForeground group signalInput bytesN_TTYlinedisciplineISIGcharacter?VINTR orVSUSPInput datacanonicallines or noncanonicalbytesExit from step 3: signalcharacterForeground group signal
Terminal settings choose between line input, byte input, and foreground-group signals.
04

Design the job table and parent-side builtins

4 min read

The mini project extends your existing parser, pipe setup, and redirection code. Its required language adds a trailing unquoted &, plus cd, exit, jobs, fg, and bg. Preserve earlier quoting rules. A quoted ampersand is an argument; an operator token is syntax. Splitting blindly on spaces cannot make this distinction.

A runs shell implementation code. cd must run in the parent to change the shell's directory. exit must affect the parent. fg and bg need the parent's job table and terminal state. Define these parent-side operations as standalone foreground commands for this project. Reject unsupported pipeline or background forms explicitly.

Use a stable job number, a PGID, a saved command string, and the pipeline's child records. Each child needs a PID, a running/stopped/done state, and its most recent wait status. Save the last pipeline stage's PID separately for exit-status selection. A job may also need saved terminal modes and a flag indicating whether those modes exist.

Do not confuse %1 with PID 1. It selects job number 1 from this shell's table. Another shell has a different table. For a manageable interface, require fg %N and bg %N, accepting positive decimal job numbers only. Parse with strtol(), checking errno, the end pointer, range, and existence. Explain this deliberate subset rather than implying every Bash job-selection form works.

Compute job state from all recorded members:

Condition Displayed job state
Every member is done Done
At least one live member; every live member is stopped Stopped
Any live member is running Running

Keep completed records until you have delivered any completion notice and preserved the required exit status. Then release the command string and member storage. Reusing a table slot should not silently change which existing job a saved number names. Document numbering and wraparound policy.

jobs first drains pending child statuses, then prints a snapshot. bg validates a stopped job and sends SIGCONT to its group without a terminal handoff. fg first hands over the terminal, restores saved job modes, continues a stopped group, and waits for completion or a group-wide stop. Bringing an already running background job forward does not require SIGCONT.

For sleep 60 | cat tracked as job %1 with default signal actions: Ctrl-Z stops the foreground job and returns the terminal to the shell; bg %1 resumes the group without a handoff; fg %1 hands over the terminal without another SIGCONT if already running; Ctrl-C then terminates the foreground pipeline. Done requires every member to finish.

  • states: [{"id": "foreground", "label": "Job %1 running in foreground"}, {"id": "stopped", "label": "Job %1 stopped; shell owns terminal"}, {"id": "background", "label": "Job %1 running in background"}, {"id": "done", "label": "Job %1 done; shell owns terminal"}]
  • transitions: [{"from": "foreground", "to": "stopped", "event": "Ctrl-Z: all live members stop; shell reclaims terminal"}, {"from": "stopped", "to": "background", "event": "bg %1: SIGCONT to group; shell keeps terminal"}, {"from": "stopped", "to": "foreground", "event": "fg %1: hand over terminal, restore modes, SIGCONT"}, {"from": "background", "to": "foreground", "event": "fg %1: hand over terminal; no SIGCONT needed"}, {"from": "foreground", "to": "done", "event": "Ctrl-C: all members terminate with default SIGINT action"}, {"from": "foreground", "to": "done", "event": "All pipeline members finish; shell reclaims terminal"}, {"from": "background", "to": "done", "event": "All pipeline members finish; shell keeps terminal"}]

For cd, check arguments and chdir() errors; define whether no argument uses HOME. If a parent builtin has redirection, save its original descriptors, apply redirection, execute, and restore them on every path. Otherwise jobs > report.txt may redirect every later prompt. Parse exit numbers with the same care as job numbers.

Choose an explicit exit policy. In this project, an initial exit with live jobs warns and keeps the shell running. A consecutive repeated exit or end-of-input sends SIGHUP, then SIGCONT, to tracked live groups before the shell leaves. For incoming SIGHUP, set a shutdown flag in a handler; normal code performs the same group notification without another prompt. This informs and wakes jobs; it does not guarantee their termination. Document that limitation and clean up lab jobs deliberately during testing.

What a job remembersJob recordJob numbershell-local selectionPGIDkernel group identifierCommand textChildrenPID, state, wait statusLast stage PIDTerminal modessaved settings and validity flagJob recordJob numbershell-local selectionPGIDkernel group identifierCommand textChildrenPID, state, wait statusLast stage PIDTerminal modessaved settings and validityflag
One job record joins its group identifier, child states, command text, and saved terminal modes.
05

Common mistakes and how to diagnose them

3 min read

First observe a real interactive Bash. Start tracing from a terminal so Bash has a controlling terminal. The trace goes to a file, keeping diagnostic text away from the prompt. The file name below is a lab artifact created when you perform the lab.

bash
strace -f -o /tmp/mysh-bash.trace \
  -e trace=process,signal,ioctl bash --noprofile --norc -i

Inside that Bash, run sleep 60 | cat, press Ctrl-Z, run jobs, run bg %1, then fg %1. Use the actual job number if it differs. Press Ctrl-C after foregrounding, then exit the traced shell. Inspect the file from your original shell:

bash
grep -E 'setpgid|TIOC[GS]PGRP|SIGTSTP|SIGCONT|SIGINT|wait4' \
  /tmp/mysh-bash.trace

Look for grouping, terminal handoff, stop reports, continuation, and reclamation. On Linux, libc's tcsetpgrp() appears as an ioctl() using TIOCSPGRP; tcgetpgrp() uses TIOCGPGRP. waitpid() commonly appears as wait4(). Compare the meaning of operations rather than requiring one library-call spelling in a system-call trace.

Symptom Likely cause Diagnose Fix
Ctrl-C kills mysh too Shell shares the job's group Compare PGIDs and TPGID Give the shell and job separate groups
Ctrl-Z stops a job but no prompt returns Wait ignores stopped states Inspect wait options and stopped children Use WUNTRACED and aggregate child state
cat & stops immediately Background terminal read Find SIGTTIN and the terminal input descriptor Foreground it or redirect its input
Shell stops while reclaiming the terminal SIGTTOU during tcsetpgrp Trace TIOCSPGRP and signal delivery Ignore or block SIGTTOU in the shell
One pipeline stage survives Ctrl-C Stage escaped the job group Compare every child's PGID Group every stage before execution
Pipeline never reaches EOF Parent or sibling retains a pipe writer Inspect descriptor ownership Close every unused pipe end
Prompt appears but typing is invisible Job's terminal modes remain active Inspect echo with stty Restore the saved shell modes

Background terminal writes need a careful distinction. Ordinary output usually succeeds when TOSTOP is clear. Enabling TOSTOP makes background output generate SIGTTOU, unless it is blocked or ignored. Terminal-setting operations such as tcsetpgrp() can generate SIGTTOU independently of TOSTOP. A prompt mixed with background output is not proof that terminal ownership is broken.

Background terminal reads normally generate SIGTTIN. If that signal is blocked or ignored, Linux can return EIO instead. Orphaned groups also have special failure rules. Therefore, retrying every EIO forever is not recovery. Check the descriptor, group relationship, and signal settings.

Trace on the board you ship

BusyBox shell job control depends on its build configuration and terminal setup. Check capabilities on the target. Cross-compile strace for the board's architecture and C library when it is missing; copying a desktop executable is insufficient. A service with piped input is a separate test from an interactive serial console.

Tracing changes timing, so repeat race tests without tracing. A passing traced run cannot establish that an ordering bug has disappeared. Keep a second terminal available to inspect a stalled shell and terminate only the lab processes you identified.

Background terminal accessReadOrdinarywriteSet terminalStop signalSIGTTINSIGTTOUSIGTTOUTOSTOP neededNoYesNoDefault actionStop groupStop groupStop groupExceptionsEIO casesBlocked orignoredBlocked orignored
Foreground ownership and signal settings explain why background reads and writes behave differently.
06

Launch a pipeline without terminal races

5 min read

A makes correctness depend on timing. Your parent and children run independently after fork(). If a child executes cat too early, it may read while still in a background group and stop. If it exits too early, a reaper may see it before the table contains its PID.

1Prepare before forking

Block SIGCHLD with sigprocmask() before creating the first child. Save the previous mask. Reserve the job record and member capacity before launching anything. Create the data pipes and a separate gate pipe. Never use a pipeline's data pipe as its launch gate.

The shell catches SIGCHLD. It must not explicitly ignore it or set SA_NOCLDWAIT, because those choices can discard exit status. Do not use SA_NOCLDSTOP when stop notifications drive your wait logic. After interactive initialization, the shell ignores SIGTSTP, SIGTTIN, and SIGTTOU. It also protects itself against prompt-time SIGQUIT and handles or ignores SIGINT according to its prompt policy.

2Establish the group in both processes

The first child's PID becomes the job PGID. In that child, setpgid(0, 0) creates the group. Later children use the inherited job PGID with setpgid(0, job_pgid). The parent also calls setpgid(child_pid, job_pgid) immediately after each fork. Both sides request the same result, so either scheduling order establishes the intended group.

setpgid() cannot move a process between sessions, change a session leader's group, or regroup a child after that child executes another program. With an effective gate, children cannot reach exec before the parent's grouping calls. Unexpected EACCES therefore indicates broken ordering, not an error to suppress indiscriminately. These restrictions are specified by setpgid(2).

3Restore child behavior and hold the gate

In every child, restore default actions for SIGINT, SIGQUIT, SIGTSTP, SIGTTIN, SIGTTOU, and SIGCHLD. Restore any other dispositions changed specifically by mysh as appropriate. Ignored dispositions survive exec; failing to reset them creates commands that resist Ctrl-C or terminal-access stopping.

Each child closes its inherited gate write end, then waits for EOF on the gate read end. Retry an interrupted gate read; treat other errors as launch failures. Keep the inherited SIGCHLD mask blocked until release, then restore the saved pre-launch mask before executing. Close the gate read descriptor and all unused pipeline descriptors too.

4Publish, hand over, and release

The parent records every child PID while SIGCHLD remains blocked. For a foreground job it calls tcsetpgrp() after the group exists. Only after successful setup does it close its gate write end. Because no child retains a writer, all waiting children can observe EOF and proceed. For a background job, release the gate while leaving the shell foreground.

On the successful foreground launch path, block SIGCHLD and reserve records before forking; group and hold the children, record every PID, then give the job the terminal with tcsetpgrp. Only then close the final gate writer so children see EOF, restore their masks, and execute. Releasing before handoff can let cat stop on a background terminal read.

  • states: [{"id": "reserved", "label": "SIGCHLD blocked; records and pipes reserved"}, {"id": "held", "label": "Children grouped with signals reset; gate closed"}, {"id": "recorded", "label": "Every child PID recorded; gate closed"}, {"id": "foreground", "label": "Job owns terminal; gate closed"}, {"id": "released", "label": "Gate released; children may execute"}, {"id": "waiting", "label": "Shell collects states and waits for job"}]
  • transitions: [{"from": "reserved", "to": "held", "event": "Fork children; set PGID in both sides; children reset signals and wait"}, {"from": "held", "to": "recorded", "event": "Record every child PID while SIGCHLD stays blocked"}, {"from": "recorded", "to": "foreground", "event": "tcsetpgrp succeeds for the job PGID"}, {"from": "foreground", "to": "released", "event": "Close final gate writer; children see EOF, restore masks, exec"}, {"from": "released", "to": "waiting", "event": "Process child changes until all finish or all live members stop"}]
Step by stepForeground pipeline launch
  1. Reserve: Block SIGCHLD and allocate the job's records and pipes.

  2. Fork: Create children, with parent and children establishing the same PGID.

  3. Hold: Children reset signal behavior and wait behind the gate.

  4. Record: Parent records every child before allowing status processing.

  5. Hand over: Parent assigns the terminal to the job's group.

  6. Release: Parent closes the gate writer; children restore masks and execute.

  7. Wait: Shell processes state changes until the foreground job stops or completes.

If a fork, group change, or handoff fails, cancel the partial launch before releasing it to execute. Signal known children individually if grouping is incomplete, close descriptors, and reap them. Restore the terminal and original signal mask on the failure path. Do not free a record while one of its children remains unaccounted for.

Going deeperWhat the kernel stores

On Linux, process-group and session associations use kernel PID objects rather than your shell's job numbers. Terminal state stores a foreground-group reference protected by kernel synchronization. In a matching kernel source tree, inspect kernel/sys.c for group/session system calls and drivers/tty/tty_jobctrl.c for terminal group operations. drivers/tty/n_tty.c handles normal terminal input. These layers validate each individual operation; they cannot make your entire fork/group/handoff sequence atomic. That ordering remains the shell's responsibility.

Gate the foreground launchParent shellChildrenTerminalBlock SIGCHLDFork and groupReset; await gateRecord child PIDsForeground jobClose gate writerRestore mask; exec1. Parent shell → ParentshellBlock SIGCHLD2. Parent shell → ChildrenFork and group3. Children → ChildrenReset; await gate4. Parent shell → ParentshellRecord child PIDs5. Parent shell → TerminalForeground job6. Parent shell → ChildrenClose gate writer7. Children → ChildrenRestore mask; exec
The gate keeps children from executing before grouping, table insertion, and foreground handoff finish.
07

Reap every child and resume whole jobs

4 min read

Standard signals can merge while pending. Ten child changes do not guarantee ten handler calls. A handler should only arrange a wakeup; normal shell code drains all available statuses. A retains exit status until its parent collects it. Stopped children are still alive and must not be removed as zombies.

Call waitpid(-1, &status, WNOHANG | WUNTRACED | WCONTINUED) repeatedly. WNOHANG returns zero when no report is ready. WUNTRACED includes stops; WCONTINUED includes resumptions. For a positive PID, use WIFEXITED, WIFSIGNALED, WIFSTOPPED, and WIFCONTINUED to interpret the status. Access WEXITSTATUS, WTERMSIG, or WSTOPSIG only after the corresponding predicate succeeds. See waitpid(2).

The following original component shows the drain operation. Its callback updates the matching child record and recomputes job state. Compile it as a component, not a standalone shell. Put the feature-test macro before every system header.

c
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>

typedef void (*status_fn)(pid_t pid, int status, void *context);

int collect_child_changes(status_fn record, void *context)
{
    for (;;) {
        int status;
        pid_t pid = waitpid(-1, &status,
                            WNOHANG | WUNTRACED | WCONTINUED);
        if (pid > 0) {
            record(pid, status, context);
            continue;
        }
        if (pid == 0)
            return 0;
        if (errno == EINTR)
            continue;
        if (errno == ECHILD)
            return 0;
        return -1;
    }
}

Use one owner for status collection. A handler and foreground loop must not both reap independently. Otherwise one can consume a status the other needs. A single dispatcher can collect all children, including background jobs, while a foreground wait tests only the selected job's aggregate state.

1Wake without doing table work inside a handler

Install a sigaction() handler that sets a volatile sig_atomic_t flag. Do not print, allocate, or manipulate linked lists there. Block SIGCHLD while draining and checking state. Clear the notification flag under that block before draining, so a later change cannot be erased accidentally.

2Sleep without losing the wakeup

If the foreground job still has a running member, use sigsuspend() with a temporary mask that unblocks SIGCHLD. It changes the mask and sleeps atomically. When the handler returns, the blocked mask is restored, allowing another drain-and-check cycle. A separate pause() after a state check leaves a window for a lost wakeup. This is the purpose of sigsuspend(2).

At the prompt, a flag alone does not wake a restarted line-reading call. One design uses a nonblocking self-pipe: the handler writes a byte, and pselect() waits for input or that pipe. Save and restore errno in the handler. A full pipe already provides a wakeup; never block there. Use this design or an atomic signal-mask change with pselect(). An interruptible reader alone still has a check-to-read race.

3Continue without trusting a future notification

For bg, drain pending states, validate a live stopped job, and send group-wide SIGCONT. After success, mark its stopped members running immediately. A later WIFCONTINUED confirms rather than creates the transition. Rapid continue-and-exit sequences need not expose every intermediate state to your shell.

For fg, block SIGCHLD, drain, and revalidate first. Transfer the terminal before sending SIGCONT, so a resumed reader cannot stop again with SIGTTIN. Wait until all members finish or all remaining members stop. Then reclaim the terminal even on error, save stopped-job modes if available, and restore shell modes before the prompt.

Going deeperStatus is per child, not per pipeline

An exit report from the last stage does not complete a job while an earlier stage lives. Preserve the last stage's status for the pipeline result, but keep waiting for every member. A shell-style result commonly uses the last stage; Bash can change pipeline failure selection with pipefail. That is a separate language policy. Do not make the lifecycle of job-table storage depend on whichever status happens to arrive last.

Aggregate job lifecycleRunningStoppedDoneTransitionsAll live stopRunningStoppedContinue succeedsStoppedRunningAll members terminateRunningDoneAll members terminateStoppedDoneRunningStoppedDoneTransitionsAll live stopRunningStoppedContinue succeedsStoppedRunningAll members terminateRunningDoneAll members terminateStoppedDone
Aggregate job state changes only after considering every live pipeline member.
08

Hangups and orphaned process groups

4 min read

SIGHUP originally reported a terminal connection going away. Its default action terminates a process. A serial connection can report a carrier loss; a virtual terminal can hang up when its master side closes. A connects two virtual terminal endpoints. Its slave side provides the terminal behavior seen by the shell.

For Linux's normal controlling-terminal hangup path, the kernel notifies the controlling session leader with SIGHUP and SIGCONT. Termination of a controlling session leader also causes terminal disassociation and a hangup notification to the foreground group. These paths can interact. Applications should handle terminal loss rather than depending on exactly one observable signal in every terminal setup.

An interactive Bash that receives SIGHUP normally sends SIGHUP to its jobs before exiting. It also continues stopped jobs so they can receive the hangup. That shell action explains why background jobs can die even though they were not foreground when the terminal disappeared.

A normal exit is a separate event. Do not teach that Bash always sends SIGHUP to every running job on clean exit. With huponexit off, the ordinary running-job case differs from terminal hangup. Login-shell settings, stopped-job handling, and explicit disown choices also affect behavior. The project exit policy defined earlier is intentionally explicit.

nohup arranges an ignored SIGHUP disposition and suitable handling of terminal streams. It does not create a session or supervise a service. Bash's disown changes the shell's job bookkeeping or hangup policy; it does not close terminal descriptors. A program may survive a hangup signal but still fail when using a disconnected terminal. For production board services, use a service manager and deliberate descriptor setup.

1Identify an orphaned group precisely

A process group is orphaned when every member's parent is either in that group or outside that group's session. Equivalently, no member has a parent in another group of the same session. This describes a relationship between groups and parents. It does not mean every process has lost its parent or that the group leader died.

For example, a shell in group A starts a child in group B within the same session. That parent link prevents B from being orphaned. If the parent exits and no other qualifying link remains, B becomes orphaned. The child's new parent may be PID 1 or a subreaper, a process configured to adopt certain descendants. Evaluate the resulting session relationships rather than assuming a particular adopter.

2Apply the stopped-member rule

When a group becomes orphaned and contains a stopped member, the kernel sends SIGHUP followed by SIGCONT to every member. The continuation prevents stopped processes from remaining stranded without a same-session parent able to manage them. If no member is stopped, becoming orphaned alone does not trigger that pair.

This rule is about the transition to orphaned status. It is not a periodic rescue service. An already orphaned group can later be stopped by an explicitly sent SIGSTOP and remain stopped. Nor should your shell synthesize this kernel rule merely because one child changed parent.

Going deeperBackground access after orphaning

Terminal job-control stop signals cannot safely suspend an orphaned group and expect a shell to rescue it. Linux therefore has special orphan checks for terminal access and job-control stopping. A background controlling-terminal read by an orphaned group fails with EIO rather than providing the ordinary SIGTTIN stop-and-fg workflow. Track the operation and its documented exceptions. A successful kill() also proves signal submission, not that every target executed a handler or stayed alive afterward.

Test hangups in an expendable terminal or pseudoterminal harness. Closing your only board console can destroy the evidence you need. Keep traces in files or observe from another connection, and distinguish shell-generated signals from kernel-generated events before drawing conclusions.

The orphan transition ruleGroupbecomesorphanednoqualifyingoutside-parent linkAny stoppedmember?Send SIGHUPevery groupmemberSendSIGCONTevery groupmemberExit from step 2: noNo orphan-rule pairGroupbecomesorphanednoqualifyingoutside-parent linkAny stoppedmember?Exit from step 2: noNo orphan-rule pairSend SIGHUPevery groupmemberSendSIGCONTevery groupmember
Only a newly orphaned group with stopped members receives the orphan-rule SIGHUP and SIGCONT pair.
09

Project milestones, review criteria, and language extensions

5 min read

1Preserve the previous shell

Start with mysh v3's parser and launch code. Keep pipeline data flow, redirection ordering, and descriptor closure working. Separate parsed commands from running jobs: a parsed pipeline describes what to launch; a job record describes what actually exists. This separation lets you cancel a partial launch without pretending the whole pipeline started.

Set explicit limits for input length, pipeline stages, and simultaneous jobs. On a small board, reject a full table before forking. Do not overwrite a live record to make room. Bound stored command text and check allocation failures. Event-driven waiting avoids burning CPU in a sleep-and-poll loop, which matters for battery-powered devices.

2Demonstrate required behavior

In fresh Bash and mysh terminals, compare the same sequences. Run a foreground pipeline, stop it with Ctrl-Z, inspect jobs, resume with bg, and return with fg. Then interrupt the foreground pipeline with Ctrl-C. Check that every stage terminates as expected and the shell remains usable.

Use cat & to exercise terminal-read stopping. Use sleep 1 & repeatedly to exercise fast completion. Include false | sleep 2 and sleep 2 | true: they expose incorrect reliance on the leader or last stage. Run yes | head -n 1 to check pipe closure and the normal broken-pipe signal, SIGPIPE, when no reader remains.

Also test missing programs, failed redirection, malformed %N, exhausted job slots, and end-of-input. After each failure, run another command. A shell that reports an error but loses the terminal has not recovered. For scripted noninteractive tests, use redirected input and confirm there are no prompts or terminal ioctls.

A pseudoterminal test harness must establish the slave as the shell's controlling terminal. It sends the configured interrupt or suspend byte through the master side. Pipes alone cannot test the terminal's signal generation. Avoid fixed sleeps as the only synchronization; wait for prompts or state evidence and enforce timeouts.

3Review with an observable rubric

Area Evidence required for acceptance
Grouping Every pipeline stage shares one PGID; shell uses another
Terminal control TPGID changes to the foreground job and returns before the prompt
Signals Ctrl-C affects the job; Ctrl-Z and fg work for the whole pipeline
Child accounting Fast exits, stops, and continues leave no lost records or zombies
Builtins cd persists; redirections restore; jobs/fg/bg validate selections
Recovery Partial failures close descriptors and restore terminal settings
Target behavior Interactive board console and noninteractive execution are both checked

Keep your results concrete: command sequence, observed state, expected state, and trace evidence when needed. Compare supported features with Bash, not output spacing or unsupported syntax. Record architecture and C library when a failure appears only on the board. Build with the board's matching cross-toolchain and system libraries; job-control APIs remain the same across glibc and musl.

4Extend command lists without confusing jobs

; runs command lists in sequence. && runs its right side only when its left side succeeds, meaning status zero. || runs its right side only when its left side fails. Pipelines bind more tightly than these list operators. && and || have equal precedence and group left to right: false && echo skipped || echo recovered reaches the final command.

Represent these relationships in an abstract syntax tree, a data structure describing how parsed operations nest. Preserve a completed command's status separately from its job's display state. For a first extension, finish command-list conditions only after the foreground job terminates. If it stops, return to the prompt and report that suspended-list continuation is unsupported. Full shell-like continuation needs saved evaluator state; silently treating a stop as successful completion is wrong.

5Design command substitution as concurrent capture

$(...) runs commands in a subshell context and substitutes their captured standard output. A subshell context isolates changes such as cd from the parent shell. Standard error normally remains separate. The capture removes trailing newline characters, not arbitrary spaces or internal newlines. Double-quoted substitution preserves the result as one argument. Unquoted results undergo field splitting and pathname expansion, which can split words and expand wildcard characters into filenames.

The parent must drain the capture pipe while the child can still write. Waiting first can deadlock when output fills the pipe. Bound captured output on a small target and define failure behavior. Nested parsing must recognize quotes and matching parentheses; scanning to the first closing parenthesis is insufficient. Substitution is also noninteractive execution: do not blindly reuse the top-level foreground handoff for every nested command.

These are design extensions, not requirements for declaring the core mysh project complete. Complete means the advertised subset passes its rubric, with its limits documented. A larger grammar should reuse the same reliable job lifecycle rather than hide a second launcher with different signal rules.

Build and review myshPreserve v3pipes andredirectionAdd jobcontrolgroups,handoff,childstatesProverecoverysignals,failures,terminalmodesChecktargetinteractiveand noninteractivepathsExtendlanguagelists andcommand substitutionPreserve v3pipes andredirectionAdd jobcontrolgroups,handoff,childstatesProverecoverysignals,failures,terminalmodesChecktargetinteractiveand noninteractivepathsExtendlanguagelists andcommand substitution
Preserve v3, integrate job control, prove recovery, and only then extend the command language.
10

Try it

1 min read

Open the lab Complete mysh: foreground pipelines, background jobs, and terminal recovery and start with the Bash trace exercise. Integrate the launch and wait mechanisms into your existing mysh v3, then collect evidence for each review criterion. Use this lesson's practice pack to rehearse process-group reasoning, terminal-mode diagnosis, and orphan-group rules before testing on an ARM console. Save your test notes for the next lesson, Virtual memory, brk/mmap, page faults, where you will examine another part of each process's state.

Practice sequenceObserveBashCompletemyshComparebehaviorCheck ARMtargetObserveBashCompletemyshComparebehaviorCheck ARMtarget
The lab moves from observing Bash to implementing mysh and checking behavior on a target.

Key takeaways

  • A pipeline is one shell job, usually represented by one process group.
  • The controlling terminal stores one foreground process group, independently of the parent-child tree.
  • With ISIG enabled, terminal interrupt and suspend characters signal the foreground group.
  • Foreground handoff must precede terminal access and foreground continuation.
  • Track each child before unblocking SIGCHLD, then drain all available wait statuses.
  • A stopped job needs terminal state as well as process state saved for later resumption.
  • Hangup handling, orphan-group signals, and a shell's exit policy are separate mechanisms.

Reflect

  1. Explain to a colleague, in two sentences: identify process IDs, process groups, sessions, and a terminal's foreground group.
  2. Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
  3. What surprised you when you ran the commands yourself?

On a real board

Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.

Practise this lessonExercises, quiz, interview preparation and spaced review

References and further reading

Official documentation first. Links checked when the lesson was published.

Find your next step

↑ ↓ to move · Enter to open · Esc to close