【发布时间】:2018-10-13 08:14:52
【问题描述】:
我使用以下代码来检查如何使用信号量:
char sema_name [NAME_MAX];
sem_t *sema_hnd = NULL;
const mode_t semprot = 0777; /*S_IRWXU | S_IRWXG ;*/
int cid = 79;
int main()
{
sprintf(sema_name, "/StarLet-TV-CID=%d", cid);
if ( !(sema_hnd = sem_open(sema_name, O_CREAT, semprot, cid)) )
printf("errno=%d", errno);
printf("sema_hnd=%#x", sema_hnd);
}
在sem_open() 中获取SIGSEGV,那么我做错了什么?
root@SysMan-Ubuntu:/dev/shm# uname -a
Linux SysMan-Ubuntu 4.15.0-34-generic #37-Ubuntu SMP Mon Aug 27 15:21:48 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
在 shm-directory.c 模块中看起来像那个麻烦:
__shm_directory (size_t *len)
{
/* Determine where the shmfs is mounted. */
__libc_once (once, where_is_shmfs);
/* If we don't know the mount points there is nothing we can do. Ever. */
if (__glibc_unlikely (mountpoint.dir == NULL))
{
__set_errno (ENOSYS);
return NULL;
}
*len = mountpoint.dirlen;
return mountpoint.dir;
}
在这一行“if (__glibc_unlikely (mountpoint.dir == NULL))”
看看:
root@SysMan-Ubuntu:/home/sysman# cd /dev/shm
root@SysMan-Ubuntu:/dev/shm# ll
total 0
drwxrwxrwt 2 root root 40 окт 13 19:17 ./
drwxr-xr-x 18 root root 3860 окт 13 19:18 ../
root@SysMan-Ubuntu:/dev/shm# ls >zz.log
root@SysMan-Ubuntu:/dev/shm# ll
total 4
drwxrwxrwt 2 root root 60 окт 13 19:19 ./
drwxr-xr-x 18 root root 3860 окт 13 19:18 ../
-rw-r--r-- 1 root root 7 окт 13 19:19 zz.log
root@SysMan-Ubuntu:/dev/shm# cat zz.log
zz.log
root@SysMan-Ubuntu:/dev/shm#
root@SysMan-Ubuntu:/dev/shm# df
Filesystem 1K-blocks Used Available Use% Mounted on
udev 990112 0 990112 0% /dev
tmpfs 204076 21896 182180 11% /run
/dev/sda1 48250196 34281344 11865744 75% /
tmpfs 1020376 4 1020372 1% /dev/shm
tmpfs 5120 0 5120 0% /run/lock
tmpfs 1020376 0 1020376 0% /sys/fs/cgroup
Downloads 976759804 802679056 174080748 83% /media/sf_Downloads
Works 976759804 802679056 174080748 83% /media/sf_Works
tmpfs 204072 12 204060 1% /run/user/122
tmpfs 204072 20 204052 1% /run/user/1000
/dev/sr0 56618 56618 0 100% /media/sysman/VBox_GAs_5.2.18
tmpfs 204072 0 204072 0% /run/user/0
root@SysMan-Ubuntu:/dev/shm#
root@SysMan-Ubuntu:/dev/shm# mount | grep shm
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
root@SysMan-Ubuntu:/dev/shm#
此外,程序的第一次运行成功,并创建了名为“StarLet ...”的信号量。第二次运行后,代码完成sem_open() 处的 SIGSEGV。重启没有帮助。
【问题讨论】:
-
你没有得到任何输出?
-
以防万一:这个
printf("sema_hnd=%#x", sema_hnd)调用未定义的行为,应该是printf("sema_hnd = 0x%p", (void*) sema_hnd)。 -
您是否使用选项
-pthread编译此代码? -
程序在 sema_open() 调用时崩溃。当然,-pthread -rt 键已包含在内。
-
sem_open 调用:SHM_GET_NAME (EINVAL, SEM_FAILED, SEM_SHM_PREFIX);