Reputation: 143
I use follows piece of code to check how to use semaphores :
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);
}
Getting SIGSEGV
in the sem_open()
, so what I do wrong?
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
It's looks like that trouble in the shm-directory.c module:
__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;
}
At this line "if (__glibc_unlikely (mountpoint.dir == NULL))"
Take a look to :
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#
Moreover, the first run of the program was successful and the semaphore with the name 'StarLet ..." was created. After second run the code is finished with SIGSEGV at sem_open(). Reboot was not help.
Upvotes: 2
Views: 264
Reputation: 70893
Your system seems to be missing the filesystem for named semphores:
From 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 thanNAME_MAX
characters.)
Use the mount
command to check if shm
is mounted as type tmpfs
.
The list returned by mount
should contain an entry like:
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
And if it isn't mounted, mounting can be done by:
mount -t tmpfs tmpfs /dev/shm -o nosuid,nodev
Upvotes: 2