STTNet 0.7.0

Chapter 19: Process Supervision, Heartbeats, and IPC Synchronization

This chapter describes the responsibility boundaries of process launch, daemonization, shared-memory heartbeats, and System V semaphores.

1. Responsibilities of Process, HBSystem, and csemp

ClassPurposeNot
Processfork/exec or run a callable; optionally restart after exitNot full daemonization
HBSystemRegister processes in shared memory, renew heartbeats, detect, terminate, and restartNot a cross-host registry
csempSystem V semaphore for inter-process exclusionNot a thread mutex

2. Process::startProcess

// sec=-1: start once.
Process::startProcess(
    "/usr/local/bin/worker",
    -1,
    "worker", "--port", "9000");

// sec=3: restart the callable three seconds after each exit.
Process::startProcess([] { runWorker(); }, 3);

The helper does not call setsid(), change directory, set umask, or redirect standard streams. It is process launch/supervision, not traditional Unix daemonization. systemd, containers, or a dedicated supervisor are still preferred in production.

3. HBSystem heartbeat flow

  1. The service calls join() with its path, PID, and up to three arguments.
  2. The service calls renew() periodically.
  3. A separate monitor calls HBCheck(sec).
  4. On timeout, the monitor sends SIGTERM (15) and waits eight seconds. A process that remains alive receives SIGKILL (9), after which the recorded process is restarted.
Current limits: fixed System V IPC key 0x5095, up to 1000 records, fixed argument buffers, and at most three arguments. Distinct IPC keys provide isolation between independent groups on one host.

4. Inter-process semaphore

csemp lock;
if(!lock.init(0x6001))
    return 1;

lock.wait();
// Access a resource shared by processes.
lock.post();

// Resource destruction remains with the owner.
lock.destroy();

5. Shutdown behavior

A supervised service uses the graceful-shutdown model from Chapter 17. SIGKILL (9) cannot be caught and remains the final fallback.

Process supervision demo →