c - 缓冲区不从线程执行的函数 - 适用于 Linux 而不适用于 OS X

标签 c linux macos pthreads

我在使用 C 代码时遇到以下问题:我无法执行函数,从线程调用它。

在代码中,一共有三个函数:无缓冲区,有缓冲区,大小为MAXBUFFER_OK,缓冲区大小为MAXBUFFER_ERROR。我从主函数和线程函数中调用这个函数。

#include <stdio.h>
#include <pthread.h>

#define MAXBUFFER_ERROR 522185
#define MAXBUFFER_OK    522184 //MAXBUFFER_ERROR - 1

void function_print_without_buffer(void){
    printf("Function without buffer\n");
}

void function_print_with_maxbuffer_ok(void){
    char buffer[MAXBUFFER_OK] = "hello";
    printf("Function with MAXBUFFER_OK - %s\n", buffer);
}

void function_print_with_maxbuffer_error(void){
    char buffer[MAXBUFFER_ERROR] = "hello";
    printf("Function with MAXBUFFER_ERROR - %s\n", buffer);
}

void * code_thread(void *param){

    printf("## Inside thread ##\n");
    function_print_without_buffer();        //Call function that do not use buffer - ok
    function_print_with_maxbuffer_ok();     //Call function that use buffer with buffersize MAXBUFFER_OK - ok
    function_print_with_maxbuffer_error();  //Call function that use buffer with buffersize MAXBUFFER_ERROR  - ERROR

    return NULL;
}

int main(int argc, const char * argv[]) {

    pthread_t thread;

    printf("## Inside Main ##\n");
    function_print_without_buffer();        //Call function that do not use buffer - ok
    function_print_with_maxbuffer_ok();     //Call function that use buffer with buffersize MAXBUFFER_OK - ok
    function_print_with_maxbuffer_error();  //Call function that use buffer with buffersize MAXBUFFER_ERROR  - ok

    //Start thread
    pthread_create(&thread, NULL, code_thread, NULL);
    pthread_join(thread, NULL);

    return 0;
}

在main函数中,三个函数都执行没有问题。但是在线程内部,使用大小为 MAXBUFFER_ERROR 的缓冲区的函数不会执行。

两个函数的缓冲区大小仅相差 1 个字节。我做了几次测试达到了这个极限。

在OS X上运行时出现这个问题,在Linux上运行完美。

Linux 输出:

## Inside Main ##
Function without buffer
Function with MAXBUFFER_OK - hello
Function with MAXBUFFER_ERROR - hello
## Inside thread ##
Function without buffer
Function with MAXBUFFER_OK - hello
Function with MAXBUFFER_ERROR - hello

OS X 输出:

## Inside Main ##
Function without buffer
Function with MAXBUFFER_OK - hello
Function with MAXBUFFER_ERROR - hello
## Inside thread ##
Function without buffer
Function with MAXBUFFER_OK - hello
Bus error: 10

在XCode上运行,报错信息为

EXC_BAD_ACCESS

设置:

Linux:
   - Centos 7 (inside Docker), X86_64, kernel 4.4.12
   - gcc 4.8.5
Os X
   - Version 10.10.6 (El Capitan)
   - gcc 4.2.1

为什么会这样?为什么可以从 main 而不是从线程执行函数 function_print_with_maxbuffer_error

最佳答案

Why is possible execute the function function_print_with_maxbuffer_error from main and not from the thread?

在 OSX 上,根据 Threading Programming Guide主线程有一个 8MiB 堆栈,但辅助线程只有 512KiB 堆栈。据推测,其中一些用于调用 function_print_with_maxbuffer_*() 函数,其余部分由堆栈上的 ~ 512KiB(524288 字节)buffer[] 分配用完。

在我的 OSX 10.9 系统上,通过反复试验:

#define MAXBUFFER_ERROR 520569
#define MAXBUFFER_OK    520568 // MAXBUFFER_ERROR - 1

关于c - 缓冲区不从线程执行的函数 - 适用于 Linux 而不适用于 OS X,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40075842/

相关文章:

c - 在 C 中嵌入 Ruby 解释器,SIGSEGV

linux - 在 Raspberry Pi 上永远安装 node.js/npm 的问题

c++ - 防止 IOKit 驱动程序在 "kextunload"中关闭,除非某些客户端保持连接

linux - 从 Shell 中的目录连接大量选择性文件

cocoa - 如何在 cocoa 中重新定位 mac 桌面

ios - sortedArrayUsingComparator 的时间复杂度(大 O)是多少? iOS/OSX

c - 在c中使用fftw进行谐波分析

c - 在 Unix/Linux 上用 C 动态确定终端高度的最佳方法是什么?

c - HDF5 属性 unsigned long long 值

linux - grep 使用 X 但不使用 Y