理解函数内部的 fork()

问题描述 投票:0回答:1

嗨,我下面有一个函数,其中函数内有一个 fork() 。我可以简单地执行 if else 操作,但从递增行为中,我无法理解如何手动获取输出。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int Call_a_Function(int x, int y)
{
    x++;
    x = fork(); /* #4 */
    if (x)
    {
        x = y;
    }
    else if (fork()) /* #5 */
    {
        x--;
        y += x;
    }
    return x + y;
}
int main()
{
    int x, y = 5, z;
    x = fork(); /* #1 */
    if (x)
    {
        x = y;
        z = Call_a_Function(x, y++);
    }
    else
    {
        (void)fork(); /* #2 */
        z = x++;
        y += z;
    }
    if (y > z)
    {
        (void)fork(); /* #3 */
    }
    printf("%d \n", x + y + z);
    return 0;
}

代码的输出是

21 6 6 6 16 16 6 14 14

但是我无法通过手动解决它并制作它的树来获得任何这些输出。如果有人能够帮助我理解这段代码,请。

c linux fork
1个回答
0
投票

正如我在评论中指出的,为了调试多个进程,我建议:

  1. 捕获变量中每个 fork() 调用的结果,并且
  2. 将其打印在
    stderr
    上,并在末尾换行。

事实上,通常最好每次打印 PID、PPID 和 fork() 的返回值,以及标识输出来自哪一行代码的标记字符串。确保使用换行符完成输出行(但不要在换行符前面放置空格)。另外,在关键点打印整数 (x, y, z) - 在一系列赋值之后、在函数入口处、在返回之前等。

这是我想出的检测代码:

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>

static int Call_a_Function(int x, int y)
{
    fprintf(stderr, "PID = %5d, PPID = %5d: -->> %s(x = %d, y = %d)\n", getpid(), getppid(), __func__, x, y);
    int pid;
    x = fork(); /* #4 */
    if (x != 0)
    {
        x = y;
        fprintf(stderr, "PID = %5d, PPID = %5d: --1- %s(x = %d, y = %d)\n", getpid(), getppid(), __func__, x, y);
    }
    else if ((pid = fork()) != 0) /* #5 */
    {
        x--;
        y += x;
        fprintf(stderr, "PID = %5d, PPID = %5d: --2- %s(pid = %d, x = %d, y = %d)\n", getpid(), getppid(), __func__, pid, x, y);
    }
    fprintf(stderr, "PID = %5d, PPID = %5d: <<-- %s(return= %d)\n", getpid(), getppid(), __func__, x + y);
    return x + y;
}

int main(void)
{
    int x, y = 5, z;
    fprintf(stderr, "PID = %5d, PPID = %5d\n", getpid(), getppid());
    x = fork(); /* #1 */
    if (x)
    {
        fprintf(stderr, "PID = %5d, PPID = %5d: --1- x = %d, y = %d\n", getpid(), getppid(), x, y);
        x = y;
        z = Call_a_Function(x, y++);
        fprintf(stderr, "PID = %5d, PPID = %5d: --2- x = %d, y = %d, z = %d\n", getpid(), getppid(), x, y, z);
    }
    else
    {
        int pid = fork(); /* #2 */
        fprintf(stderr, "PID = %5d, PPID = %5d: --3- (pid = %d)\n", getpid(), getppid(), pid);
        z = x++;
        y += z;
        fprintf(stderr, "PID = %5d, PPID = %5d: --4- x = %d, y = %d, z = %d\n", getpid(), getppid(), x, y, z);
    }
    if (y > z)
    {
        int pid = fork(); /* #3 */
        fprintf(stderr, "PID = %5d, PPID = %5d: --4- (pid = %d)\n", getpid(), getppid(), pid);
    }
    fprintf(stderr, "PID = %5d, PPID = %5d: --4- x = %d, y = %d, z = %d; sum = %d\n", getpid(), getppid(), x, y, z, x + y + z);
    printf("%d\n", x + y + z);

    int corpse;
    int status;
    while ((corpse = wait(&status)) > 0)
        fprintf(stderr, "PID = %5d, PPID = %5d: child %5d exited 0x%.4X\n", getpid(), getppid(), corpse, status);

    return 0;
}

当我运行该代码时,输出之一是:

