ArticleslgStudy

computer science

Fork (system call)

Fork (system call) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Fork (system call) rather than just read about it. In short: In computing, fork is an operation whereby a process creates a copy of itself. It is usually implemented as a C standard library wrapper to the fork, clone, or other system calls of the kernel.

Key takeaways

  • Fork (system call) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Fork (system call) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Fork (system call) from memory before moving on to harder problems.

Reference excerpt

In computing, fork is an operation whereby a process creates a copy of itself. It is usually implemented as a C standard library wrapper to the fork, clone, or other system calls of the kernel. For many years, fork was the primary method of process creation on Unix and Unix-like operating systems, and it remains a required interface for compliance with POSIX. Despite this, it has fallen out of favor in recent years as a result of flaws including poor performance, a lack of thread safety, and its status as a common source of security vulnerabilities.

Overview In multitasking operating systems, processes (running programs) need a way to create new processes, e.g. to run other programs. Fork and its variants are typically the only way of doing so in Unix-like systems. For a process to start the execution of a different program, it first forks to create a copy of itself. Then, the copy, called the "child process", makes any environment changes the child will need and then calls the exec system call to overlay itself with the new program: it ceases execution of its former program in favor of the new. (Or, in rarer cases, the child forgoes the exec and continues executing, as a separate process, some other functionality of the original program.) The fork operation creates a separate address space for the child. The child process has an exact copy of all the memory segments of the parent process. In modern UNIX variants that follow the virtual memory model from SunOS-4.0, copy-on-write semantics are implemented and the physical memory need not be actually copied. Instead, virtual memory pages in both processes may refer to the same pages of physical memory until one of them writes to such a page: then it is copied. This optimization is important in the common case where fork is used in conjunction with exec to execute a new program: typically, the child process performs only a small set of actions before it ceases execution of its program in favour of the program to be started, and it requires very few, if any, of its parent's data structures. When a process calls fork, it is deemed the parent process and the newly created process is its child. After the fork, both processes not only run the same program, but they resume execution as though both had called the system call. They can then inspect the call's return value to determine their status, child or parent, and act accordingly. The history of fork dates back to the 1960s as one of the earliest references to a fork concept appeared in A Multiprocessor System Design by Melvin Conway, published in 1962. Conway's paper motivated the implementation by L. Peter Deutsch of fork in the GENIE time-sharing system, where the concept was borrowed by Ken Thompson for its earliest appearance in Research Unix. Fork later became a standard interface in POSIX.

Technical

Example The following variant of the "Hello, World!" program demonstrates the mechanics of the fork system call in the C programming language. The program forks into two processes, each deciding what functionality they perform based on the return value of the fork system call. What follows is a dissection of this program.

The first statement in main calls the fork system call to split execution into two processes. The return value of fork is recorded in a variable of type pid_t, which is the POSIX type for process identifiers (PIDs), defined to be a signed integer type.

A negative result indicates an error in fork: no new process was created, so an error message is printed. If fork was successful, then there are now two processes, both executing the main function from the point where fork has returned. To make the processes perform different tasks, the program must branch on the return value of fork to determine whether it is executing as the child process or the parent process.

In the child process, the return value appears as zero (which is an invalid process identifier; the child can use getpid() to obtain its own process identifier if needed). The child process prints the desired greeting message, then exits.

The other process, the parent, receives from fork the process identifier of the child, which is always a positive number. The parent process passes this identifier to the waitpid system call to suspend execution until the child has exited. When this has happened, the parent resumes execution and exits by means of the return statement.

Communication The child process starts off with a copy of its parent's file descriptors. For inter-process communication with data exchange and synchronization between parent and child processes, pipes can be used. The parent process will often create one or several pipes, and then after forking the processes will close the ends of the pipes that they do not need. To share the same variable between the parent and child processes, memory mapping or shared memory must be used.

Variants

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Fork (system call)

Start with the simplest possible case. Write down what Fork (system call) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Fork (system call) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Fork (system call) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Fork (system call)

In research
Fork (system call) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Fork (system call) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Fork (system call) is common in secondary-school and first-year university syllabi. It links to neighbouring topics C POSIX library, Process (computing), System calls, so understanding it makes those chapters shorter.
In everyday life
Look for Fork (system call) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Fork (system call)” →

How to study Fork (system call) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Fork (system call) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Fork (system call) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Fork (system call) in simple terms?

In computing, fork is an operation whereby a process creates a copy of itself. It is usually implemented as a C standard library wrapper to the fork, clone, or other system calls of the kernel.

Why does Fork (system call) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Fork (system call)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Fork (system call).

Tags

  • C POSIX library
  • Process (computing)
  • System calls

Keep exploring