简体   繁体   中英

Function with buffer not executing from thread - works on Linux and does not on OS X

I am facing the following problem with a C code: I can't execute a function, calling it from a thread.

On the code, there are three functions: without buffer, with buffer with size MAXBUFFER_OK and with buffer size MAXBUFFER_ERROR. I call this functions from the main function and from the thread function.

#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;
}

In the main function, all the three functions executes without problem. But inside the thread, the function that uses a buffer with size MAXBUFFER_ERROR does not execute.

The buffer size difference from the two functions is only 1 byte. I came to this limit doing several tests.

This problem appears when I execute it on OS X. On Linux, it runs perfectly.

Linux output:

## 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 output:

## 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

Running it on XCode, the error message is

EXC_BAD_ACCESS

Settings:

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

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

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

On OSX, according to the Threading Programming Guide the main thread has an 8MiB stack but secondary threads only have 512KiB stacks. Presumably some of that is used calling the function_print_with_maxbuffer_*() function and the rest is used up by your ~ 512KiB (524288 bytes) buffer[] allocation on the stack.

On my OSX 10.9 system, by trial and error:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM