×

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

Sum of all elements between k1’th and k2’th Smallest Elements

In this tutorial, we will look at how to determine sum of all given elements between two given indexes’ smallest elements. Assuming an array of integers and two numbers, k1...

2 minutes read.

Iostream in C++

Using Iostream in C++, we can perform input and output operation capabilities. This represents input and output, and the stream is used to carry out this capability. A stream is...

4 minutes read.

How to Handle Divide by Zero Exception in C++

If you are a programmer or interested in coding then it is obvious that you face some illogical test cases. Suppose, you have written one program that calculates the factorial...

6 minutes read.

Loops in C++

A loop statement in most programming languages allows us to execute a statement or a collection of statements numerous times. Control structures of programming languages vary, allowing for more complex...

6 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.

How to Declare Unordered Sets in C++

The implementation of an unordered set using a hash table ensures that the insertion is always randomised by hashing the keys into hash table indices. When we define keys of...

4 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.

Structure of C++ Program

Many people believe that C++, an object-oriented programming (OOP) language, is the finest language for developing demanding applications. A superset of the C language is C++. Java, a closely comparable...

4 minutes read.

Reverse String Word-Wise in C++

What is a reversed String? Reversing the words of a sentence is called reversed string by words. The difference between the reverse a string and the reverse a string word-wise is...

4 minutes read.

Factorial Program in C++

C++ Factorial Program: The product of all positive descending integers is the factorial of n. n! denotes the factorial of n. For instance: 5! = 5*4*3*2*1=120 4! = 4*3*2*1=24 In Combinations and Permutations, the...

4 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.

Storage Classes in C

Storage Classes in C Storage Classes are used to define the variable and function property. These functionalities include basically the scope, accessibility, and lifetime that help us detect the existence of...

4 minutes read.

Program that produces different results in C and C++

Introduction: There are many such programs that compile run both in C and C++ but give different outcomes when compiled by the C and C++ compilers. There are a variety of such...

6 minutes read.

C++ Overloading

C++ Overloading is a condition when two or more members have the same name with different parameter type or a different number of parameter. C++ overloading is two types: Function...

1 minute read.

C++ Math Functions

C++ Math Functions: Like other programming languages, C++ offers plenty of mathematical functions needed for various purposes. These functions are defined mainly in the math library in C++. Let us...

4 minutes read.

C++ Program For FCFS (First Come First Serve)

The most basic scheduling technique is FCFS, often known as "FIFO (First In, First Out)". In this procedure, the first one is utilized and executed first, while the second one...

4 minutes read.

Initialization of Data Members

In this tutorial, we'll look at how to initialise static member variables in C++. Static members, such as functions or variables, can be added to C++ classes. After declaring the...

1 minute read.

Array of sets in C++

Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. A collection of data objects stored in a continuous way is...

6 minutes read.

C++ Friend function

A friend function has the right to access all private and protected members of a class although it is defined outside that class' scope. Syntax class className{     ......     friend retyrn_type function_Name(argument);     .......   }   return_type function_Name(argument){     ......   } C++ friend function Example #include <iostream>   using namespace std;   class Length   {       private:           int meter;       public:           Length(): meter(5) { }           friend int addMethod(Length); //friend function declaration   };   int addMethod(Length l) // friend function definition   {       l.meter += 10; //accessing private data from non-member function       return l.meter;   }   int main()   {       Length L;       int totallength;       totallength=addMethod(L);       cout<<"Length: "<< totallength;       return 0;   } Output: Length: 15   C++ friend function...

1 minute 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.