- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我是自学C,这是一道关于信号量的练习题:
Recall that a counting semaphore S is an integer variable that can only be accused via two standard atomic operations, P (probe) and V (signal), as shown in the figure:
/*The probe or wait operation */
P(S) {
while(S <= 0); // do nothing
S--;
}
/*The signal operation */
V(S) {
S++;
}The value of a counting semaphore can range over an unrestricted domain of integers (that is, the semaphore can hold an arbitrary value) whereas the value of a binary semaphore can only be 0 or 1. Show how counting semaphores can be implemented using only binary semaphores, and ordinary (that is, preemptible) machine instructions and data structures.
Provide the pseudocode for the P and V operations.
我在网上找到了一个相关的答案:
struct semaphore {
int value;
queue L; // l list of processes
}
wait(S) {
if(s.value > 0){
s.value = s.value - 1;
} else {
add this process to S.L;
block;
}
}
signal(S){
if(S.L != EMPTY) {
remove a process P from S.L;
wakeup(P);
} else {
s.value = s.value + 1;
}
}
但老实说,我不知道它要做什么。如果有人可以解释答案,或者用伪代码演示如何回答这个问题,我将不胜感激。
最佳答案
Show how counting semaphores can be implemented using only binary semaphores, and ordinary (that is, preemptible) machine instructions and data structures.
二进制信号量与互斥体非常相似(存在一些显着差异,但出于这个问题的目的,假设它们是等价的),因此您可以使用具有计数器和二进制信号量。二进制信号量用于同步对计数器的访问。
可以通过获取二进制信号量(即等待信号量)、递增计数器然后向二进制信号量发送信号(释放锁)来实现信号发送。
等待是通过反复获取锁(等待二进制信号量),检测计数器是否大于0,释放锁来实现的。如果计数器确实大于 0,则意味着我们排到了一个位置,因此我们将计数器递减并返回。请注意,这必须是原子的:我们不能释放二进制信号量然后递减计数器,因为这会打开一个时间窗口,另一个线程可能会错误地看到相同的东西并同时获得我们在行中的位置.因此,二进制信号量用于自动测试计数器并递减它(如果它大于 0)。
假设二进制信号量的类型为 bin_sem_t
。这是一些说明这一点的代码。数据结构如下:
/* A semaphore implemented with a binary semaphore */
struct semaphore {
bin_sem_t sem; /* Controls concurrent access to the counter */
int counter;
};
信令:
void sem_signal(struct semaphore *semaphore) {
/* We use the binary semaphore to atomically increment the counter */
wait(semaphore->sem);
semaphore->counter++;
signal(semaphore);
}
等待:
void sem_wait(struct semaphore *semaphore) {
int acquired = 0;
/* Here we use the binary semaphore to atomically test and
* decrement the counter
*/
while (!acquired) {
wait(semaphore->sem);
if (semaphore->counter > 0) {
semaphore->counter--;
acquired = 1;
}
signal(semaphore->sem);
}
}
一些重要的注意事项:
sem_signal()
或 sem_wait()
之前初始化二进制信号量> 发生。老实说,我不认为你在网上找到的答案是正确的,它似乎在计数器或进程队列上没有任何形式的同步。因此,它未能解决实现同步原语时的核心问题之一:显然,它们必须是线程安全的!
这看起来也不对:
The value of a counting semaphore can range over an unrestricted domain of integers (that is, the semaphore can hold an arbitrary value) [...]
首先,通用信号量通常不能有负值,它总是大于等于0。其次,计数器的值有一个明显的上限;计算机没有无限的内存。在 Linux 中,信号量可以容纳的最大值在 semaphore.h
中定义为 SEM_VALUE_MAX
。
所以请小心这些在线教程,它们中的大多数要么不准确,要么缺乏细节,要么完全错误。你应该从一本好书中学习。我通常喜欢推荐 UNIX 环境中的高级编程,虽然它不是专门针对线程的,但它涵盖了很深入的同步原语。
关于c - 信号量的标准原子操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31776702/
有没有办法用连词创建原子 if ?也就是说,我可以以某种方式在 C 中自动测试 if(A && B) 吗?如果它在第一个连接处短路,那么没问题,但如果没有短路,则在检查 B 时,A 可能已更改。有什么
我有很多 fork 的过程。子进程做很多事情和另一个系统调用。 当任何子进程从系统调用中获取错误时,它会将错误描述打印到 stderr 并将 SIGUSR1 发送到组长(主要父进程)。 SIGUSR1
阅读 boost::atomic 上的文档和 std::atomic 让我感到困惑的是 atomic 是否接口(interface)应该支持非平凡类型? 也就是说,给定一个只能通过将读/写包含在一个完
我有一个命令,可以将叠加图像放在视频上。 之后,我调整输出大小以适合某些尺寸。 通常一切正常,但有时且仅在某台台式计算机上,当第二次精化开始时,命令返回错误:moov atom not found 让
我最近发现当 LANG 设置为 C.utf8 时,X11 原子 WM_NAME 未在 Swing JFrame 中设置。但为 LANG 的其他值设置。这发生在带有 OpenJDK 11.0.9 的 L
我目前正在使用blackmagic的prorecorder录制视频。我使用 ffmpeg 将视频即时转码为 mp4 视频容器。持续时间未知,因为我正在对 prorecorder 输出到命名管道的 .t
这里真的有人使用 atom 来处理 git 提交消息吗?我想但我遇到了这个问题并且一直坚持使用 git commit -m '....' 。当我尝试使用 atom 时,它会打开 atom,我几乎立即从
考虑: void foo() { std::vector> foo(10); ... } foo 的内容现在有效吗?或者我是否需要显式循环并初始化它们?我检查过 Godbolt,看起来不错,但
在official FAQ我阅读的 Memcached: “发送到 memcached 的所有单独命令都是绝对原子的。” 然而,当涉及到 get_multi 和 set_multi 时,我仍然不清楚。
在测试程序的可扩展性时,我遇到了必须将 memcpy 操作设置为原子操作的情况。我必须将 64 字节的数据从一个位置复制到另一个位置。 我遇到了一种解决方案,即使用旋转变量: struct recor
我对 C++ 原子变量感到困惑。如果我有一个原子 x,我想在一个线程中递增并在另一个线程中读取,我可以执行++x 还是必须执行 x.atomic_fetch_add(1)。在读者线程中,我可以做类似
跟进自 Multiple assignment in one line ,我很想知道这对原子数据类型是如何工作的,特别是 bool 类型的例子。 给定: class foo { std::at
我想创建一个版本控制系统,并且对版本号为 1 的新条目的查询如下所示: ID 和修订号组合起来就是主键。 insert into contentfile (id, name, revision, ac
我在 iOS 项目中有下一个独立的测试片段: /// ... std::atomic_bool ab; ab.store(true); bool expected = false; while (!a
我了解如何使用条件变量(此构造的名称很糟糕,IMO,因为 cv 对象既不是变量也不表示条件)。所以我有一对线程,canonically使用 Boost.Thread 设置为: bool awake =
因此,对于最终项目,我尝试制作一款包含三种不同 meteor 的游戏;铜牌、银牌和金牌。虽然青铜阵列在Setup()中工作正常,但银色和金色 meteor 由于某种未知原因而高速移动。 functio
第一个问题,为什么不在 atomic_compare_exchange_weak 操作的参数中应用后缀求值 (++)?运算前后a的值相同。然而,当在 printf() 中使用时,正如预期的那样,该值会
我正在尝试使用 OpenMP 对已经矢量化的代码进行内部函数并行化,但问题是我使用一个 XMM 寄存器作为外部“变量”,我会在每个循环中递增。现在我正在使用 shared 子句 __m128d xmm
clojure“atom”的文档指出 - "Changes to atoms are always free of race conditions." 但是,竞争条件不仅根据更改定义,而且在不同线程中
我一直在研究原子引用计数的实现。 库之间的大多数操作都非常一致,但我在“减少引用计数”操作中发现了惊人的多样性。 (请注意,通常情况下,shared 和 weak decref 之间的唯一区别是调用了
我是一名优秀的程序员,十分优秀!