简体   繁体   English

sem_wait的Coredump

[英]Coredump at sem_wait

I have a rather strange issue here or I am be ignorant of the way it works, but any way I have the program below that creates the semaphore properly and runs to the end for the first time. 我在这里有一个很奇怪的问题,或者我对它的工作方式一无所知,但是无论如何,我下面的程序都会以适当的方式正确创建信号量并首次运行到结尾。 But SEGFaults at sem_wait, if the semaphore already exists. 但是,如果信号灯已存在,则SEGFaults在sem_wait上。 I am running this on 64bit Fedora 17. Does this have to do anything with the error? 我在64位Fedora 17上运行此程序。这是否与错误有关?

#include <stdio.h>          /* printf()                 */
#include <stdlib.h>         /* exit(), malloc(), free() */
#include <sys/types.h>      /* key_t, sem_t, pid_t      */
#include <sys/shm.h>        /* shmat(), IPC_RMID        */
#include <errno.h>          /* errno, ECHILD            */
#include <semaphore.h>      /* sem_open(), sem_destroy(), sem_wait().. */
#include <fcntl.h>          /* O_CREAT, O_EXEC          */

int
main() {


        sem_t *mysem;
        int oflag = O_CREAT | O_EXCL;
        mode_t mode = 0777;
        const char semname[] = "mysem";
        unsigned int value = 1;
        int sts;


        mysem = sem_open(semname, oflag, mode, value);
        //sem_unlink(semname);

        if(mysem == (void *)-1) {
                printf("sem_open() failed");
                exit(1);
        }

        printf("opened a semaphore successful\n");

        if(!sem_wait(mysem)) {
                /*locked */
                printf("worked\n");
        } else {
                printf("error\n");
        }
         return 0;
}

Contents of /dev/shm sem.mysem / dev / shm sem.mysem的内容

Program received signal SIGSEGV, Segmentation fault.
0x000000332980d5f0 in sem_wait () from /lib64/libpthread.so.0
Missing separate debuginfos, use: debuginfo-install glibc-2.15-58.fc17.x86_64
(gdb) where
#0  0x000000332980d5f0 in sem_wait () from /lib64/libpthread.so.0
#1  0x000000000040074a in main () at str2.c:31

Strange issue is that when I delete the semaphore in /dev/shm or uncomment sem_unlink it works every time. 奇怪的问题是,当我删除/ dev / shm中的信号量或取消注释sem_unlink时,它每次都有效。 Am I doing something wrong here or do I need to run a sem_post somewhere? 我在这里做错了吗,还是需要在某个地方运行sem_post?

thanks. 谢谢。

If sem_open fails it returns SEM_FAILED , which on my system (and probably everyone else) is the equivalent to NULL . 如果sem_open失败,它将返回SEM_FAILED ,在我的系统(可能还有其他所有人)上,它等于NULL Check against that instead of -1 . 对照而不是-1检查。

Also, if it fails then print the actual error (use eg perror() or strerror() ). 另外,如果失败,则打印实际错误(例如使用perror()strerror() )。

Segmentation fault usually occurs when an attempt is made to a particular memory that CPU can not physically address. 当尝试尝试CPU无法物理寻址的特定内存时,通常会发生分段错误。 Hardware notifies OS about memory violation, the kernel (OS) in response sends a corrective action against it usually terminating it or causing a dump core. 硬件会通知OS有关内存冲突的信息,作为响应,内核(OS)会发送纠正措施,通常是终止它或导致转储内核。 The most common cause of segmentation is dereferencing NULL pointers . 分段的最常见原因是取消引用NULL指针 Trying that out might help. 尝试一下可能会有所帮助。

Check man open_sem: 检查man open_sem:

"If both O_CREAT and O_EXCL are specified in oflag,then an error is returned if a semaphore with the given name already exists." “如果在oflag中同时指定了O_CREAT和O_EXCL,那么如果已经存在具有给定名称的信号灯,则会返回错误。”

It looks like your semaphore already exists. 您的信号量似乎已经存在。 That is why your code works again after removing from /dev/shm - so it can create it again w/o error. 这就是为什么您的代码从/ dev / shm中删除后可以再次工作的原因-因此可以再次创建它而不会出现错误。

I'm not sure why everyone is checking for -1 return value for error when man says SEM_FAILED. 当人说SEM_FAILED时,我不确定为什么每个人都在检查-1返回值是否有错误。 Probably used to behave like open. 可能表现得像开放。 SEM_FAILED could be defined to be anything, you should not assume its value and use the MACRO. 可以将SEM_FAILED定义为任何值,您不应该假设其值为并使用MACRO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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