CSE Roadmap

Core tier · about 2 hours · 7 parts

Part 5 of 7 · 3 min read

The first process

Think of a restaurant that opens in order: the manager unlocks the door, then starts the cooks, and the cooks start the helpers. The first process starts the others in the same way. The analogy stops in one place: a manager may leave once everyone works, and the first process stays running for as long as the system does.

In plain words: Once the kernel is running with the root file system mounted, it starts the first user process. On modern Linux this is systemd, which always has process id 1. Every other process descends from it. It reads its configuration, then starts other programs: services, the network, the login screen.

Programs that run in the background for the whole life of the system are daemons. A web server, a database or the program that handles logins is typically a daemon. At last the system shows a login prompt or a graphical screen. Only now is the system said to be fully booted and running.

The key lines.

A short program can show the data the kernel exposes through the /proc file system. It prints the time since boot and the name of process 1:

#include <stdio.h>

int main(void) {
    double up = 0, idle = 0;
    FILE *f = fopen("/proc/uptime", "r");
    if (!f || fscanf(f, "%lf %lf", &up, &idle) != 2) { perror("uptime"); return 1; }
    fclose(f);
    int days = (int)(up / 86400);
    int hours = (int)(up - days * 86400.0) / 3600;
    int mins = (int)(up - days * 86400.0 - hours * 3600.0) / 60;
    printf("up %.2f seconds = %d days, %d hours, %d minutes\n", up, days, hours, mins);
    char name[64] = "";
    f = fopen("/proc/1/comm", "r");
    if (f && fgets(name, sizeof name, f)) printf("process 1 is: %s", name);
    if (f) fclose(f);
    return 0;
}

On the test machine, a small virtual machine, the output was:

up 356.43 seconds = 0 days, 0 hours, 5 minutes
process 1 is: process_api

On a normal Linux laptop, the second line would read process 1 is: systemd. The first process is whatever program the kernel was told to start. In this virtual machine it is a custom program, not systemd. The principle is unchanged: one first process, and everything else descends from it.

A worked example.

A check of the arithmetic: 100,000 seconds is 1 day (86,400), leaving 13,600 seconds, which is 3 hours (10,800) and 46 minutes (2,760), with 40 seconds over. So "1 day, 3 hours, 46 minutes".

Daemon

A daemon is a process that runs in the background without a controlling terminal (a terminal window that started it and could end it), usually started at boot and kept running to provide a service such as logging, scheduled jobs or remote login.

Example: sshd waits for remote login requests, and cron runs scheduled jobs.

Check yourself: 1. Why does the first process always have number 1? 2. What is a daemon? 3. How long is 200,000 seconds of uptime in days, hours and minutes?