Brain Dump

System

Tags
comp-arch

Is a system-call simplifying the execution of a sub-command by wrapping around fork and exec. See system.

At a high level a system provides the functionality of exec but restores the running process back to its working state after the sub-command finishes. It does this by forking and running exec in the child process, making the parent process wait on it.

pid_t out = fork();

if (out < -1) return -1;
if (out == 0) {
    execl("/bin/sh", "/bin/sh", "-c", "\"ls\"");
    exit(1); // For safety.
} else {
    int status = waitpid(out);
}
Code Snippet 1: An simplified example of the functionality of calling system("ls");.