5 Years
Discussion Span
Last Post by Smeagel13

Have one controlling program that can start and kill the others with system calls such as fork() and execve().


When you compile your 'C' code, an object file is generated by the compiler. Such object files from each program(from the collection of programs) are build into a single binary(executabe) file by another program called linker.


fork() and exec*() work together to make a new process. I put a * after exec because there is a group of exec functions such as execl, execvp, and others.

Fork copies the program completely and the copy continues running the parent program's code starting on the line after fork. fork() returns 0 for the child process (the new one) and returns the child process's process id to the parent.

You might use fork() like this:

int pid;

pid = fork();
if (pid) {
  /* Parent does something here */
} else {
  /* Child does something here */

The exec functions take a program and load it into the new process, erasing the parent's program.

For example, here is how you would run "ls -l" in the new forked() process:

execl("/bin/ls", "/bin/ls", "-l", (char *) 0);

Edited by Mouche: n/a


or ... are you asking how most of unix utilities work?

What appears to be several programs is actually just one program with multiple functionality bundled up, when the program is launched it looks at argv[0] to see what 'program' or functionality to run!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.