fork() in C program

fork() c++
create process tree using fork()
fork child process example
create multiple child processes using fork
parent and child process program c
c fork, exec
fork practice questions
wait() in c

This is a question from recent GATE entrance exam. A process executes the code

fork();  
fork();  
fork();  

The total number of child processes created is

(A) 3. (B) 4. (C) 7. (D) 8.

My answer was (A) 3.

My view is that after each fork(), 1 child process will be created and execution of parent will continue normally.

Unreliable answer (without any explanation) from coaching institutes were (C) 7.

I think they are treating that each fork will create a child process and a new parent process. And they are counting all the parent process as well. [I am not allowed to post image but my friend explained in a diagram, a tree with each left node forking down in two nodes. Therefore 4 parent node in left and 3 child node in right.] But the Question clearly mentions child process only. And also I don't think that parent process is newly created in forking.

Can someone explain some forking fundamentals, and a proper solution to above question please.

P.S. If programming language make any difference in concept of forking, then as per syllabus, this should be either C or C++ program.

fork() results in both the original process and one child to start from that point in the code. Therefore you have this picture:

Creating multiple process using fork(), fork() in C. CServer Side ProgrammingProgramming. In this section we will see what is the  In this section we will see what is the fork system call in C. This fork system call is used to create a new process. This newly created process is known as child process. The current process which is creating another child process is called the parent process. A child process uses the same program counter, CPU register, same files that are

fork() in C, OK, now get back to the code. Before the line with fork(), this C program is perfectly normal: 1 line is executing at a time, there's only one process for this program  fork() function explanation and examples in Linux C programming Language fork() is used to create new process by duplicating the current calling process , and newly created process is known as child process and the current calling process is known as parent process .

Try this:

printf("initial pid: %d\n", (int)getpid());
fork();
fork();
fork();
printf("final pid: %d\n", (int)getpid());

Your First C Program Using Fork System Call – Linux Hint, fork() is used to create new process by duplicating the current calling process, and newly created process is known as child process and the current calling  In a C or C++ program, fork() can be used to create a new process, known as a child process. This child is initially a copy of the the parent, but can be used to run a different branch of the program or even execute a completely different program. After forking, child and parent processes run in parallel.

After each call to fork there are two processes, the parent and the child. And both of those processes continue executing immediately after the fork. Some of the resulting processes are (after all of the forking) both parents and children. The ones that are only children are the leaves of the process tree. The one (the original one) that is only a parent is the root of the tree. The ones that are both parents and children are the branches.

fork() function explanation and examples in Linux C programming , Click here to download this file fork-01.c. #include <stdio.h> Suppose the above program executes up to the point of the call to fork() (marked in red color):​  When fork is called, a new process is created, which is identical in virtually every way as the original process, except for the return value of the fork function. The newly created process is called the child process, and hence the process that spawned it is referred to as the parent process.

At the end of the third fork call, there will be a total of 8 processes. Out of these 8 processes, there will be 7 child processes and 1 parent process.

There are two important points to note while solving these type of problems:

  1. A fork() call returns twice, once in the parent and once in the child process. If fork is successful, it returns with a value of 0 in child process and with non-zero value(pid of child) in parent process.

  2. Both the child and parent process starts executing right after the fork call.

I have explained the above problem with the help of a diagram and also a video. I hope you find it useful.

fork example

The fork() System Call, Spawning a new process inside a single program is the duty of the fork() function. That function is declared in the unistd.h header file. It returns  In this program, both processes print lines that indicate (1) whether the line is printed by the child or by the parent process, and (2) the value of variable i. For simplicity, printf() is used. When the main program executes fork(), an identical copy of its

A Fork in Your Code, The purpose of fork() is to create a new process, which becomes the child B and C will never reach this code */ pid3=fork(); /* D */ if(pid3==0) { /* This is child D  C program to demonstrate fork() and pipe() pipe() System call; Pi(π) in C++ with Examples; Speed up Code executions with help of Pragma in C/C++; Role of SemiColon in various Programming Languages

Fork() function in C, fork() returns the process identifier (pid) of the child process in the parent, and the source code for fork.c was executed and the time that getpid.c was executed. What are the differences between fork and pipe in C? Can we use them in C++? Can we use them in C++? I need to know this is because I want to implement a program in C++ which can access live video input, convert its format and write it to a file.

Understanding usage of fork() System call in C program : Advanced , Fork is a system call and you shouldnt think of it as a normal C function. When a fork () occurs you effectively create two new processes with their own address space.Variable that are initialized before the fork () call store the same values in both the address space. However values modified within the address space of either of the process

Comments
  • The child processes created by the first process go on to fork() themselves. You should count those too.
  • No, forking is an OS concept - the programming language won't matter.
  • This shows why multi-choice questions are diabolical for demonstrating understanding. If you had to write an answer that explained how you arrived at the answer, you could get credit for understanding the concepts even if you came to a different conclusion from the 'official answer'. For example, are the children of the child processes to be counted? It changes the answer to be given.
  • Actually, a really good question would be: Justify each of the answers (a) 3, (b) 4, (c) 7, and (d) 8.
  • @Rup so as an OS concept, would forking on windows and unix be different??
  • Nice job on the diagram.
  • @JonathanLeffler Hehe thanks, and nice job on your answer, after all it is more detailed!
  • @eznme"-from that point in code-" does this means that after second fork the child and parent both will continue executing from next line which is the third fork() statement?
  • @AbhinavKulshreshtha hmm, think of it this way: fork(); fork(); fork(); there is one process in the beginning, there are 2 processes executing the first semicolon, there are 4 processes executing the second semicolon, there are 8 processes executing the third semicolon. (then subtract 1 because you only want child-processes)
  • Official answer key from iit came out few days ago. The Correct answer is 7. unfortunately, they didn't gave complete solution set, just answers.
  • i got linker error for _getpid and _fork. This is the code i used #include <stdio.h> #include <conio.h> void main() { printf("Initial pid: %d\n", (int)getpid()); fork(); fork(); fork(); getch(); }
  • i am using turbo c++ 3.1 under dosbox emulation, in windows 7
  • Neither DOS nor Windows have fork(). You cannot try that snippet in those Operating Systems. I have no idea how to do the same in DOS; you can try CreateProcess() in Windows (which is not exactly like fork()).
  • No, DOS can only run one process at once so it has no equivalent. If you want to try this your best bet is probably to get VMware player or similar and set up a Linux VM, or find a unix machine somewhere on the internet that you can get a shell account on.