Sobes.tech
Junior — Middle

Which Linux system functions are responsible for starting and creating new processes?

sobes.tech AI

Answer from AI

In Linux, the system calls used to create and launch new processes are:

  • fork() — creates a new process that is a copy of the calling process. The new process is called a child.
  • exec() (for example, execl(), execvp(), and other variants) — replaces the current process with a new executable.

A typical scenario for launching a new process looks like this:

  1. The process calls fork(), creating a copy of itself.
  2. In the child process, exec() is called to load and run a new program.

Example in C:

pid_t pid = fork();
if (pid == 0) {
    // Child process
    execl("/bin/ls", "ls", "-l", (char *)NULL);
    // If exec returns, an error occurred
    perror("execl");
    exit(1);
} else if (pid > 0) {
    // Parent process
    wait(NULL); // Wait for the child to finish
} else {
    // fork error
    perror("fork");
}

Thus, fork() and exec() are key system functions for creating and launching new processes in Linux.