📜  使用fork()创建多个进程

📅  最后修改于: 2021-05-30 12:18:22             🧑  作者: Mango

先决条件– fork,getpid()和getppid()的介绍
问题陈述–编写一个程序,使用fork()函数在每个进程找到其ID的情况下创建一个有三个孩子的父母。例如 :

Output :parent
28808 28809 
 my id is 28807 
First child
0 28810 
my id is 28808  
Second child
28808 0 
my id is 28809  
third child
0 0 
 

说明–在这里,我们使用fork()函数创建了四个进程,一个父进程和三个子进程。

  • 现有的进程可以通过调用fork()函数来创建一个新进程。
  • fork()创建的新进程称为子进程。
  • 我们在这里使用getpid()来获取进程ID
  • 在fork()中创建的总过程为= 2 ^ fork()的数量

注–在某些情况下,不必先执行子进程,也不必先分配父进程的CPU,任何进程都可以在某个时间分配CPU。此外,进程ID在不同的执行过程中可能会有所不同。

代码1 –使用getpid()

// C++ program to demonstrate creating processes using fork()
#include 
#include 
  
int main()
{
    // Creating first child
    int n1 = fork();
  
    // Creating second child. First child
    // also executes this line and creates
    // grandchild.
    int n2 = fork();
  
    if (n1 > 0 && n2 > 0) {
        printf("parent\n");
        printf("%d %d \n", n1, n2);
        printf(" my id is %d \n", getpid());
    }
    else if (n1 == 0 && n2 > 0)
    {
        printf("First child\n");
        printf("%d %d \n", n1, n2);
        printf("my id is %d  \n", getpid());
    }
    else if (n1 > 0 && n2 == 0)
    {
        printf("Second child\n");
        printf("%d %d \n", n1, n2);
        printf("my id is %d  \n", getpid());
    }
    else {
        printf("third child\n");
        printf("%d %d \n", n1, n2);
        printf(" my id is %d \n", getpid());
    }
    return 0;
}

输出 –

parent
28808 28809 
 my id is 28807 
First child
0 28810 
my id is 28808  
Second child
28808 0 
my id is 28809  
third child
0 0 

代码2 –使用getppid()

// C++ program to demonstrate creating processes using fork()
#include 
#include 
  
int main()
{
    // Creating first child
    int n1 = fork();
  
    // Creating second child. First child
    // also executes this line and creates
    // grandchild.
    int n2 = fork();
  
    if (n1 > 0 && n2 > 0)
    {
        printf("parent\n");
        printf("%d %d \n", n1, n2);
        printf(" my id is %d \n", getpid());
        printf(" my parentid is %d \n", getppid());
    }
    else if (n1 == 0 && n2 > 0)
    {
        printf("First child\n");
        printf("%d %d \n", n1, n2);
        printf("my id is %d  \n", getpid());
        printf(" my parentid is %d \n", getppid());
    }
    else if (n1 > 0 && n2 == 0)
    {
        printf("second child\n");
        printf("%d %d \n", n1, n2);
        printf("my id is %d  \n", getpid());
        printf(" my parentid is %d \n", getppid());
    }
    else {
        printf("third child\n");
        printf("%d %d \n", n1, n2);
        printf(" my id is %d \n", getpid());
        printf(" my parentid is %d \n", getppid());
    }
  
    return 0;
}

输出 –

parent
5219 5220 
 my id is 5217 
 my parentid is 5215 
First child
0 5221 
my id is 5219  
 my parentid is 1 
second child
5219 0 
my id is 5220  
my parentid is 1 
third child
0 0 
 my id is 5221 
 my parentid is 1 
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”