×

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

Two dimension array

C++ Two dimension (2D) Array Two dimension (2D) array is an array of arrays. It is represented in the form of row and column. The elements of 2D array are accessed through the...

2 minutes read.

Palindrome Number Program in C++

A palindrome number is one that is the same when it is reversed. Palindrome numbers include 22, 33, 44, 55, 66, 77, 88, and 99. Algorithm for Palindrome Numbers Get the user's...

4 minutes read.

How to build a program in C++

Building a program is all about creating the program and executing it successfully. There are some steps  precisely, which must be followed to make the program. Step 1: Get an IDE...

4 minutes read.

Palindrome using For loop in C++

A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++ also allows...

6 minutes read.

Difference between C and C++

What do you mean by C? C is a machine-independent structure or procedural oriented computer language that is widely utilized in a variety of applications. C is a fundamental programming language...

4 minutes read.

Snake Code in C++

Snake is a popular game that can be played on almost any device and runs on any operating system. In this game, snakes can move in any direction, including left,...

4 minutes read.

Difference between Two Sets in C++

The distinction between the two sets is made up of the components that are present in the first set but absent from the second set. The function consistently duplicates the...

3 minutes read.

C++ Inheritance

What do you mean by Inheritance ? The ability to define new classes based on existing classes in order to reuse and organise code is referred to as inheritance. Single inheritance...

7 minutes read.

C++ Data Abstraction

Object-oriented programming (OOP) provides a number of characteristics that enable programmers to design programmes based on a variety of ideas, reducing errors and increasing program flexibility. The abstraction of data...

7 minutes read.

The Stock Span Problem

It is necessary to determine the span of a stock's price throughout all n days in order to solve the stock span problem, which involves a set of n daily...

5 minutes read.

C++ String

A string is a collection of characters. C++ programming language supports both C string as well as standard C++ library string. In C++, string is an object of std::string class. C Style String The C style...

5 minutes read.

How to concatenate two strings in C++

In the C++ programming language, the concatenation of two or even more strings is covered in this section. The term "string concatenation" refers to a collection of characters that join two...

4 minutes read.

Splitting a string in C++

Any programming language must have the ability to work with string data. For programming needs, we sometimes need to separate string data. Many computer languages provide a split() method that...

4 minutes read.

C++ Forward Iterators

Iterators : Iterators serve as a link between algorithms and STL containers, allowing the data inside the container to be modified. They let you to iterate through the container, access and...

3 minutes read.

Diamond Pattern 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...

5 minutes read.

Handling multiple clients on the Server with multithreading using Socket Programming in C or C++

To understand this guide completely, the reader is assumed to be familiar with the foundations of server and client models and socket programming. If one wants to create any scalable...

7 minutes read.

C++ Continue in Do-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 start...

4 minutes read.

C++ Prime number program

In this lesson, you'll learn how to verify whether a given number is a prime number or not in C++, and you'll obtain code to do it. What is the definition...

3 minutes read.

User-defined literals in C++

Introduction to User-Defined Literals: User-defined literals, introduced in C++11, are a way to extend the C++ language to allow users to define their literal suffixes and the corresponding behavior. These suffixes...

7 minutes read.

How is multiset implemented in C++

Similar to sets, multisets are an associative container type where several items may share the same values. Associative containers implement instantly searchable sorted data structures with O(log n) complexity. In a multiset,...

5 minutes read.