linux进程间互斥例程,linux下如何实现进程间互斥锁?高手来。

问题:

一个动态库,其中有两个函数都会创建一个文件,是同样的文件,并可对文件进行操作。

这个库可以供多个进程调用,此时是否可以通过某种机制,实现进程A上锁后,进程B就不可以操作文件,当A解锁后进程B才可以继续访问。

我通过sem_init()/sem_wait()/sem_post()可以实现在同一个进程内的多个线程之间的互斥。

但是进程之间就有点想不通了。

int sem_init (sem_t *sem, int pshared, unsigned int value);

man sem_init

sem_init() initialises the unnamed semaphore at the address pointed to by sem.  The value argument speci-

fies the initial value for the semaphore.

The pshared argument indicates whether this semaphore is to be shared between the threads of  a  process,

or between processes.

If  pshared has the value 0, then the semaphore is shared between the threads of a process, and should be

located at some address that is visible to all threads (e.g., a global variable, or a variable  allocated

dynamically on the heap).

If pshared is non-zero, then the semaphore is shared between processes, and should be located in a region

of shared memory (see shm_open(3), mmap(2), and shmget(2)).  (Since a child created by  fork(2)  inherits

its  parent’s memory mappings, it can also access the semaphore.)  Any process that can access the shared

memory region can operate on the semaphore using sem_post(3), sem_wait(3), etc.

Initialising a semaphore that has already been initialised results in undefined behaviour.

其中第四段是重点,我试着去按照提示实现,但是google后,有的说这个暂时还没实现。

疑惑。。。。

求高手帮忙。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值