0

我有一个我无法解释的问题。

检查valgrind内存泄漏,我注意到程序打印的顺序与我只运行程序的可执行文件时得到的顺序不同。

我减少了我的程序,以便编译以显示问题所在。

当我编译并运行以下代码时:

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

int main(void)
{
    printf("I am %d\n", (int)getpid() );

    pid_t pid = fork();
    printf("Fork returned %d\n", (int)pid );

    if ( pid < 0 ){
        perror("Fork Faild\n");
        exit(1);
    }
    if ( pid == 0 ){
        printf("I am the child with pid %d\n", (int)getpid());
        sleep(5);
        printf("Child exiting...\n");
        exit(0);
    }

    printf("I am the parent waiting for child to end\n");
    wait(NULL);
    printf("Parent ending.\n");

    return 0;
}

我得到以下输出:

michi@michael ~ $ ./program 
I am 18320
Fork returned 18321
I am the parent waiting for child to end
Fork returned 0
I am the child with pid 18321
Child exiting...
Parent ending.

但是当我检查它时valgrind,我会以另一个顺序得到输出:

michi@michael ~ $ valgrind --leak-check=full --track-origins=yes ./program
==18361== Memcheck, a memory error detector
==18361== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==18361== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==18361== Command: ./program
==18361== 
I am 18361
Fork returned 18362
Fork returned 0
I am the child with pid 18362
I am the parent waiting for child to end
Child exiting...
==18362== 
==18362== HEAP SUMMARY:
==18362==     in use at exit: 0 bytes in 0 blocks
==18362==   total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated
==18362== 
==18362== All heap blocks were freed -- no leaks are possible
==18362== 
==18362== For counts of detected and suppressed errors, rerun with: -v
==18362== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Parent ending.
==18361== 
==18361== HEAP SUMMARY:
==18361==     in use at exit: 0 bytes in 0 blocks
==18361==   total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated
==18361== 
==18361== All heap blocks were freed -- no leaks are possible
==18361== 
==18361== For counts of detected and suppressed errors, rerun with: -v
==18361== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

我是 fork 的新手,我不明白这对我来说是否有问题。为什么会发生这种情况?

这是在带有 GCC 7 的 Linux Mint 18.2 上编译的。

4

2 回答 2

1

Valgrind“仪器”您的代码以检查泄漏。这意味着添加额外的代码、变量等。 这个答案给出了一个非常快速的概述。

在一组情况下,您的程序可能“通常”以特定顺序执行。但是,如果您更改这些情况(例如,使用 检测valgrind),运行顺序可能会更改。这将取决于许多因素,包括调度程序等。

这是一个非常简化的答案,但本质上,除非您使用自己的调度程序、信号量等来控制您的代码流,否则如果您大幅更改代码/环境,则感知执行顺序可能会发生变化。

于 2017-11-27T23:18:14.110 回答
1

Valgrind 将一些其他代码添加到您的代码中以检查内存泄漏。但这不是导致您的程序输出不同的原因。没有 Valgrind 也可能发生这种情况。即使没有 valgrind,也可能有不同的输出。Fork 将创建另一个进程,从那里您的程序将以任何顺序运行。您的程序不应依赖于执行顺序,您需要保护机制(互斥/信号量)来控制共享资源的访问。

于 2017-11-27T23:48:30.273 回答