gpt4 book ai didi

c - 调用 sem_open() 时的 SIGSEGV 信号

转载 作者:太空狗 更新时间:2023-10-29 12:09:20 25 4
gpt4 key购买 nike

我使用以下代码来检查如何使用信号量:

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 ...”的信号量。第二次运行后代码完成SIGSEGV 在 sem_open()。重启没有帮助。

最佳答案

您的系统似乎缺少命名信号量的文件系统:

来自 man 7 sem_overview :

Accessing named semaphores via the filesystem

On Linux, named semaphores are created in a virtual filesystem, normally mounted under /dev/shm, with names of the form sem.somename. (This is the reason that semaphore names are limited to NAME_MAX-4 rather than NAME_MAX characters.)

使用 mount命令检查 shm 是否安装为 tmpfs 类型。

mount 返回的列表应该包含如下条目:

tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)

如果没有挂载,可以通过以下方式挂载:

mount -t tmpfs tmpfs /dev/shm -o nosuid,nodev

关于c - 调用 sem_open() 时的 SIGSEGV 信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52790951/

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