📜  链进程与在 C 中使用 fork()函数的进程风扇

📅  最后修改于: 2022-05-13 01:54:26.070000             🧑  作者: Mango

链进程与在 C 中使用 fork()函数的进程风扇

Fork 系统调用 fork 系统调用用于创建一个新进程,该进程称为子进程,它与进行 fork() 调用的进程(父进程)并发运行。创建新的子进程后,两个进程都将执行 fork() 系统调用之后的下一条指令。子进程使用相同的程序计数器、相同的 CPU 寄存器和父进程中使用的相同打开的文件。

要创建进程扇形或进程链,首先插入头文件“unistd.h”以使用 fork()函数创建进程。使用 exit() 方法包括“stdlib.h” ,程序中有 3 种方法可以使用 exit 语句:  

  • exit(0):正常终止
  • exit(1):对于异常终止
  • exit():可以正常也可以异常。

流程链

假设有三个进程,第一个进程是父进程,它正在创建子进程,那么第二个进程创建另一个进程(第三个进程),然后第一个进程成为第二个进程的父进程,第二个进程成为第三个进程的父进程。因此,获得了一个流程链,这称为流程链。

流程链

3 个过程的链

下面是使用 fork() 创建进程链的实现:

C
// C program to create a chain of process
#include 
#include 
#include 
#include 
#include 
  
// Driver Code
int main()
{
    int pid;
  
    // Iterate in the range [0, 2]
    for (int i = 0; i < 3; i++) {
        pid = fork();
  
        if (pid > 0) {
            // Print the parent process
            printf("Parent process ID is %d\n",
                   getpid());
            break;
        }
        else {
            // Print the child process
            printf("Child process ID is %d\n",
                   getpid());
        }
    }
  
    return 0;
}


C
// C program to create a fan of processes
  
#include 
#include 
#include 
#include 
  
// Driver Code
int main()
{
    // Iterate in the range [0, 2]
    for (int i = 0; i < 3; i++) {
        if (fork() == 0) {
            // getpid gives process id
            // getppid gives parent process id
            printf("child pid %d from the"
                   " parent pid %d\n",
                   getpid(), getppid());
  
            // Set Normal termination of
            // the program
            exit(0);
        }
    }
  
    for (int i = 0; i < 3; i++)
        wait(NULL);
}


输出

Parent process ID is 1359
Child process ID is 1360
Parent process ID is 1360
Child process ID is 1360
Child process ID is 1361
Parent process ID is 1361
Child process ID is 1360
Child process ID is 1361
Child process ID is 1362

说明:在上面的程序中, getpid() 用于获取进程ID。或者,也可以使用getppid()来获取父进程 ID。 每次运行程序,输出都不一样,因为不是每次都使用相同的进程 id 由操作系统分配给进程,但PID(进程ID) 总是得到 程序运行时按非递减顺序排列。

过程风扇

假设有3个进程,第一个进程是父进程,它正在创建两个 子进程。如果两个进程具有相同的父进程,那么它就是进程的粉丝。

3个进程的风扇

下面是使用 fork() 创建进程粉丝的实现:

C

// C program to create a fan of processes
  
#include 
#include 
#include 
#include 
  
// Driver Code
int main()
{
    // Iterate in the range [0, 2]
    for (int i = 0; i < 3; i++) {
        if (fork() == 0) {
            // getpid gives process id
            // getppid gives parent process id
            printf("child pid %d from the"
                   " parent pid %d\n",
                   getpid(), getppid());
  
            // Set Normal termination of
            // the program
            exit(0);
        }
    }
  
    for (int i = 0; i < 3; i++)
        wait(NULL);
}
输出
child pid 29831 from parent pid 29830
child pid 29832 from parent pid 29830
child pid 29833 from parent pid 29830

说明:在上面的程序中,每次都是同一个进程ID 不是每次都分配给操作系统处理。这意味着在这种情况下,每次程序运行时输出也不同,但创建的子进程将具有相同的父进程 ID。

想要从精选的视频和练习题中学习,请查看C 基础到高级C 基础课程