How do I stop sem_open() failing with ENOSYS?

后端 未结 5 1527
不思量自难忘°
不思量自难忘° 2020-12-19 18:03

I have two Slackware Linux systems on which the POSIX semaphore sem_open() call fails with errno set to 38. Sample code to reproduce below (the code works fine

相关标签:
5条回答
  • 2020-12-19 18:41

    Older versions of the threading libraries don't support sharing POSIX semaphores between processes. From man sem_init

    The pshared argument indicates whether the semaphore is local to the current process ( pshared is zero) or is to be shared between several processes ( pshared is not zero). LinuxThreads currently does not support process-shared semaphores, thus sem_init always returns with error ENOSYS if pshared is not zero.

    As sem_open() creates named semaphores, it always tries to share them between processes.

    To support sharing anonymous semaphores between processes with sem_init() on Slackware 10

    • upgrade libpthread and (possibly) librt
    • upgrade the kernel

    Additionally, to support sharing named semaphores with sem_open()

    • add a line to /etc/fstab to mount /dev/shm as a tmpfs

      tmpfs /dev/shm tmpfs defaults 0 0

    • run mount /dev/shm or reboot

    0 讨论(0)
  • 2020-12-19 18:43

    I was working with posix message queues i have got the same error mq_open was failed with errono 38 (ENOSYS).

    The work arround is to rebuild the kenel with POSIX MESSGE QUEUE enabled in the kernel configuration.

    This will build the kernel with POSIX message queue support and it worked for me.

    Thanks

    0 讨论(0)
  • 2020-12-19 18:44

    Another way to share a semaphore across processes is to use SystemV semaphores.

    These do work even where shared POSIX semaphores don't (at least on the systems described above.).

    See http://www.linuxdevcenter.com/pub/a/linux/2007/05/24/semaphores-in-linux.html for examples of the two types of semaphore use.

    0 讨论(0)
  • 2020-12-19 18:45

    Is /dev/shm mounted? Older versions of slackware may not have mounted this filesystem at boot. From /etc/fstab:

    tmpfs  /dev/shm  tmpfs  defaults  0   0
    

    Edit: That is probably not the problem after all. I think you may just need to upgrade your kernel or maybe even librt.

    Edit2: I think that for slackware 11, which I think you are using, you'll need a kernel newer than 2.6.13 to use the NPTL threading libraries (libs in /lib/tls) which appear to be required for the sem_open to work.

    Edit3: I managed to get it to work with a slackware 11 box I have by a) mounting /dev/shm and b) setting the environment variable LD_ASSUME_KERNEL to 2.6.13 (any kernel version > 2.6.12 will work). That seems to work even though the kernel is 2.6.11.11, but other things like threads might not.

    0 讨论(0)
  • 2020-12-19 18:56

    The "process shared sema4s don't work" hypothesis makes some sense to me. Not that it helps you, but if you have time and inclination you might want to try the following, to see whether the "process-shared" aspect is what is failing:

    1. create a semaphore using sem_init in unshared memory (for threads). If it works then sema4s work within the process.

    2. repeat experiment in shared memory. This should tell you if they work between processes. Note that you may need to actually try to USE the sema4 to see whether it works between processes.

    0 讨论(0)
提交回复
热议问题