简体   繁体   中英

Segfault at sem_post

I am trying to write a small C program using semaphores, but I am having this issue I can't resolve. I am always getting a segfault at sem_post. Here is the relevant part of the main method in my server code:

sem_t * sem_query = NULL;
sem_t * sem_server = NULL;
sem_t * sem_client = NULL;

//prepare semaphores
init_semaphores(sem_query, sem_server, sem_client);

//prepare the shared memory
struct shm_struct * shared = init_shm();
int exit_code = 0;

while (1) {

    if (sem_post(sem_query) == -1) {
        perror("Error posting the semaphore!");
        exit_code = 1;
        break;
    }

    if (sem_wait(sem_server) == -1) {
        if (errno == EINTR) {
            printf("Received interrupting signal! Quitting now...");
            break;
        } else {
            perror("Error while waiting on semaphore!");
            exit_code = 1;
            break;
        }
    }

    //calculate the response and write it to shm
    calculate_and_write_response(shared, procs);

    if (sem_post(sem_client) == -1) {
        perror("Error posting the semaphore!");
        exit_code = 1;
        break;
    }

     if (sem_wait(sem_server) == -1) {
        if (errno == EINTR) {
            printf("Received interrupting signal! Quitting now...");
            break;
        } else {
            perror("Error while waiting on semaphore!");
            exit_code = 1;
            break;
        }
    } 

}    
free_shm(shared);

free_semaphores(sem_query, sem_server, sem_client);

exit(exit_code);

And here is the init_semaphores method:

static void init_semaphores(sem_t * s1, sem_t * s2, sem_t * s3) {

    s1 = sem_open(SEM_A_NAME, O_CREAT | O_EXCL, 0600, 0);
    s2 = sem_open(SEM_B_NAME, O_CREAT | O_EXCL, 0600, 0);
    s3 = sem_open(SEM_C_NAME, O_CREAT | O_EXCL, 0600, 0);

    if (s1 == SEM_FAILED || s2 == SEM_FAILED || s3 == SEM_FAILED) {
        perror("Could not open semaphores!");
        free_semaphores(s1, s2, s3);
        exit(1);
    }
}

I am not posting the code for the client thread here because it is irrelevant: The segfault occures immediately after I start the server, without the client process running. None of my error messages is printed before the segfault.

Here is the what gdb says when running the program:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff79c6b44 in sem_post@@GLIBC_2.2.5 () from /lib64/libpthread.so.0
Missing separate debuginfos, use: debuginfo-install glibc-2.17-157.el7_3.1.x86_64

So, if I am reading this correctly, the segfault occures at a sem_post() call, which has to be the first call on sem_query, because the program would wait at the sem_wait() call.

What is wrong with the code? Where does the segfault come from?

Your initialization of semaphores isn't right:

init_semaphores(sem_query, sem_server, sem_client);

The function init_semaphores would only modify the local copies ( s1 , s2 and s3 ). So the changes in init_semaphores() do not change the pointers in main(), leaving your semaphores set to NULL. Instead pass pointer to pointers to modify them:

//prepare semaphores
init_semaphores(&sem_query, &sem_server, &sem_client);

and

static void init_semaphores(sem_t **s1, sem_t **s2, sem_t **s3) {

    *s1 = sem_open(SEM_A_NAME, O_CREAT | O_EXCL, 0600, 0);
    *s2 = sem_open(SEM_B_NAME, O_CREAT | O_EXCL, 0600, 0);
    *s3 = sem_open(SEM_C_NAME, O_CREAT | O_EXCL, 0600, 0);

    if (*s1 == SEM_FAILED || *s2 == SEM_FAILED || *s3 == SEM_FAILED) {
        perror("Could not open semaphores!");
        free_semaphores(*s1, *s2, *s3);
        exit(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