Open In App

Multiple calculations in 4 processes using fork()

Last Updated : 06 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Write a program to create 4 processes: parent process and its child process which perform various tasks :

  • Parent process count the frequency of a number
  • 1st child sort the array
  • 2nd child find total even number(s) in a given array
  • 3rd child calculate the sum of even numbers in an array

Example -

Input :  
2, 4, 6, 7, 9, 0, 1, 5, 8, 3
Output :
Parent process :
the key to be searched is 7
the frequency of 7 is 1
1st child process :
the sorted array is
0 1 2 3 4 5 6 7 8 9
2nd child process :
Total even no are: 5
3rd child process :
the sum is :45

Explanation -

Here, we had used fork() function to create 4 processes three child and one parent process. So, here we use two fork() function which create 4 process n1=fork() and n2 = fork()

  • if n1 and n2 is greater than zero then it is parent process which counts the frequency of a number.
  • if n1 is equal to zero and n2 is greater than zero then it is 1st child process which sorts the given array.
  • if n1 is greater than zero and n2 is equal to zero than it is 2nd child process which finds the total even numbers in the array.
  • if n1 and n2 both equal to zero then it is 3rd child calculates the sum of all elements in an array.

Code -

CPP
// C++ code to demonstrate the calculation
// in parent and its 3 child processes using fork()
#include <iostream>
#include <unistd.h>

using namespace std;

int main()
{

    int a[10] = { 2, 4, 6, 7, 9, 0, 1, 5, 8, 3 };
    int n1, n2, i, j, key, c, temp;
    n1 = fork();
    n2 = fork();

    // if n1 is greater than zero
    // and n2 is greater than zero
    // then parent process executes
    if (n1 > 0 && n2 > 0) {

        int c = 0;
        cout << "Parent process :" << endl;

        // key to be searched is 7
        key = 7;
        cout << "the key to be searched is " << key << endl;

        for (i = 0; i < 10; i++) {

            if (a[i] == key)
                // frequency of key
                c++;
        }

        cout << "the frequency of " << key << " is " << c << endl;
    }

    // else if n1 is zero
    // and n2 is greater than zero
    // then 1st child process executes
    else if (n1 == 0 && n2 > 0) {

        cout << "1st child process :" << endl;

        for (i = 0; i < 10; i++) {

            for (j = 0; j < 9; j++) {

                if (a[j] > a[j + 1]) {

                    temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }

        cout << "the sorted array is" << endl;

        for (i = 0; i < 10; i++) {

            cout << a[i] << " ";
        }

        cout << endl;
    }

    // else if n1 is greater than zero
    // and n2 is zero
    // then 2nd child process executes
    else if (n1 > 0 && n2 == 0) {

        int f = 0;
        cout << "2nd child process :" << endl;

        for (i = 0; i < 10; i++) {

            // counting total even numbers
            if (a[i] % 2 == 0) {

                f++;
            }
        }

        cout << " Total even no are: " << f << " ";
        cout << endl;
    }

    // else if n1 is zero
    // and n2 is zero
    // then 3rd child process executes
    else if (n1 == 0 && n2 == 0) {

        cout << "3rd child process :" << endl;

        int sum = 0;
        // summing all given keys
        for (i = 0; i < 10; i++) {

            sum = sum + a[i];
        }

        cout << "the sum is :" << sum << endl;
    }

    return 0;
}
Java
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        // The array to be used in the calculations
        int[] a = {2, 4, 6, 7, 9, 0, 1, 5, 8, 3};

        // Parent process
        System.out.println("Parent process :");

        // Key to be searched is 7
        int key = 7;
        System.out.println("The key to be searched is " + key);

        // Frequency of key
        int c = 0;
        for (int num : a) {
            if (num == key) {
                c++;
            }
        }
        System.out.println("The frequency of " + key + " is " + c);

        // 1st child process
        System.out.println("1st child process :");

        // Sorting the array
        Arrays.sort(a);
        System.out.println("The sorted array is " + Arrays.toString(a));

        // 2nd child process
        System.out.println("2nd child process :");

        // Counting total even numbers
        int f = 0;
        for (int num : a) {
            if (num % 2 == 0) {
                f++;
            }
        }
        System.out.println("Total even numbers are: " + f);

        // 3rd child process
        System.out.println("3rd child process :");

        // Summing all given keys
        int sum = 0;
        for (int num : a) {
            sum += num;
        }
        System.out.println("The sum is : " + sum);
    }
}
C#
using System;
using System.Threading.Tasks;