PID = 52290, PPID = 52280
PID = 52290, PPID = 52280: --1- x = 52291, y = 5
PID = 52290, PPID = 52280: -->> Call_a_Function(x = 5, y = 5)
PID = 52290, PPID = 52280: --1- Call_a_Function(x = 5, y = 5)
PID = 52290, PPID = 52280: <<-- Call_a_Function(return= 10)
PID = 52290, PPID = 52280: --2- x = 5, y = 6, z = 10
PID = 52290, PPID = 52280: --4- x = 5, y = 6, z = 10; sum = 21
21
PID = 52291, PPID = 52290: --3- (pid = 52293)
PID = 52291, PPID = 52290: --4- x = 1, y = 5, z = 0
PID = 52292, PPID = 52290: --2- Call_a_Function(pid = 52294, x = -1, y = 4)
PID = 52292, PPID = 52290: <<-- Call_a_Function(return= 3)
PID = 52292, PPID = 52290: --2- x = 5, y = 6, z = 3
PID = 52291, PPID = 52290: --4- (pid = 52295)
PID = 52291, PPID = 52290: --4- x = 1, y = 5, z = 0; sum = 6
6
PID = 52293, PPID = 52291: --3- (pid = 0)
PID = 52293, PPID = 52291: --4- x = 1, y = 5, z = 0
PID = 52292, PPID = 52290: --4- (pid = 52296)
PID = 52292, PPID = 52290: --4- x = 5, y = 6, z = 3; sum = 14
PID = 52294, PPID = 52292: <<-- Call_a_Function(return= 5)
PID = 52294, PPID = 52292: --2- x = 5, y = 6, z = 5
14
PID = 52295, PPID = 52291: --4- (pid = 0)
PID = 52295, PPID = 52291: --4- x = 1, y = 5, z = 0; sum = 6
6
PID = 52296, PPID = 52292: --4- (pid = 0)
PID = 52296, PPID = 52292: --4- x = 5, y = 6, z = 3; sum = 14
PID = 52293, PPID = 52291: --4- (pid = 52297)
14
PID = 52293, PPID = 52291: --4- x = 1, y = 5, z = 0; sum = 6
6
PID = 52297, PPID = 52293: --4- (pid = 0)
PID = 52297, PPID = 52293: --4- x = 1, y = 5, z = 0; sum = 6
6
PID = 52294, PPID = 52292: --4- (pid = 52298)
PID = 52294, PPID = 52292: --4- x = 5, y = 6, z = 5; sum = 16
PID = 52292, PPID = 52290: child 52296 exited 0x0000
PID = 52291, PPID = 52290: child 52295 exited 0x0000
16
PID = 52298, PPID = 52294: --4- (pid = 0)
PID = 52298, PPID = 52294: --4- x = 5, y = 6, z = 5; sum = 16
PID = 52293, PPID = 52291: child 52297 exited 0x0000
16
PID = 52291, PPID = 52290: child 52293 exited 0x0000
PID = 52294, PPID = 52292: child 52298 exited 0x0000
PID = 52290, PPID = 52280: child 52291 exited 0x0000
PID = 52292, PPID = 52290: child 52294 exited 0x0000
PID = 52290, PPID = 52280: child 52292 exited 0x0000
PID = 52299, PPID = 52280
PID = 52299, PPID = 52280: --1- x = 52300, y = 5
PID = 52299, PPID = 52280: -->> Call_a_Function(x = 5, y = 5)
PID = 52299, PPID = 52280: --1- Call_a_Function(x = 5, y = 5)
PID = 52299, PPID = 52280: <<-- Call_a_Function(return= 10)
PID = 52299, PPID = 52280: --2- x = 5, y = 6, z = 10
PID = 52299, PPID = 52280: --4- x = 5, y = 6, z = 10; sum = 21
21
PID = 52300, PPID = 52299: --3- (pid = 52302)
PID = 52300, PPID = 52299: --4- x = 1, y = 5, z = 0
PID = 52301, PPID = 52299: --2- Call_a_Function(pid = 52303, x = -1, y = 4)
PID = 52301, PPID = 52299: <<-- Call_a_Function(return= 3)
PID = 52301, PPID = 52299: --2- x = 5, y = 6, z = 3
PID = 52302, PPID = 52300: --3- (pid = 0)
PID = 52302, PPID = 52300: --4- x = 1, y = 5, z = 0
PID = 52300, PPID = 52299: --4- (pid = 52304)
PID = 52300, PPID = 52299: --4- x = 1, y = 5, z = 0; sum = 6
6
PID = 52303, PPID = 52301: <<-- Call_a_Function(return= 5)
PID = 52303, PPID = 52301: --2- x = 5, y = 6, z = 5
PID = 52301, PPID = 52299: --4- (pid = 52305)
PID = 52301, PPID = 52299: --4- x = 5, y = 6, z = 3; sum = 14
PID = 52302, PPID = 52300: --4- (pid = 52306)
PID = 52302, PPID = 52300: --4- x = 1, y = 5, z = 0; sum = 6
14
6
PID = 52303, PPID = 52301: --4- (pid = 52307)
PID = 52303, PPID = 52301: --4- x = 5, y = 6, z = 5; sum = 16
PID = 52304, PPID = 52300: --4- (pid = 0)
PID = 52304, PPID = 52300: --4- x = 1, y = 5, z = 0; sum = 6
6
16
PID = 52305, PPID = 52301: --4- (pid = 0)
PID = 52306, PPID = 52302: --4- (pid = 0)
PID = 52305, PPID = 52301: --4- x = 5, y = 6, z = 3; sum = 14
PID = 52306, PPID = 52302: --4- x = 1, y = 5, z = 0; sum = 6
14
6
PID = 52307, PPID = 52303: --4- (pid = 0)
PID = 52307, PPID = 52303: --4- x = 5, y = 6, z = 5; sum = 16
16
PID = 52300, PPID = 52299: child 52304 exited 0x0000
PID = 52301, PPID = 52299: child 52305 exited 0x0000
PID = 52302, PPID = 52300: child 52306 exited 0x0000
PID = 52303, PPID = 52301: child 52307 exited 0x0000
PID = 52300, PPID = 52299: child 52302 exited 0x0000
PID = 52301, PPID = 52299: child 52303 exited 0x0000
PID = 52299, PPID = 52280: child 52300 exited 0x0000
PID = 52299, PPID = 52280: child 52301 exited 0x0000

有了这些信息,您就可以推断出发生了什么。总共有5个过程。

© www.soinside.com 2019 - 2024. All rights reserved.