Lesson 07advancedProcesses

Signal Coordination

Applied signal programming bringing together fork, exec, and signals. Includes a two-program alarm management system, parent-child coordination using signals, and multiplexing signals across four child processes.

coordinationalarmmulti-processSIGUSR1SIGUSR2
1

Alarm Handler (Child)

The child program that handles alarm signals — launched by the alarm manager via exec.

alarm_manager/alarm_handler.c
/*
 * alarm_handler.c — Alarm handler child (executed by alarm_manager)
 *
 * Key concepts: Child process that handles SIGALRM, called via exec
 * Compile: gcc -o alarm_handler alarm_handler.c
 * Run:     ./prog
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>


int main(int argc, char *argv[]) {
    printf("\n");
    printf("══════════════════════════════════════\n");
    printf("  Alarm Handler (Child Process)\n");
    printf("══════════════════════════════════════\n\n");
    if (argc != 3) {
        fprintf(stderr, "Usage: %s <seconds> <parent_pid>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    int seconds = atoi(argv[1]);
    pid_t parent_pid = atoi(argv[2]);

    printf("[Handler PID %d] Will sleep %d seconds, then signal parent PID %d\n", getpid(), seconds, parent_pid);
    fflush(stdout);

    // Sleep for the specified number of seconds
    sleep(seconds);

    // Send SIGUSR1 to the parent process
    if (kill(parent_pid, SIGUSR1) == -1) {
        perror("[Handler] Failed to send SIGUSR1 to parent");
        exit(EXIT_FAILURE);
    }

    printf("[Handler PID %d] Sent SIGUSR1 to parent PID %d after %d seconds.\n", getpid(), parent_pid, seconds);

    return 0;
}
Reference — not run on its ownStarted by alarm_manager.c with exec — run that example.
2

Alarm Manager (Parent)

A parent program that forks and execs the alarm handler, then coordinates alarm scheduling across processes.

alarm_manager/alarm_manager.c
$ gcc -o alarm_handler alarm_manager/alarm_handler.c && gcc -o alarm_mgr alarm_manager/alarm_manager.c
$ ./alarm_mgr
Expected Outputclick Run to execute live
══════════════════════════════════════
  Alarm Manager (fork + exec)
  Each alarm spawns a child process
══════════════════════════════════════

[Parent PID 1558] Alarm Manager ready.
[Info]  Type seconds to set an alarm, 'exit' to quit.
[Info]  Each alarm forks a child that sleeps, then signals back.

myalarm> 2
exit
[Parent] Forked child PID 1569 — alarm set for 2 seconds.
myalarm> Exiting Alarm Manager.

══════════════════════════════════════
  Alarm Handler (Child Process)
══════════════════════════════════════

[Handler PID 1569] Will sleep 2 seconds, then signal parent PID 1558
[Handler PID 1569] Sent SIGUSR1 to parent PID 1558 after 2 seconds.

[ALARM #1] Signal 10 received — a child timer fired!
myalarm> student:~$
Asks for input — sample input is one click away
3

Parent-Child Coordination

Parent and child synchronize execution using SIGUSR1 and SIGUSR2, taking turns in a controlled sequence.

coordination.c
$ gcc -o coordination coordination.c
$ ./coordination
Expected Outputclick Run to execute live
══════════════════════════════════════
  Parent-Child Signal Coordination
  Child1 → Parent → Child2 → Parent
══════════════════════════════════════

[Parent PID 1565] Signal handlers registered.

[Child 1 PID 1566] Started. Working for 2 seconds...
[Parent] Forked Child 1 (PID 1566) and Child 2 (PID 1567).
[Child 2 PID 1567] Started. Waiting for signal from Parent...
[Parent] Waiting for Child 1 to finish its work...

[Child 1 PID 1566] Done! Sending SIGUSR1 to Parent (PID 1565).

[Parent] Received SIGUSR1 from Child 1 — task acknowledged.
[Parent] Sending SIGUSR2 to Child 2 (PID 1567) to start its task...
[Child 2 PID 1567] Received signal! Task done. Sending SIGUSR2 to Parent.

[Parent] Received SIGUSR2 from Child 2 — all tasks completed!
[Parent] Terminating both children...
4

Signals Across 4 Children

A parent process that forks four children and coordinates them by sending different signals to each.

multiply_signals.c
$ gcc -o multi_sig multiply_signals.c
$ ./multi_sig
Expected Outputclick Run to execute live
══════════════════════════════════════
  Signal Multiplexing — 4 Children
  Each child sends a different signal
══════════════════════════════════════

[Parent PID 1609] Handlers registered for SIGINT, SIGUSR1, SIGUSR2, SIGTERM.

[Child 0 PID 1610] Will send SIGINT in 2 seconds.
[Child 1 PID 1611] Will send SIGUSR1 in 4 seconds.
[Child 2 PID 1612] Will send SIGUSR2 in 6 seconds.

[Parent] All children forked. Waiting for signals...
[Parent] Signals will arrive staggered: 2s, 4s, 6s, 8s.

[Child 3 PID 1613] Will send SIGTERM in 8 seconds.
[Child 0 PID 1610] Sending SIGINT to Parent (PID 1609)...

[Handler] SIGINT (signal 2) — Interrupt. Handling gracefully.

[Child 1 PID 1611] Sending SIGUSR1 to Parent (PID 1609)...
[Handler] SIGUSR1 (signal 10) — User-defined signal 1 received.

[Child 2 PID 1612] Sending SIGUSR2 to Parent (PID 1609)...
[Handler] SIGUSR2 (signal 12) — User-defined signal 2 received.

[Child 3 PID 1613] Sending SIGTERM to Parent (PID 1609)...
[Handler] SIGTERM (signal 15) — Termination. Cleaning up...

Open Full Terminal

Get a Linux terminal with all 4 files from this lesson pre-loaded. Compile, run, and experiment freely.