×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

C - fork() Function: Explained with Examples

In this tutorial, we will learn about the fork() function, its uses, and examples.

What is fork() Function in C?

fork() is creates 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. So we can say that fork() is used to create a child process of calling process.

Syntax of fork() Function

Below is the syntax of fork() function in C language:

variable_name = fork();

Using fork() Function

By using fork() function, we can create a exact same copy of the calling process, this function returns the process id of own and this process id is known as child process id and if we get the parent id of this process it would be the same as the parent process id in which fork() is exist.

fork() Example - C program demonstrating use of fork() in Linux

In the following example, we are demonstrating the use of the fork() function:

Consider the example

#include <stdio.h>
#include <unistd.h>

int main()
{
    int id;
    printf("Hello, World!\n");

    id = fork();
    if (id > 0) {
        /*parent process*/
        printf("This is parent section [Process id: %d].\n", getpid());
    }
    else if (id == 0) {
        /*child process*/
        printf("fork created [Process id: %d].\n", getpid());
        printf("fork parent process id: %d.\n", getppid());
    }
    else {
        /*fork creation faile*/
        printf("fork creation failed!!!\n");
    }

    return 0;
}

Output

Hello, World!
This is parent section [Process id: 1252].
fork created [Process id: 1253].
fork parent process id: 1252.

Code Explanation

This is parent section [Process id: 1252].
1252 is the parent process id which is the process id of the main function too; this is the parent section of new created fork.

fork created [Process id: 1253].
This message will print under the fork section and assigns a new id to the newly created child process.

fork parent process id: 1252.
This message will also print under the fork section, it shows parent process id of the newly created child process which is equivalent to parent process’s id (main process id). Hence we can say newly created child process is the child process of main which is known as parent process.

Another Example using fork()

In this example, we will print natural numbers from 1 to 10 using for loop and we will create a fork() - you will see numbers will be printed twice, because of fork(), it will create duplicate copy of the process.

#include <stdio.h>
#include <unistd.h>

int main()
{
    int id, i;

    printf("Start of main...\n");

    id = fork();
    if (id > 0) {
        /*parent process*/
        printf("Parent section...\n");
    }
    else if (id == 0) {
        /*child process*/
        printf("\nfork created...\n");
    }
    else {
        /*fork creation faile*/
        printf("\nfork creation failed!!!\n");
    }

    printf("Printing the numbers from 1 to 10\n");
    for (i = 1; i <= 10; i++)
        printf("%d ", i);
    printf("\n");
    printf("End of the main function...\n");

    return 0;
}

Output

Start of main...
Parent section... 
Printing the numbers from 1 to 10 
1 2 3 4 5 6 7 8 9 10
End of the main function... 

fork created... 
Printing the numbers from 1 to 10 
1 2 3 4 5 6 7 8 9 10
End of the main function... 

C Advance Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.