×

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

C Program of Fencing the Ground

The college's ground is rectangular. Fencing the ground the management makes the decision to construct a fence around the ground. They planned to wrap a thick rope around the ground...

1 minute read.

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.

Nested Structure in C

In C language, we can create nested Structure (Structure within Structure). There are two ways to define a nested structure. By separate structure By Embedded structure   Separate structure In a separate...

1 minute read.

What is required in each C Program?

Each C program must require one function, i.e., main() function. It is because when we execute the C program, C compiler looks for the main() function, and from here only...

5 minutes read.

If Statement in C

Decision Control Statements We frequently desire one set of instructions to be carried out in one circumstance while another set of instructions to be carried out in a completely different circumstance....

3 minutes read.

Conio.h in C

Header files are the source files with the extension of .h. In c language header files are referred to as the helping files and they contain definitions of various functions...

4 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

Assignment Operator Program in C

An assignment operator is a symbol used to assign a value to a variable in a programming language. The assignment operator is often the equal symbol (=) in programming languages....

12 minutes read.

Bubble sort in C

Bubble sort in C Bubble sort is defined as the algorithm that is used for the sorting mechanism. It follows the technique of replacing the first index with the more minor...

4 minutes read.

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.

Goto and Labels in C

Introduction Goto in C: The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto...

3 minutes read.

Top Array Keywords in C

Sorting array in C Types of array in C Array of structure in C How do you initialize an array in C Array declaration in C Array of strings in C Multidimensional array in C Reverse array...

1 minute read.

Big O Notation in C

Big O Notation in C: In the C programming language, we have so many algorithms and solutions to the problems that exist, which have different aspects and purposes to an...

3 minutes read.

Addition of Matrix in C

The mathematical operation of includingor greater matrices is called the addition of matrices. A matrix is a square series of numbers, symbols, words, letters, and different things. In this article,...

3 minutes read.

Add two numbers using the function in C

Here we will learn how to add two numbers by creating function in C. Let’s learn this with help of example. Code: - #include <stdio.h> int add_two_no(int a, int b); int main(){   int first_num,...

1 minute read.

#include in C

In the C programming language, #include is another way of inferring a standard, or a user defines file into the application or program. The preprocessor of the programming standard generally...

4 minutes read.

Formatted Input and output function in C

Formatted I/O functions display multiple outputs to the user by taking various inputs. All data types like int, float, char and double are supported by the formatted I/O function. In...

4 minutes read.

Null character in C

Introduction The Null character in the C programming language is used to terminate the character strings. In other words, the Null character is used to represent the end of the string...

3 minutes read.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

Volatile in C

Introduction A volatile keyword is a qualifier in C. Qualifiers are nothing but keywords which are used to modify the properties of a variable. Qualifiers are of two types: 1) Const The const type...

3 minutes read.