The purpose of this post (as with its predecessors although it has a while) is to collect use-cases for fork which don't involve invoking exec in the forked process to run another program.
A program I'm dealing with acts as system backend to a cloud-based (Heroku) web UI offering system configuration, monitoring and access features. Among other things, it supports interactive shell sessions based on an Angular terminal widget running in a brower¹. This program is supposed to support interruption-free upgrades in future, ie, replacing its running instance with an updated one without disconnecting active shell sessions. The following general algorithm will be used for that (not yet completely implemented).
1. Switch from processing network input to buffering it.
2. Wait for all outstanding requests to complete.
3. Fork to have the old program running in a new process where it can continue to receive and buffer network input and will also keep the running state for later restoration.
4. Exec the updated program in the original process.
5. After that has completed enough of its initialization (established a WebSocket connect to Heroku) to be useful, contact the old program running in the forked process to determine the state information necessary to create shell &c management objects in the updated program's process and do this.
6. Subscribe to all necessary ActionCable channels, start buffering network input,
7. Contact the old program again to received buffered network input. After that was sent, the old program will terminate itself.
8. Process this input and then switch to processing new input received over the ActionCable bus.
This also relies on parent/child relations between processes not being affected by an exec and on both the forked process and the newly executed program in the old process inheriting open file descriptors from it.
¹ Using ActionCable and JSON for all data communication --- we've come some miles since the Nagle-algorithm was invented to stop telnet sessions from causing congestion collapses.