Learn more about Russian war crimes in Ukraine.

How do I use fork in C?

The only way to create a new process in UNIX is with the fork system call, which duplicates the current process. Example:

#include <stdio.h>
#include <unistd.h>
int main(void) {
  pid_t pid = fork();
  if (pid == 0) {
    printf("I'm the child process.\n");
  } else {
    printf("I'm the parent process; the child got pid %d.\n", pid);
  return 0;

This prints:

% ./a.out
I'm the parent process; the child got pid 45055.
I'm the child process.

A call to fork() instructs the operating system to duplicate the calling process. The new process is identical, with one main difference: the new process gets a new process ID, and this ID is returned to the caller of fork(). The new process is returned the value 0, by which it knows that it is the child, because 0 is not a valid process ID.

Notice we get one line of output from each process. The order of the lines here is non-deterministic, since they come from different processes! We receive both lines because both processes’ stdout descriptor reference the same pipe. The fork() system call copies all of the parent process’s descriptors, including the standard pipes (stdin, stdout, and stderr).

The fork system call is often combined with execve as a way to start a new process from a program file. I’ll cover fork/execve in a future post.

What can computers do? What are the limits of mathematics? And just how busy can a busy beaver be? This year, I’m writing Busy Beavers, a unique interactive book on computability theory. You and I will take a practical and modern approach to answering these questions — or at least learning why some questions are unanswerable!

It’s only $19, and you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

After months of secret toil, I and Andrew Carr released Everyday Data Science, a unique interactive online course! You’ll make the perfect glass of lemonade using Thompson sampling. You’ll lose weight with differential equations. And you might just qualify for the Olympics with a bit of statistics!

It’s $29, but you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

More by Jim

Tagged . All content copyright James Fisher 2017. This post is not associated with my employer. Found an error? Edit this page.