Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
The fork() Function: What is it?
3.
Key Takeaways about the fork() Function
4.
Frequently Asked Questions
4.1.
What if the fork() function fails to create a new process?
4.2.
Do parent and child processes share the same memory space?
4.3.
How can I distinguish between a parent process and a child process in code?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

C Fork() Function

Author Gunjan Batra
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

In the realm of operating systems and concurrent programming, the ability to create new processes is crucial. In C programming on a Unix-based system, this can be accomplished using the fork() function. The fork() function creates a new process by duplicating the existing process.

C fork() function

 

 This article will unravel the complexities of the fork() function, explain how it operates with examples, and address common queries related to it.

The fork() Function: What is it?

The fork() function is a system call in Unix-based systems used for creating a new process, termed a child process. The new process is an exact copy of the calling process, the parent process, except for a few values, including its Process ID (PID), Parent Process ID (PPID), and more. The fork() function doesn't take any arguments, but it returns an integer value, the PID of the child process to the parent, and 0 to the child process.

To use the fork() function, we need to include the unistd.h library:

#include <unistd.h>

//An Example of fork() Function

//Consider the following C program:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>


void forkexample() {
    // child process because return value zero
    if (fork() == 0)
        printf("Hello from Child!\n");


    // parent process because return value non-zero.
    else
        printf("Hello from Parent!\n");
}


int main() {
    forkexample();
    return 0;
}

Output

Output

In the above program, the forkexample() function contains a fork() system call. If the fork() function call creates a child process successfully, it returns 0 in the child process and the child process ID in the parent process. Depending on the returned value, either the parent's or the child's message gets printed.

Also see, Floyd's Triangle in C

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Key Takeaways about the fork() Function

New Process Creation: The fork() function creates a new process by duplicating the existing one.

  • Returned Value: The fork() function returns 0 to the newly created child process. For the parent process, it returns the PID of the newly created child process.
     
  • Resource Sharing: The child process gets a separate memory location. Any change in a variable of the parent process doesn’t affect the child process and vice versa.
     
  • Concurrency: After a new child process is created, both the parent and the child will execute the next instruction following the fork() function.
    Also read, Bit stuffing program in c

Frequently Asked Questions

What if the fork() function fails to create a new process?

If the fork() function fails, it returns -1 in the parent process.

Do parent and child processes share the same memory space?

No, the child process gets a separate memory space. Any changes in the parent process do not affect the child process and vice versa.

How can I distinguish between a parent process and a child process in code?

The return value from fork() helps distinguish. It returns 0 to the child process and the child's PID to the parent process.

Conclusion

The fork() function in C is a potent tool for creating concurrent programs by spawning new child processes. By understanding how to use it effectively, programmers can leverage the power of multiple processes running concurrently for a wide range of applications, from web servers to scientific computations. As always, a solid grasp of fundamental concepts like these provides the foundation for more advanced programming topics.

Check out these useful blogs on - 


Check out the following problems - 

 

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available, interview puzzles, take a look at the interview experiences, and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow

Happy Reading!!‍

Live masterclass