~/ learn/ comp-325/ cards/ Replacing the program, keeping the process
1 of 11

fork, exec, wait — the shape of a shell

fork, exec, wait — the shape of a shell

Answer

pid_t pid = fork(); if (pid == 0) { execl("/bin/date", "date", (char *)0); perror("execl"); _exit(127); } wait(NULL);

The child becomes date while keeping its PID; the parent blocks in wait() until it finishes. The two lines after execl run only if the exec failed.

S&K 3e ch19 §19.3.5 and ch17 §17.5, §17.7; the exec family per POSIX.1-2024

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/98f5e123-a00b-4fec-9cc0-29a6b4f67000/flashcard utf-8 LF