[英]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. 我正在尝试使用信号量编写一个小型C程序,但是遇到无法解决的问题。 I am always getting a segfault at sem_post. 我总是在sem_post遇到段错误。 Here is the relevant part of the main method in my server code: 这是服务器代码中main方法的相关部分:
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: 这是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);
}
}
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. segfault之前不会打印我的任何错误消息。
Here is the what gdb says when running the program: 这是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
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. 因此,如果我正确地阅读了此内容,则segfault会在sem_post()调用中发生,该调用必须是sem_query上的第一个调用,因为程序将在sem_wait()调用中等待。
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
). 函数init_semaphores
将仅修改本地副本( s1
, s2
和s3
)。 So the changes in init_semaphores()
do not change the pointers in main(), leaving your semaphores set to NULL. 因此, init_semaphores()
的更改不会更改main()中的指针,而将信号量设置为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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.