×

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

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 minutes read.

Structure in C

Why Structure came into the picture? In the actual world, we deal with entities that are collections of objects rather than tiny bits of data like integers, characters, floats, etc. So,...

4 minutes read.

Armstrong Number in C

The Armstrong number is defined as the sum of each of its digits to the power of the number base for the each given number with any given number base....

3 minutes read.

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

Explain the Increment and Decrement Operators in C

In the C programming language, the increment operator (++) and the decrement operator (--) are used to increase or decreasing a variable’s value by 1, respectively. The increment operator is written...

10 minutes read.

Multithreading in C interview questions

Q1. What exactly is a thread? Ans: A thread is a brief sequence of instructions intended to be planned and carried out by the CPU apart from the parent process. Q2. Can...

4 minutes read.

How to delete a file in C

A file is a group of data kept on a secondary device, such as a hard disc. It is typically utilized as a real-world application with a lot of data. These...

3 minutes 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.

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.

Operators in C

An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. It is the combination of constants and variables through expressions. Example: int c = a+b*5 Where,...

5 minutes read.

CRC Program in C

CRC (Cyclic Redundancy Check) is an error-detection algorithm that is used to detect any errors that may have occurred during the transmission or storage of data. The basic idea behind...

4 minutes read.

Range of int in C

In this session we know about the rage of the int in C language with the suitable examples and the programs. Firstly, we can learn the range of int: The int is...

3 minutes read.

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

4 minutes read.

While Loop in C programming examples

Introduction There is always a header file containing all the essential data regarding inputs and outputs of various functions in the C programs. The following statement describes how to use/add header file...

22 minutes read.

Malloc function in C

When the user or programmer does not know how much memory space is needed in the application, it a needs dynamic memory allocation during runtime. Dynamic memory allocation helps us to...

4 minutes read.

Git Hooks

Git Hooks Overview Just like other version control systems, Git has its way to deliver customized scripts whenever some important activity occurs. The hooks act just like the triggers or catalysts...

4 minutes read.

Storage class in C

Storage class in C defines the scope, the visibility, and the lifetime of variables and functions. In other words, storage classes are used to describe the features of variables and...

3 minutes read.