Program: Executable instructions
Process: Running instance of a program
Process States
Fork: Create a new process on the exact point of execution
Init Process: PID 1, calls wait() in a loop to collect orphan processes
// fork() returns:
// * >1 child's pid for parent process
// * 0 for child process
// * -1 on failure
int fork = fork();
if (fork == -1) perror("fork");
// wait(&status) waits for one child process to finish, returns -1 on failure
pid_t pid;
int result;
if ((pid = wait(&result)) == -1) perror("wait");
else {
if (WIFEXITED(status))
printf("Child %d exited with code %d\\n", pid, WEXITSTATUS(status));
else if (WIFSIGALED(status))
printf("Child %d exited with signal %d\\n", pid, WTERMSIG(status));
}
Exec: Replace the current process with the new program
// exec Variants:
// * l: Parameters as vararg, v: Parameters as array
// * p: Search for binary in paths
// * e: Specify environment variables
// Example: execvp(char* executable, char** null-terminated argv)
// * The code following exec will only execute on error
execl("./hello", NULL);
perror("exec");