📜  使用fork()在4个过程中进行多次计算

📅  最后修改于: 2021-04-23 07:21:33             🧑  作者: Mango

编写一个程序来创建4个流程:执行各种任务的父流程及其子流程:

  • 父进程计数一个数字的频率
  • 第一个孩子对数组进行排序
  • 第二个孩子在给定数组中找到偶数总数
  • 第三个孩子计算数组中的偶数之和

例子 –

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

说明–在这里,我们使用fork()函数创建4个进程,三个子进程和一个父进程。因此,这里我们使用两个fork()函数创建4个进程n1 = fork()和n2 = fork()

  • 如果n1和n2大于零,则是父进程计算一个数字的频率。
  • 如果n1等于零且n2大于零,则是第一个子进程对给定数组进行排序。
  • 如果n1大于零且n2等于零,则它是第二个子进程,该子进程在数组中查找总偶数。
  • 如果n1和n2都等于零,则它是第三个子代,计算数组中所有元素的总和。

代码 –

// C++ code to demonstrate the calculation
// in parent and its 3 child processes using fork()
#include 
#include 
  
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;
}

输出 –

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