gpt4 book ai didi

c - sem_post 的段错误

转载 作者:太空宇宙 更新时间:2023-11-04 08:07:42 25 4
gpt4 key购买 nike

我正在尝试使用信号量编写一个小型 C 程序,但我遇到了这个我无法解决的问题。我总是在 sem_post 遇到段错误。这是我的服务器代码中主要方法的相关部分:

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);

这里是 init_semaphores 方法:

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

我没有在这里发布客户端线程的代码,因为它是无关紧要的:段错误在我启动服务器后立即发生,客户端进程没有运行。在段错误之前我的错误消息都没有打印出来。

这是 gdb 在运行程序时说的:

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

因此,如果我没看错的话,段错误发生在 sem_post() 调用时,这必须是对 sem_query 的第一次调用,因为程序会在 sem_wait() 调用时等待。

代码有什么问题?段错误从何而来?

最佳答案

你的信号量初始化不正确:

init_semaphores(sem_query, sem_server, sem_client);

init_semaphores 函数只会修改本地副本(s1s2s3)。因此,init_semaphores() 中的更改不会更改 main() 中的指针,从而使您的信号量设置为 NULL。而是将指针传递给指针以修改它们:

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

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

关于c - sem_post 的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41533104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com