我怎样才能得到多次调用sem_open在C工作? [英] How can I get multiple calls to sem_open working in C?

查看:1281
本文介绍了我怎样才能得到多次调用sem_open在C工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了很多困难得到信号灯在C.一个基于Linux的系统

I'm experiencing a lot of difficulty getting Semaphores to work on a Linux based system in C.

我的应用程序的过程是这样的:

The process of my application is such:


  1. 应用程序启动

  2. 申请叉成父/子

  3. 每个进程使用 sem_open 有一个共同的名字,打开信号灯。

  1. Application starts
  2. Application forks into child/parent
  3. Each process uses sem_open with a common name to open the semaphore.

如果我分叉之前创建的信号,它工作正常。然而,要求prevent我这样做。当我尝试调用 sem_open 第二次,我得到错误权限被拒绝(通过错误号)。

If I create the semaphore before forking, it works fine. However, requirements prevent me from doing so. When I try to call sem_open for the second time, I get a "Permission Denied" error (via errno).

是否有可能做到这一点以任何方式?或者是有没有办法在一个进程打开信号灯,并使用了共享内存机制,与子进程共享呢?

Is it possible to do this in any way? Or is there any way to open the semaphore in one process and used a shared memory mechanism to share it with the child process?

推荐答案

您使用sem_open 4个参数或2个参数的版本?

Are you using the 4 parameter or 2 parameter version of sem_open?

请务必使用4参数的版本和使用模式,将允许其他进程打开的信号。假设所有的过程都是由同一个用户拥有的0600模式( S_IRUSR | S_IWUSR )就足够了。

Make sure to use the 4 parameter version and use a mode that will allow other processes to open the semaphore. Assuming all the processes are owned by the same user, a mode of 0600 (S_IRUSR | S_IWUSR) will be sufficient.

您可能还需要验证您的umask没有掩盖任何必要的权限。

You may also want to verify that you umask is not masking out any of the necessary permissions.

这篇关于我怎样才能得到多次调用sem_open在C工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