×

How to create the Processes with Fork in C++

 In this article, we are going to explain the different methods that help us to create processes with a fork(). There are two methods to do so. These methods are as follows:

  1. By using getpid().
  2. By using getppid().

Now, we will discuss them individually.

1. By Using getpid()

In this method, a unique id is generated when a new process is started. This unique id is called a process id. With the help of this getpid() function, the program returns the process id. It is also used to generate the unique and temporary file name.

Syntax:

pid_t getpid();

Example:

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


int main()
{


int n1 = fork();


int n2 = fork();


if (n1 > 0 && n2 > 0) {
 printf("it is parent class\n");
 printf("%d %d \n", n1, n2);
 printf(" my process id is %d \n", getpid());
}
else if (n1 == 0 && n2 > 0)
{
 printf("it is First child class\n");
 printf("%d %d \n", n1, n2);
 printf("my process id is %d \n", getpid());
}
else if (n1 > 0 && n2 == 0)
{
 printf("It is Second child class\n");
 printf("%d %d \n", n1, n2);
 printf("my process id is %d \n", getpid());
}
else {
 printf("It is third child class\n");
 printf("%d %d \n", n1, n2);
 printf(" my process id is %d \n", getpid());
}
return 0;
}

Output:

How to Create the Processes with Fork in C++

2. By Using getppid()

This method returns the process id of the parent class. 

Syntax:

pid_t getppid();

Example:

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


int main()
{
int n1 = fork();


int n2 = fork();


if (n1 > 0 && n2 > 0)
{
 printf(" It is the parent class\n");
 printf("%d %d \n", n1, n2);
 printf(" my process id is %d \n", getpid());
 printf(" my parentid is %d \n", getppid());
}
else if (n1 == 0 && n2 > 0)
{
 printf("It is the First child class\n");
 printf("%d %d \n", n1, n2);
 printf("my process id is %d \n", getpid());
 printf(" my parentid is %d \n", getppid());
}
else if (n1 > 0 && n2 == 0)
{
 printf("It is the second child class\n");
 printf("%d %d \n", n1, n2);
 printf("my process id is %d \n", getpid());
 printf(" my parentid is %d \n", getppid());
}
else {
 printf("It is third child class\n");
 printf("%d %d \n", n1, n2);
 printf(" my process id is %d \n", getpid());
 printf(" my parentid is %d \n", getppid());
}


return 0;
}

Output:

How to Create the Processes with Fork in C++

Related Topics

Continue in C++ While loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the...

4 minutes read.

C++ Fibonacci Series

What is a Fibonacci series? A Fibonacci series or sequence is a very popular programming paradigm. The next element occurring in the N terms series is determined by the sum of...

2 minutes read.

Char Array to String in C++

Regardless of the programming language you use, data structure is critical to the success of your project. Although each programming language has its own collection of data structures, C++ contains a...

4 minutes read.

C++ Do while loop

In this article, we will discuss the C++ Do-While loop with its syntax, working, key features, algorithm, and examples. Do-While Loop: The do-while loop constitutes a specific style of looping construct in...

5 minutes read.

Structure Vs Class in C++

The structure in C++ is similar to that of a class, with a few exceptions. Both the structure and the stage, the most important thing is safety. The property is...

4 minutes read.

Abstract class in C++

In this article, you will get exposure to an abstract class in C++. We will discuss this topic using some practical examples too. To understand the abstract classes, you should...

6 minutes read.

How to initialize a dynamic array in C++

Regular arrays or static arrays have a predetermined size or fixed size. Change in the size of regular arrays is not possible. The memory size for static arrays determines at compile...

4 minutes read.

Floating Point Operations and Associativity in C, C++ and Java

In this tutorial, we are going to compare Floating-point operations and the concept of associativity. Before we apply the concept of associativity in the floating-point operations in all three programming...

3 minutes read.

Default arguments in C++

Arguments in a function are defined as the values supplied when the function is called. The source is the values supplied, and the destination is the receiving function. Let us...

3 minutes read.

C++ Algorithms

There are plenty of programming paradigms that are closely associated with the implementations of code and simulate them into a proper functional one. This is done with the help of...

5 minutes read.

Similarities between C++ and Java

People who are preparing for software engineering roles must have come across either one of these languages because as all the companies do not accept python for hiring a fresher...

3 minutes read.

Constructor Overloading

The program contains more than one constructor in a class with the same name, and different types of arguments are called constructor overloading. Calling of constructor depends on the number and types...

2 minutes read.

Converting string into integer in C++

When programming in C++, we'll frequently need to change one data type to another. When we use C++ to create apps, we must transform data from one type to another. When...

7 minutes read.

C++ Exception Handling

Exception is an unexpected problem that occurs at program run time. This problem might include condition such as division by zero, running out of memory space, array out of bonds, etc....

2 minutes read.

How to call a void function in C++

Generally, any function has two types: 1. Void function: It doesn't return any value. 2. Non-void function: It returns some value. Program to call a void function in C++ #include <iostream> using namespace std;  void check() {  ...

2 minutes read.

Difference between exit() and _Exit() in C++

Before understanding the difference between the exit() and _Exit(), one must know about exit() and _Exit() functions. The exit() function in C/C++ The exit() method in the C language kills the calling...

3 minutes read.

Naming Convention in C++

The first and most fundamental step a programmer takes to produce clean code is to name a file or a variable. This naming must be acceptable so that it serves...

5 minutes read.

Armstrong Number using While Loop in C++

What is while Loop? A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the...

4 minutes read.

rand() and srand() in C / C++

In this tutorial, we'll explore the syntax, usage, and examples of the C++ STL functions rand() and srand(). What exactly is rand()? The C++ STL's built-in rand() function is defined in the...

3 minutes read.

C++ Queue

C++ queue: Queue in C++ is also a container adapter with the functionality of a queue. Queue is just the opposite of the stack in C++ because stack works on...

4 minutes read.