Questions tagged [fork]

The `fork()` function is the Unix/Linux/POSIX way of creating a new process by duplicating the calling process.

The fork() function is the Unix/Linux/POSIX way of creating a new (child) process by duplicating the calling (parent) process. The new process is identical in almost every respect to its parent (see the fork specification for exceptions). The fork() system call returns values in both the child and the parent process. In the child process, it returns 0, and in the parent process it returns the pid of the new child.

Traditionally, fork was a very heavyweight operation as it involved copying all memory pages owned by the process. This is nowadays usually done by marking pages copy-on-write.

The tag is used for questions related to the use of the fork() and vfork() functions and system calls.

Fork is also a common term used when discussing source control. It is the act of taking a copy of a branch (i.e. branching), and is sometimes used interchangeably with the noun branch (i.e. "how do I update my fork from my master branch?). See for this meaning.

6613 questions
251
votes
5 answers

The difference between fork(), vfork(), exec() and clone()

I was looking to find the difference between these four on Google and I expected there to be a huge amount of information on this, but there really wasn't any solid comparison between the four calls. I set about trying to compile a kind of basic…
user476033
  • 4,607
  • 8
  • 32
  • 35
247
votes
24 answers

How to make child process die after parent exits?

Suppose I have a process which spawns exactly one child process. Now when the parent process exits for whatever reason (normally or abnormally, by kill, ^C, assert failure or anything else) I want the child process to die. How to do that…
Paweł Hajdan
  • 18,074
  • 9
  • 49
  • 65
238
votes
9 answers

Differences between fork and exec

What are the differences between fork and exec?
Sashi
  • 3,069
  • 6
  • 20
  • 18
208
votes
9 answers

What is the reason for performing a double fork when creating a daemon?

I'm trying to create a daemon in python. I've found the following question, which has some good resources in it which I am currently following, but I'm curious as to why a double fork is necessary. I've scratched around google and found plenty of…
Shabbyrobe
  • 12,298
  • 15
  • 60
  • 87
188
votes
3 answers

fork() branches more than expected?

Consider the following piece of code: #include #include #include int main(void) { int i; for(i = 0; i < 2; i++) { fork(); printf("."); } return 0; } This program outputs 8…
Nikolay Kovalenko
  • 1,767
  • 2
  • 13
  • 14
161
votes
6 answers

How to use shared memory with Linux in C

I have a bit of an issue with one of my projects. I have been trying to find a well documented example of using shared memory with fork() but to no success. Basically the scenario is that when the user starts the program, I need to store two values…
bleepzter
  • 9,607
  • 11
  • 41
  • 64
156
votes
15 answers

What is the closest thing Windows has to fork()?

I guess the question says it all. I want to fork on Windows. What is the most similar operation and how do I use it.
rlbond
  • 65,341
  • 56
  • 178
  • 228
112
votes
15 answers

What is the purpose of fork()?

In many programs and man pages of Linux, I have seen code using fork(). Why do we need to use fork() and what is its purpose?
kar
106
votes
4 answers

What is the difference between fork and thread?

Can anyone explain the difference between a fork and a thread?
Pavunkumar
  • 5,147
  • 14
  • 43
  • 69
94
votes
3 answers

printf anomaly after "fork()"

OS: Linux, Language: pure C I'm moving forward in learning C programming in general, and C programming under UNIX in a special case. I detected a strange (for me) behaviour of the printf() function after using a fork() call. Code #include…
pechenie
  • 1,908
  • 2
  • 18
  • 17
91
votes
4 answers

Forking vs Threading

I have used threading before in my applications and know its concepts well, but recently in my operating system lecture I came across fork(). Which is something similar to threading. I google searched difference between them and I came to know…
90
votes
4 answers

How to make parent wait for all child processes to finish?

I'm hoping someone could shed some light on how to make the parent wait for ALL child processes to finish before continuing after the fork. I have cleanup code which I want to run but the child processes need to have returned before this can…
Donatello
  • 947
  • 1
  • 7
  • 6
87
votes
3 answers

Visually what happens to fork() in a For Loop

I have been trying to understand fork() behavior. This time in a for-loop. Observe the following code: #include void main() { int i; for (i=0;i<3;i++) { fork(); // This printf statement is for debugging purposes …
lucidgold
  • 4,432
  • 5
  • 31
  • 51
83
votes
9 answers

Zombie process vs Orphan process

A Zombie is created when a parent process does not use the wait system call after a child dies to read its exit status, and an orphan is child process that is reclaimed by init when the original parent process terminates before the child. In terms…
MarkAWard
  • 1,699
  • 2
  • 16
  • 28
81
votes
2 answers

fork: retry: Resource temporarily unavailable

I tried installing Intel MPI Benchmark on my computer and I got this error: fork: retry: Resource temporarily unavailable Then I received this error again when I ran ls and top command. What is causing this error? Configuration of my machine: Dell…
user1260391
  • 1,237
  • 2
  • 10
  • 6
1
2 3
99 100