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.

6086 questions
221
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
  • 16,853
  • 9
  • 45
  • 63
219
votes
9 answers

Differences between fork and exec

What are the differences between fork and exec?
Sashi
  • 2,829
  • 6
  • 18
  • 18
216
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
  • 3,957
  • 8
  • 30
  • 35
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,737
  • 2
  • 13
  • 14
136
votes
13 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
  • 59,991
  • 50
  • 166
  • 218
136
votes
5 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
  • 8,463
  • 11
  • 38
  • 62
119
votes
6 answers

Pull request without forking?

Here are steps of code contribution from the topic "How do I contribute to other's code in GitHub?" Fork the project Make one or more well commented and clean commits to the repository. You can make a new branch here if you are modifying more than…
Jasper
  • 4,312
  • 10
  • 31
  • 38
95
votes
4 answers

What is the difference between fork and thread?

Can anyone explain the difference between a fork and a thread?
Pavunkumar
  • 4,717
  • 10
  • 39
  • 67
93
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
82
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,788
  • 1
  • 17
  • 17
81
votes
4 answers

How do I change which GitHub project I forked from?

I forked a project, made some changes, and got a pull request accepted. But now, the project I forked moved to another repository and is a fork of that repository. That is: Original -> MyFork Now: NewOriginal -> Original -> MyFork How would I get…
LanguagesNamedAfterCofee
  • 5,202
  • 6
  • 40
  • 71
80
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,176
  • 4
  • 26
  • 46
80
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
  • 5
76
votes
6 answers

Why does this program print "forked!" 4 times?

Why does this program print “forked!” 4 times? #include #include int main(void) { fork() && (fork() || fork()); printf("forked!\n"); return 0; }
Rawan Lezzeik
  • 891
  • 1
  • 7
  • 6
73
votes
12 answers

Difference between "system" and "exec" in Linux?

What is the difference between system and exec family commands? Especially I want to know which one of them creates child process to work?
Kamil
  • 731
  • 1
  • 5
  • 3
1
2 3
99 100