Systems programming in C interacts directly with the operating system: processes, files, signals, sockets, and memory-mapped I/O.

File Descriptors

  #include <fcntl.h>
#include <unistd.h>

int fd = open("file.txt", O_RDONLY);
if (fd < 0) { perror("open"); return 1; }

char buf[256];
ssize_t n = read(fd, buf, sizeof buf);
write(1, buf, n);  /* stdout */
close(fd);
  

Process Management

  #include <unistd.h>
#include <sys/wait.h>

pid_t pid = fork();
if (pid == 0) {
    execl("/bin/ls", "ls", "-l", NULL);
} else {
    int status;
    waitpid(pid, &status, 0);
}
  

Signals

  #include <signal.h>
#include <stdio.h>

volatile sig_atomic_t running = 1;

void handle_sigint(int sig) {
    (void)sig;
    running = 0;
}

int main(void) {
    signal(SIGINT, handle_sigint);
    while (running) { /* work */ }
    printf("Clean shutdown\n");
}
  

Use sigaction instead of signal for portable, reliable handlers.

Memory Mapping

  #include <sys/mman.h>
void *addr = mmap(NULL, size, PROT_READ|PROT_WRITE,
                  MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
/* use addr */
munmap(addr, size);
  

errno and perror

  #include <errno.h>
if (some_syscall() < 0) {
    fprintf(stderr, "Error %d: %s\n", errno, strerror(errno));
}
  

Common Pitfalls

  • Treating compiler warnings as optional rather than actionable feedback.
  • Skipping error checks on library and system calls.
  • Copy-pasting examples without adapting to your project’s conventions.

Best Practices

  • Enable strict compiler warnings and fix them before merging.
  • Write small, testable units with clear input/output contracts.
  • Document non-obvious invariants and preconditions.
  • Use version control and code review for every change.

Memory and Performance Notes

System calls transition to kernel mode — expensive compared to function calls. Batch I/O with buffers.

Exercise

Write a program that runs fork, has the child write to a pipe, and the parent reads and prints the message.

Hint: Use pipe() and close the unused ends in each process to avoid deadlock.

Summary

Apply these concepts in small programs before moving to larger projects. Combine with adjacent topics in the learning path for deeper mastery.

Real-World Application

These concepts appear in production codebases — from operating system kernels to embedded firmware. Study open-source projects that use this topic extensively to see idiomatic patterns at scale.

Debugging Checklist

  1. Reproduce the issue with the smallest possible input.
  2. Enable compiler warnings and sanitizers.
  3. Use a debugger to inspect state at the failure point.
  4. Verify assumptions about types, sizes, and return values.
  5. Compare working and broken code paths side by side.
  6. Write a regression test once the bug is fixed.

Further Reading

Consult the ISO C standard, Effective C by Robert C. Seacord, and your compiler documentation for platform-specific behavior.

Quick Reference

Review the code examples on this page before starting the exercise. Type them manually to build muscle memory.

Additional Examples

Consider how this topic applies in a larger project:

  // Break the problem into smaller functions
// Test each function independently
// Integrate incrementally
  

Working through variations of the examples above builds deeper understanding than reading alone.