class Program
{
    static void Main()
    {
        int[] a = { 2, 4, 6, 7, 9, 0, 1, 5, 8, 3 };

        // Fork process
        int n1 = 0;
        int n2 = 0;

        // Creating tasks for child processes
        Task firstChild = Task.Run(() =>
        {
            Console.WriteLine("1st child process :");

            // Sorting the array
            Array.Sort(a);

            Console.WriteLine("The sorted array is " + string.Join(", ", a));
        });

        Task secondChild = Task.Run(() =>
        {
            Console.WriteLine("2nd child process :");

            // Counting total even numbers
            int f = 0;
            foreach (int num in a)
            {
                if (num % 2 == 0)
                    f++;
            }

            Console.WriteLine("Total even numbers are: " + f);
        });

        // Parent process
        Console.WriteLine("Parent process :");

        // Key to be searched
        int key = 7;
        Console.WriteLine("The key to be searched is " + key);

        // Frequency of key
        int c = 0;
        foreach (int num in a)
        {
            if (num == key)
                c++;
        }

        Console.WriteLine("The frequency of " + key + " is " + c);

        Task thirdChild = Task.Run(() =>
        {
            Console.WriteLine("3rd child process :");

            // Summing all given keys
            int sum = 0;
            foreach (int num in a)
            {
                sum += num;
            }

            Console.WriteLine("The sum is : " + sum);
        });

        // Wait for all tasks to complete before exiting
        Task.WaitAll(firstChild, secondChild, thirdChild);
    }
}
JavaScript
// The array to be used in the calculations
let a = [2, 4, 6, 7, 9, 0, 1, 5, 8, 3];

// Parent process
console.log("Parent process :");

// Key to be searched is 7
let key = 7;
console.log("The key to be searched is " + key);

// Frequency of key
let c = 0;
for (let num of a) {
    if (num === key) {
        c++;
    }
}
console.log("The frequency of " + key + " is " + c);

// 1st child process
console.log("1st child process :");

// Sorting the array
a.sort((x, y) => x - y);
console.log("The sorted array is " + JSON.stringify(a));

// 2nd child process
console.log("2nd child process :");

// Counting total even numbers
let f = 0;
for (let num of a) {
    if (num % 2 === 0) {
        f++;
    }
}
console.log("Total even numbers are: " + f);

// 3rd child process
console.log("3rd child process :");

// Summing all given keys
let sum = 0;
for (let num of a) {
    sum += num;
}
console.log("The sum is : " + sum);
Python3
# Importing the required libraries
import os

# The array to be used in the calculations
a = [2, 4, 6, 7, 9, 0, 1, 5, 8, 3]

# Creating the first child process
n1 = os.fork()

# Creating the second child process
n2 = os.fork()

# If n1 is greater than zero and n2 is greater than zero
# then parent process executes
if n1 > 0 and n2 > 0:
    print("Parent process :")

    # Key to be searched is 7
    key = 7
    print("The key to be searched is", key)

    # Frequency of key
    c = a.count(key)

    print("The frequency of", key, "is", c)

# Else if n1 is zero and n2 is greater than zero
# then 1st child process executes
elif n1 == 0 and n2 > 0:
    print("1st child process :")

    # Sorting the array
    a.sort()

    print("The sorted array is", a)

# Else if n1 is greater than zero and n2 is zero
# then 2nd child process executes
elif n1 > 0 and n2 == 0:
    print("2nd child process :")

    # Counting total even numbers
    f = len([num for num in a if num % 2 == 0])

    print("Total even numbers are:", f)

# Else if n1 is zero and n2 is zero
# then 3rd child process executes
elif n1 == 0 and n2 == 0:
    print("3rd child process :")

    # Summing all given keys
    sum = sum(a)

    print("The sum is :", sum)

Output -

Parent process :
the key to be searched is 7
the frequency of 7 is 1
1st child process :
the sorted array is
0 1 2 3 4 5 6 7 8 9
2nd child process :
Total even no are: 5
3rd child process :
the sum is :45

Practice Tags :

Similar Reads