产生pthread会对核心转储行为产生巨大变化

时间:2017-03-15 03:04:52

标签: c segmentation-fault pthreads core glibc

下面是一个小程序,它使用mmap来分配" 20GB的虚拟地址空间,然后是segfaults。有两种行为可以独立控制(通过cmd行参数):第一种导致应用程序实际写入每个20GB;第二个导致应用程序在启动时生成pthread。因此,有4种程序可以运行。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <pthread.h>

#define DELAY 3
#define THREAD_DELAY 1
#define TIMES 20
#define GB 1024*1024*1024


void* thread_run(void* name){
    while(1){
        sleep(THREAD_DELAY);
    }
}


void thread_start(char* name) {
    pthread_t thread;
    int ret;

    ret = pthread_create(&thread, NULL, thread_run, (void*)name);

    if(ret){
        printf("failure spawing thread [%d]\n", ret);
        exit(-1);
    }
}


int write_to_memory = 0;
int start_thread = 0;

int main(int argc, char** argv){
    int     i;
    void**  address_array = malloc(TIMES * sizeof(void*));
    int*    ptr = (int*)0;

    printf("PID is %d\n", getpid());

    for(i = 0; i < argc; i++){
        if(strcmp(argv[i], "write_to_memory") == 0) write_to_memory = 1;
        if(strcmp(argv[i], "start_thread") == 0) start_thread = 1;
    }

    printf("write_to_memory = %d\n", write_to_memory);
    printf("start_thread = %d\n", start_thread);


    if(start_thread){
        thread_start("OTHER_THREAD");
    }

    sleep(DELAY);
    for(i = 0; i < TIMES; i++){
        address_array[i] = mmap(NULL, GB, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
        if(address_array[i] == MAP_FAILED){
            printf("mmap failed");
            exit(-2);
        }
    }

    if(write_to_memory){
        sleep(DELAY);
        for(i = 0; i < TIMES; i++){
            memset(address_array[i], 0, GB);
        }
    }

    sleep(DELAY);
    *ptr = 123;
    return *ptr;
}

运行程序全部4种方式,我已经看到了一些非常奇怪的东西 - 对于程序运行的四种方式中的三种,核心转储大小约为20 GB;但是,如果我生成一个线程,但没有写入分配的内存,则核心转储的大小只有11 MB。我无法解释这种行为,并想知道这是否是预期的。

首先,有关环境的一些细节:

[user@machine coredump]$ uname -a
Linux machine.domain 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

[user@machine coredump]$ gcc --version
gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-16)

[user@machine coredump]$ /lib/libc.so.6
GNU C Library stable release version 2.12, by Roland McGrath et al.
        Native POSIX Threads Library by Ulrich Drepper et al

现在输出

_____________________
PID is 16012
write_to_memory = 0
start_thread = 0
Segmentation fault (core dumped)
-rw------- 1 user group 21G Mar 14 22:57 core.16012
_____________________
PID is 16201
write_to_memory = 0
start_thread = 1
Segmentation fault (core dumped)
-rw------- 1 user group 11M Mar 14 22:57 core.16201
_____________________
PID is 16234
write_to_memory = 1
start_thread = 0
Segmentation fault (core dumped)
-rw------- 1 user group 21G Mar 14 22:58 core.16234
_____________________
PID is 16425
write_to_memory = 1
start_thread = 1
Segmentation fault (core dumped)
-rw------- 1 user group 21G Mar 14 22:59 core.16425

这是预期的行为吗?如果是这样,使用pthreads意味着有一个更聪明的&#34;默认的segfault处理程序,它意识到分配的内存没有被触及,因此不需要转储?

0 个答案:

没有答案