简体   繁体   English

使用POSIX计数信号量作为二进制信号量

[英]using POSIX counting semaphore as a binary semaphore

I am trying to use POSIX counting semaphore as a binary semaphore ? 我正在尝试使用POSIX counting semaphore作为binary semaphore

For the purpose, I have written the following code 为此,我编写了以下代码

sem = sem_open(argv[optind], flags, perms, 1); // Initialising semaphore to 1

    while(sem_getvalue(sem) > 0)
    {
    continue;

    }
    sem_post(sem);

Are there any other methods to use a counting semaphore as a binary semaphore? 还有其他方法可以将计数信号量用作二进制信号量吗? Here if the comtext switching occurs immediately after the while lopp is evalauted to false, and yet sem_post hasn't been called, in such a situation wouldn't that leads to race condition? 在这里,如果在将while lopp声明为false之后立即进行了上下文切换,但是尚未调用sem_post,那么在这种情况下是否会导致竞争状态? Is there any other better soultions, for what I am trying to achieve. 对于我想要实现的目标,还有其他更好的方案吗?

I have multiple processes being synchronized with the sempahore. 我有多个进程与Sempahore同步。 I am aware this code does not let gurantee a scenario, when during sem_getvalue even if the sem value becomes zero, even before sem_post in a particular process is called, anotehr process may also call sem_post, leading the value to be 2. How such a scenario can be resolved. 我知道这段代码无法保证情况发生,即使在sem_getvalue期间,即使sem值变为零,甚至在调用特定进程的sem_post之前,anotehr进程也可能会调用sem_post,导致值变为2。方案可以解决。

My problem won't be solved by a mutex, as in my problem, there are processes that is used only for the signal ie sem_post operation, unlike in mutex, where all the processes will have wait and signal continually 我的问题无法通过互斥锁解决,因为在我的问题中,有些进程仅用于信号,即sem_post操作,与互斥锁不同,互斥锁中的所有进程都将持续等待并发出信号

There are some issues with the code you posted. 您发布的代码存在一些问题。

while(sem_getvalue(sem) > 0)

This is referred to as busy waiting, which means the process spins on the semaphore and doesn't relinquish the CPU to the scheduler. 这称为忙等待,这意味着进程在信号量上旋转,并且不将CPU交给调度程序。 Typically, busy waiting is only done in the scenario where the wait should be less than the time for context switching (eg low latency). 通常,仅在等待时间小于上下文切换时间(例如,低延迟)的情况下才执行繁忙等待。

The next issue is that your semantics are inverted. 下一个问题是您的语义被颠倒了。 You decrement and proceed when the semaphore is greater than 0. Additionally, your calls are not atomic which introduces a number of race conditions. 当信号量大于0时,您将递减并继续。此外,您的调用不是原子的,这会引入许多竞争条件。

Effectively, you need mutex semantics since there are only two states (0/locked and 1/unlocked). 实际上,由于只有两种状态(0 /已锁定和1 /已解锁),因此您需要互斥体语义。 For this, you can either guarantee that a sem_post never proceeds a sem_wait , or you can use a file lock. 为此,您可以保证sem_post永远不会进行sem_wait ,也可以使用文件锁。

const char* lock_file = ".lock";

const int fd_lock = open(lock_file, O_CREAT);

flock(fd_lock, LOCK_EX);

// do stuff

flock(fd_lock, LOCK_UN);

// do more stuff

close(fd_lock);    
unlink(lock_file);

The POSIX variant would involve fcntl instead of flock . POSIX变体将使用fcntl而不是flock

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM