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.c45 lines
/*
 * 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;
}
2

Alarm Manager (Parent)

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

alarm_manager/alarm_manager.c135 lines
3

Parent-Child Coordination

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

coordination.c108 lines
Expected Outputclick Run to execute live
[Program timed out after 5 seconds]
4

Signals Across 4 Children

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

multiply_signals.c136 lines
Expected Outputclick Run to execute live
[Program timed out after 5 seconds]

Open Full Terminal

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