C System Programming: POSIX System Calls, Processes & IPC Masterclass

โšก C (C17 / C23 Standard) ๐ŸŸข Lesson 61 ๐Ÿ“‚ Phase 22: System Programming & Embedded C ๐Ÿ“… 2026 Comprehensive Master Edition
๐Ÿ“Œ Covered in this in-depth guide: POSIX System Calls vs stdio ยท File Descriptors (0 1 2) ยท fork() Process Creation ยท Copy-On-Write (COW) RAM ยท execvp() ยท Inter-Process Communication (Pipes & Signals)

Welcome to Phase 22 (Chapter 61): C System Programming โ€” POSIX System Calls, Processes & IPC Masterclass! System programming interacts directly with the operating system kernel. In this guide, you will master POSIX system calls, process creation with `fork()`, process replacement with `execvp()`, and Inter-Process Communication (IPC) via pipes.

1Process Creation & Copy-On-Write RAM Architecture
fork() Process Duplication & Copy-On-Write (COW): Parent Process (PID 100) Child Process (PID 101) โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Code & Data Segments โ”‚ โ”‚ Copy of Parent RAM โ”‚ โ”‚ fork() returns > 0 โ”‚ โ”‚ fork() returns == 0 โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ–ผ โ–ผ Both processes execute concurrently from the exact line after fork()! Kernel uses Copy-On-Write (COW) to share physical RAM pages until modified.
2Inter-Process Communication (Pipes & Signals)
C โ€” Parent-Child Communication via Unidirectional Pipeโ–ถ Run Code in C Compiler
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>

int main(void) {
    int pipefd[2]; // pipefd[0] = read, pipefd[1] = write
    if (pipe(pipefd) == -1) { perror("pipe"); return 1; }

    pid_t pid = fork();
    if (pid < 0) { perror("fork"); return 1; }

    if (pid == 0) { // CHILD PROCESS
        close(pipefd[1]); // Close unused write end
        char buffer[128];
        ssize_t bytesRead = read(pipefd[0], buffer, sizeof(buffer) - 1);
        if (bytesRead > 0) {
            buffer[bytesRead] = '\0';
            printf("[Child Received]: %s\n", buffer);
        }
        close(pipefd[0]);
        exit(0);
    } else { // PARENT PROCESS
        close(pipefd[0]); // Close unused read end
        const char *msg = "Hello from Parent Process!";
        write(pipefd[1], msg, strlen(msg));
        close(pipefd[1]);
        wait(NULL); // Wait for child to exit
        printf("[Parent]: Child finished execution.\n");
    }
    return 0;
}
3Technical FAQs

Q1: What is a Zombie process in C?

A terminated child process whose parent has not yet called `wait()` or `waitpid()` to read its exit status, leaving an entry in the OS process table.

Q2: What is an Orphan process?

A child process whose parent process terminated before it. The OS `init` / `systemd` process (PID 1) automatically adopts orphans.

Q3: What is the difference between system() and execvp()?

`system()` launches a new shell subprocess. `execvp()` completely overwrites the current process memory image with the new binary.

Q4: How do file descriptors work in POSIX?

Integers indexing an OS kernel table: 0 is stdin, 1 is stdout, 2 is stderr. `open()` returns descriptor numbers 3, 4, 5...

Q5: What is signal handler safety in C?

Signal handlers interrupt normal program flow asynchronously. Only async-signal-safe functions (like `write()`) should be called inside signal handlers.