×

fork() in C

Introduction

To create a new function in the system, there is a need for a system call, i.e. fork system call. It is also known as the child process. These child classes run concurrently with the parent process. Here parent process is fork call(). Both processes will follow the instruction of the fork() system call after the creation of the new child process. The newly created child process uses the criteria of the same pc(program counter), same CPU registers, and duplicate open files which operate in the parent process. It returns an integer value without using a parameter. The different values returned by fork() are as follows:

  1. When the creation of the child process is unsuccessful, then it returns a negative value.
  2. When the creation of the child process is successful, then it returns zero.
  3. When it returns a parent value, it produces a positive value.

What is a fork()?

In computer language, it is a method of creation of a process in an operating system like Linux and Unix. With the help of the function, we can copy a new child function from the parent process. When the parent process fails, then the child process also fails.

Example 1

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
 fork();


 printf("javaTpoint\n");
 return 0;
}

Output:

javaTpoint
javaTpoint

Example 2

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void for example()
{
 if (fork() == 0)
 printf("Hello, I am from Child!\n");
 else
 printf("Hello, I am from Parent!\n");
}
int main()
{
 forkexample();
 return 0;
}

Output:

Hello, I am from Parent!
Hello, I am from Child!

Example 3

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
 printf("I am: %d\n", (int) getpid());


 pid_t pid = fork();
 printf("fork returned: %d\n", (int) pid);


 if (pid < 0) {
  perror("Fork failed");
 }
 if (pid == 0) {
  printf("I am the child with pid %d\n", (int) getpid());
 printf("Child process is exiting\n");
 exit(0);
 }
 
 printf("I am the parent waiting for the child process to end\n");
 wait(NULL);
 printf("parent process is exiting\n");
 return(0);
} 

Output:

I am: 3363
fork returned: 3364
I am the parent waiting for the child process to end
fork returned: 0
I am the child with pid 3364
The child process is exiting
the parent process is exiting

Related Topics

Nested Loops in C Programming Examples

A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a “loop inside the...

6 minutes read.

Fibonacci series program in C using Recursion

In this tutorial, we’ll explore how to utilise recursion to build the Fibonacci sequence in C language. What does the term "Fibonacci Series" mean? The following number in a Fibonacci number series is...

2 minutes read.

Stack implementation in C

Stack implementation in C Stack stores the data in a particular order. It is a linear data structure that follows the principle of the Last In First Out (LIFO) technique where...

3 minutes read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.

Conditional Operator in C

In C programming language, the conditional operator (also known as the ternary operator) is a shorthand way of writing an if-else statement. The syntax of conditional operator in C is...

3 minutes read.

getch() function in C

getch() is a pre-define or built-in function present in the conio.h library. It returns the given character immediately without waiting for the enter key to be entered. By using getch()...

3 minutes read.

For Loop in C Programming Examples

The Syntax of the For Loop for (initialization statement; termination condition; modifying (increment/ decrement) statement) {       /* main body of the For loop */     } In for loop, the initialization command...

6 minutes read.

Fibonacci series in C

We have learned about the Fibonacci series in mathematics. For a quick recap, A Fibonacci series is the sequence of numbers following a certain pattern i.e., the next number should...

3 minutes read.

Free() Function in C

Free() function is a built-in function which is define in the stdlib.h header file. If we want to use this function in our program, we must include the stdlib.h header...

4 minutes read.

#undef in C

#undef in C In the C programming language, the #undef directive is used to undefine the macro or any constant, which will be defined by the preprocessor directive #define. The #undef...

3 minutes read.

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.

Pointer arithmetic in C

In the C programming language, a pointer is an address which stores a numeric value. Hence, a developer can perform several arithmetic operations on the same just as one does...

4 minutes read.

Continue in C

C language: C language is a procedure oriented programming language. We can say that it is a platform dependent language. C language is introduced by Dennis Ritchie in the year 1970. We...

2 minutes read.

Else If Ladder in C

What is Else If Ladder: When there are multiple options and a user has to decide from the options, we use Else If Ladder. Basically, it is an extension of if...

3 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

Character Class Tests in C

Introduction The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple...

4 minutes read.

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

3 minutes read.

String Handling functions in C

String :- The String is the collection of characters. Every String ends with the null character, and the String is enclosed in the double quotations .ie, "javaTpoint". If we see any...

5 minutes read.

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

Find reverse of an array in C using functions

Introduction: Here, we describe how to find the reverse array in C using functions. Suppose you have an array with n elements. I need to display the elements present in...

3 minutes read.