由买买提看人间百态

topics

全部话题 - 话题: semaphores
1 2 3 4 5 6 7 8 9 下页 末页 (共9页)
w****x
发帖数: 2483
1
来自主题: JobHunting版 - 哪里有mutex和semaphore例子?
mutex和semaphore实现读写锁
class ReadWriteMutex
{
public:
ReadWriteMutex(int maxReaders = 32)
: semaphore(maxReaders)
{
}

void lockRead() { semaphore++; }
void unlockRead() { semaphore--; }
void lockWrite() {
QMutexLocker locker(&mutex);
for (int i = 0; i < maxReaders(); ++i)
semaphore++;
}
void unlockWrite() { semaphore -= semaphore.total(); }
int maxReaders() const ... 阅读全帖
z****e
发帖数: 2024
2
来自主题: Programming版 - multithread 的semaphore问题
请问如下这个问题是怎么解决的?
看不懂。用mutex好像不行。也不知道semaphore怎么工作的。
Suppose we have the following code to use class Foo. We do not know how the threads will be scheduled in the OS.
question:
Can you design a mechanism to make sure that all the methods will be executed in sequence?
Foo f;
f.A(.....);
f.B(.....);
f.C(.....);
f.A(.....);
f.B(.....);
f.C(.....);
solution:
Semaphore s_a(0);
Semaphore s_b(0);
Semaphore s_c(1);
A {
s_c.acqure(1);
/***/
s_a.release(1);
}
B {
s_a.acqure(1);
/****/
s_b.release(1);
}
C {
s
r****o
发帖数: 1950
3
一直觉得很奇怪,semaphore不是进程间的同步机制吗?
为啥老见人说semaphore是进程间的通信机制呢? 光靠semaphore能实现连个进程间的通
信吗?
l*******y
发帖数: 1498
4
mutex是专门用来同步的, semaphonre既可以用来同步也可以用来通信。因为mutex只
能由拥有mutex的那个进程释放,而semaphonre可以由非拥有的进程释放。这也是mutex
和 binary semaphonre的一个主要区别吧。
Consider I/O examples, we pick up a binary semaphore to wait for some kind
of I/O, when the I/O comes, task picks up the byte, the task then releases
the same semaphore. This cannot be done with a mutex.
r****o
发帖数: 1950
5
【 以下文字转载自 Programming 讨论区 】
发信人: roufoo (五经勤向窗前读), 信区: Programming
标 题: 为什么说semaphore是一种进程间的通信方式。
发信站: BBS 未名空间站 (Sat Oct 9 02:02:50 2010, 美东)
我一直没有搞明白的就是:
semaphor是一种进程间的同步方式,为什么人们也把它列为进程间的通信方式之一呢?
l*******y
发帖数: 1498
6
通信不一定是要传数据吧。一个进程在等一个semaphore的值到0 (然后他就可以干某
件事),另外一个进程修改了这个semaphore的值到0,就是告诉那个进程可以做他想做
的事了。这个不就是通信么?
o****d
发帖数: 2835
7
首先 lock是locking mechanism,而semaphore是signal mechanism
lock只能被同一个线程lock和unlock,而semaphore可以不同线程通过信号协同作用
另外,lock分spinlock和blocking lock(mutex)
他们的区别ls也已经说了
spinlock比较适合 等待时间比较短的情况,还有kernel的lock也是用spinlock;
而blocking lock适合等待时间比较长的情况,因为context switch比较耗时
还有spinlock+blocking lock的组合,先spin一阵然后用blocking lock
M*******a
发帖数: 1633
8
来自主题: JobHunting版 - n thread rendezvous怎么做用semaphore
count down lock算semaphore?
我看这题要求semaphore做
g***l
发帖数: 2753
9
【 以下文字转载自 Programming 讨论区 】
发信人: gmail (Gmail is Nothing --Lanxiang/SJTU), 信区: Programming
标 题: 请教一个linux下面的多线程semaphore的问题。
发信站: BBS 未名空间站 (Thu Oct 7 13:40:37 2010, 美东)
就是普通的两个linux thread,一个semaphore.
thread 1:
semaphore_wait(sem);
xxxxxx。。。。。
semaphore_signal(sem);
thread 2:
while(true)
{
semaphore_wait(sem);
xxxx......
semaphore_signal(sem);
}
现在的问题是thread 1被这个sema给block住了,根本没有机会去抢到这个sema,而
thread 2在不停的hold和release这个sema。
如果在thread2 process_sleep()一下,thread1就可以了。
两个线程的优先级是一样的。调度方式也是RR。
我的问... 阅读全帖
h**i
发帖数: 712
10
spin lock不会引起caller sleep,而semaphore会,它把进程从队列里拖出去,除非获
得锁。
spin lock适合保持时间短的任何context,包括中断,semaphore适合保持时间长的(
进程)context。
j******a
发帖数: 1599
11
来自主题: Programming版 - thread, semaphore, 问题。
why user-mode threads in a single process can use a user-mode semaphore but
cannot use a kernel semaphore to perform synchronization?
n**d
发帖数: 9764
12
来自主题: Programming版 - Semaphores in Linux (转载)
【 以下文字转载自 Linux 讨论区 】
发信人: noid (DoIneedit?), 信区: Linux
标 题: Semaphores in Linux
发信站: BBS 未名空间站 (Sat Jan 10 00:14:53 2009)
http://www.linuxdevcenter.com/pub/a/linux/2007/05/24/semaphores-in-linux.html?page=1
in this link, the author says "In general, the older Unix-based systems uses
the System V version and the current Linux-based systems use the POSIX
version."
The System V uses semget()... and POSIX uses sem_init...
However, in my company's Linux system, the gcc compiles semget()...
succesfu
n**d
发帖数: 9764
13
来自主题: Programming版 - Semaphores in Linux (转载)
I know the differnce between System V and POSIX for Semaphore. My question
is what kind of Semaphore Linux supports, System V or POSIX.
n**d
发帖数: 9764
14
来自主题: Programming版 - Semaphores in Linux (转载)
How do we use System V Semaphores for unrelated processes (not forked one)?
Some example code uses Ftok("filename" ...) to generate the key. So if the 2
processes use the same filename then they can get the same key.
More often, we use IPC_PRIVATE for the key. In this case, how can the 2
processes get the same key? Does that mean we have to use Ftok() if we want
to use Sys V Semaphores for 2 unrelated processes?
n**d
发帖数: 9764
15
来自主题: Unix版 - Semaphores in Linux (转载)
【 以下文字转载自 Linux 讨论区 】
发信人: noid (DoIneedit?), 信区: Linux
标 题: Semaphores in Linux
发信站: BBS 未名空间站 (Sat Jan 10 00:14:53 2009)
http://www.linuxdevcenter.com/pub/a/linux/2007/05/24/semaphores-in-linux.html?page=1
in this link, the author says "In general, the older Unix-based systems uses
the System V version and the current Linux-based systems use the POSIX
version."
The System V uses semget()... and POSIX uses sem_init...
However, in my company's Linux system, the gcc compiles semget()...
succesfu
z***9
发帖数: 696
16
同步不也是通信?Semaphores let processes query or alter status information。
They are often used to monitor and control the availability of system
resources such as shared memory segments
g*******s
发帖数: 2963
17
mutex: 一个厕所,A在用,B来敲了下进不去,就走开一会,等会再试。
spinlock: 一个厕所,A在用,B很急,就一直站那敲门,直到A完事。
semaphore: N个厕所,来一个人占一个,直到N个都满,就谢绝入内,除
非一个人完事后面的人才能进。
另外很多实际应用是hybrid,先spinlock敲两下,如果等一小会还不出来就进入mutex
状态。
w*******s
发帖数: 138
18
来自主题: JobHunting版 - n thread rendezvous怎么做用semaphore
一个初始值为-(n - 1)的semaphore就够了?
M*******a
发帖数: 1633
19
来自主题: JobHunting版 - n thread rendezvous怎么做用semaphore
这好像基本概念不清把
semaphore只能大于等于0吧,0的时候再wait就是block但是不会变成-1把
w*******e
发帖数: 395
20
来自主题: JobHunting版 - n thread rendezvous怎么做用semaphore
不懂题目什么意思
这俩thread难道不已经是用semaphore实现的了吗?
f******e
发帖数: 164
21
【 以下文字转载自 Programming 讨论区 】
发信人: francise (小飞猫), 信区: Programming
标 题: 你们觉得 《the little book of semaphore》这本书怎么样?
发信站: BBS 未名空间站 (Thu Mar 13 12:50:33 2008)
有谁看过的?
M**u
发帖数: 10158
22
凑合
基本就是给你详细讲了semaphore
y***y
发帖数: 295
23
来自主题: Programming版 - mutex和semaphore的差别到底是什么?
【 以下文字转载自 JobHunting 讨论区 】
发信人: sherwoodluo (sherwoodluo), 信区: JobHunting
标 题: Re: 刚才的电话面试,一点总结
发信站: BBS 未名空间站 (Mon Feb 25 16:39:11 2008)
关于mutex和semaphore的差别我被问到好几次。从来没有找到标准答案。我的理解是
mutex用于互斥访问,而且是忙等待。而semephore则是用于资源管理,属于阻塞等待
。大家指教一下?
,比较简单的一些问题
pattern, boost, standard template library, 需要的我可以邮件给大家
P*****f
发帖数: 2272
24
来自主题: Programming版 - mutex和semaphore的差别到底是什么?
mutex: must be release by the owner.
semaphore: could be release by other party

简单来说我觉得就是酱紫啊
I**********s
发帖数: 441
25
来自主题: Programming版 - mutex和semaphore的差别到底是什么?
I agree that it depends on specific implementations.
For C# multithreading, see a very good free E-book at
http://www.albahari.com/threading/
and it compares lock, mutex and semaphore in C# here:
http://www.albahari.com/threading/part2.html
P*****f
发帖数: 2272
26
来自主题: Programming版 - mutex和semaphore的差别到底是什么?
not really. We are talking about concept only.
In classic OS book, you know mutex has owner concept, while "semaphore"
not.
there is one of the fundamental differences between these two.

This depends on specific implementations.
w****h
发帖数: 212
27
来自主题: Programming版 - critical section semaphore muta的问题
请问以下有何区别?
critical section semaphore muta
B*****g
发帖数: 19
28
来自主题: Programming版 - Monitor和semaphore, mutex是什么关系?
各有各的用途吧。
monitor 可以带内部的conditional variable,也就是说里边还可以有一个waiting
queue.
semaphore 和 mutex 区别比较本质。
n**d
发帖数: 9764
29
来自主题: Programming版 - Semaphores in Linux (转载)
Yes, we can use gcc with -lrt or -lpthread to pass compile and link. What
does it mean? So can we say Linux supports both kinds of Semaphores?
h****b
发帖数: 157
30
来自主题: Programming版 - 问个semaphore 和 mutex的问题
binary semaphore 和 mutex 有啥分别? google过,好像说是ownership不一样,BS
任何
thread可以release, mutex只允许lock他的release. 谁给讲讲?先谢了
z****e
发帖数: 2024
31
来自主题: Programming版 - 问个semaphore 和 mutex的问题
what is semaphore? 3x.
g***l
发帖数: 2753
32
就是普通的两个linux thread,一个semaphore.
thread 1:
semaphore_wait(sem);
xxxxxx。。。。。
semaphore_signal(sem);
thread 2:
while(true)
{
semaphore_wait(sem);
xxxx......
semaphore_signal(sem);
}
现在的问题是thread 1被这个sema给block住了,根本没有机会去抢到这个sema,而
thread 2在不停的hold和release这个sema。
如果在thread2 process_sleep()一下,thread1就可以了。
两个线程的优先级是一样的。调度方式也是RR。
我的问题是:
thread1在等待一会是可以的,但是如果thread2释放了sema,scheduler 不是马上会把
正在等待sema的线程转为当前活动线程?
非常感谢!!
p***o
发帖数: 1252
33
I don't think you really have RR enabled for thread scheduling.
What have you done?
Otherwise, there is no such guarantee of liveness for semaphore.
You need some other mechanism, e.g. similar to RR, to achieve
some kind of fairness.
r****o
发帖数: 1950
34
我一直没有搞明白的就是:
semaphor是一种进程间的同步方式,为什么人们也把它列为进程间的通信方式之一呢?
d******s
发帖数: 113
35
来自主题: Unix版 - Unix semaphore
I use system V semaphore.
But for "semget" function, I can only set the key=1234,
nsem=1.
For any other key value, the function will fail "No space
left on device".
For key=1234, nsem!=1, the function will fail, not valid
argument.
What is wrong?
Thanks!
d*****h
发帖数: 8
36
One brother here posted a question he was asked in interview. I am trying to
give an answer. Please comment on it.
Thanks
// 实现两个函数: H() and O(), 这两个函数会被多线程调用。当一个线程调用H或O时
// ,如果当前已经有至少两个线程call H和一个线程call O。那么让两个call H和一个
// call O的线程返回(产生一个水分子),其他的都block
package Solution;
import java.util.concurrent.*;
import java.
public class H2O implements Runnable
{
private int[] H = new int[2];
private int O;
private Semaphore[] HCS = null;
private Semaphore[] HPS = null;
private Semapho... 阅读全帖
g**e
发帖数: 6127
37
来自主题: Programming版 - 编程题又一道
用semaphore写了一个,谁能告诉我用cyclicbarrier怎么做?
public class PrintOddEvenNumbersInTurn {
public static void main(String[] args) {
Semaphore mutex = new Semaphore(1);
Semaphore mutex2 = new Semaphore(1);

try {
mutex.acquire();
mutex2.acquire();
} catch (InterruptedException e1) {
e1.printStackTrace();
... 阅读全帖
u***************r
发帖数: 11227
38
来自主题: Security版 - unidentified_title
发信人: hellotree(树先生), 信区: Security
标题: 电脑蓝屏了怎么办?
发信站: BBS未名空间站(Wed Sep 20 10:23:28 2017,GMT)
Win7操作系统有酷炫的界面和丰富的功能,受到很多朋友的喜爱,成为大家生活工作的
好伙伴。但是因为硬件和软件的各种原因Win7系统也会偶尔遇到蓝屏,由数字和字母组
成的蓝屏代码分别代表不同的含义,下面为大家介绍一些Windows 7蓝屏的处理方法以
及部分Windows7系统蓝屏代码和含义
​Windows 7蓝屏产生的原因很多,但大多数往往集中在不兼容的硬件和驱动程序
有问题的软件、病毒等。遇到蓝屏错误时,可以尝试选用下面的方法。 重启系统
如果只是某个程序或驱动程序偶尔出现错误,重启系统后部分问题会消除。 检查硬

检查新硬件是否插牢,这个被许多人忽视的问题往往会引发许多莫名其妙的故障。如果
确认没有问题,将其拔下,然后换个插槽试试,并安装最新的驱动程序。同时还应对照
微软网站的硬件兼容类别检查一下硬件是否与操作系统兼容。
检查是否做了CPU超频,超频操作进行了超载运算,造成其内... 阅读全帖
s********k
发帖数: 6180
39
来自主题: JobHunting版 - google和twitter的onsite面经
读写锁要求是什么?是不是很多读比较少写,read可以同时进行?但是写的时候所有读
都被block?
按照qt的reference,写了一个
class ReadWriteMutex
{
public:
ReadWriteMutex(int maxReaders = 32)
: semaphore(maxReaders)
{
}

void lockRead() { semaphore++; }
void unlockRead() { semaphore--; }
void lockWrite() {
QMutexLocker locker(&mutex);
for (int i = 0; i < maxReaders(); ++i)
semaphore++;
}
void unlockWrite() { semaphore -= sem... 阅读全帖
s********k
发帖数: 6180
40
来自主题: JobHunting版 - google和twitter的onsite面经
读写锁要求是什么?是不是很多读比较少写,read可以同时进行?但是写的时候所有读
都被block?
按照qt的reference,写了一个
class ReadWriteMutex
{
public:
ReadWriteMutex(int maxReaders = 32)
: semaphore(maxReaders)
{
}

void lockRead() { semaphore++; }
void unlockRead() { semaphore--; }
void lockWrite() {
QMutexLocker locker(&mutex);
for (int i = 0; i < maxReaders(); ++i)
semaphore++;
}
void unlockWrite() { semaphore -= sem... 阅读全帖
w****x
发帖数: 2483
41
来自主题: JobHunting版 - read-write locker的实现
class ReadWriteMutex
{
public:
ReadWriteMutex(int maxReaders = 32)
: semaphore(maxReaders)
{
}

void lockRead() { semaphore++; }
void unlockRead() { semaphore--; }
void lockWrite() {
QMutexLocker locker(&mutex);
for (int i = 0; i < maxReaders(); ++i)
semaphore++;
}
void unlockWrite() { semaphore -= semaphore.total(); }
int maxReaders() const { return semaphore.to... 阅读全帖
j*****y
发帖数: 1071
42
来自主题: JobHunting版 - read-write locker的实现
有 write的时候 要block read, 这个清楚
有 read的时候,如果要 write, 需要block正在read的线程吗?还是等这些
正在 read的线程 释放 read-write lock后再 write ?

class ReadWriteMutex
{
public:
ReadWriteMutex(int maxReaders = 32)
: semaphore(maxReaders)
{
}

void lockRead() { semaphore++; }
void unlockRead() { semaphore--; }
void lockWrite() {
QMutexLocker locker(&mutex);
for (int i = 0; i < maxReaders(); ++i)
semaphore++;
}
... 阅读全帖
l**h
发帖数: 893
43
来自主题: JobHunting版 - LinkedIn 面经
实现两个函数: H() and O(), 这两个函数会被多线程调用。当一个线程调用H或O时
,如果当前已经有至少两个线程call H和一个线程call O。那么让两个call H和一个
call O的线程返回(产生一个水分子),其他的都block。
写了个Java版本的,大家指教:
import java.util.*;
import java.util.concurrent.*;
public class ThreadH2OSema {
private final Semaphore hCalled = new Semaphore(0);
private final Semaphore hUsed = new Semaphore(0);
private final Semaphore oCalled = new Semaphore(0);
private final Semaphore oUsed = new Semaphore(0);
public void H() {
try {
h... 阅读全帖
l**h
发帖数: 893
44
来自主题: JobHunting版 - LinkedIn 面经
实现两个函数: H() and O(), 这两个函数会被多线程调用。当一个线程调用H或O时
,如果当前已经有至少两个线程call H和一个线程call O。那么让两个call H和一个
call O的线程返回(产生一个水分子),其他的都block。
写了个Java版本的,大家指教:
import java.util.*;
import java.util.concurrent.*;
public class ThreadH2OSema {
private final Semaphore hCalled = new Semaphore(0);
private final Semaphore hUsed = new Semaphore(0);
private final Semaphore oCalled = new Semaphore(0);
private final Semaphore oUsed = new Semaphore(0);
public void H() {
try {
h... 阅读全帖
M******o
发帖数: 121
45
来自主题: JobHunting版 - 急求大神指导一道面经
这个如何?
private static int NUM_OS = 1;
private static int NUM_HS = 2;
private Semaphore oS = new Semaphore(NUM_OS);
private Semaphore hS = new Semaphore(NUM_HS);
private Semaphore entryS = new Semaphore(1);
public YourClass() {
oS.drainPermits();
hS.drainPermits();
}
public void O() {
entryS.acquire();
if (!tryAcquire(oS)) {
if (canGenerateH2O()) {
releaseForH2O();
entryS.release();
} else {
entryS.release();
oS.aquire... 阅读全帖
y*h
发帖数: 107
46
来自主题: Programming版 - how to answer this question, thanks
It should be possible to implement general semaphores using binary
semaphores. We can use the operations semWaitB and semSignalB and two
binary semaphores, delay and mutex. Consider the following:
01 Semaphore mutex = 1;
02 Semaphore delay = 0;
03
04 void semWait (semaphore s)
05 {
06 semWaitB(mutex);
07 if (--s < 0)
08 {
09 semSignalB(mutex);
10 semWaitB(delay);
11 }
12 else
13 semSignalB(mutex);
14 }
15
16
r****s
发帖数: 1025
47
来自主题: JobHunting版 - 一道多线程的面试题
用一个synchronous queue,再加个semaphore size(1), 一个字符 变量, 不就行了?
每个thread wait on semaphore, 而不是queue。抢到semaphore的就先查前一字符是什
么,如果是回车就release semaphore, exit; 如果不是就等在queue那里,进来一个字
符就print,然后把字符update进变量。release semaphore.
最后送一个回车符,大家一起exit就行了。
L*****1
发帖数: 34
48
来自主题: JobHunting版 - 求问一道multithreading问题
求教一下,也就是说se2 = new Semaphore(0)仍然还是只有一个permit,只是初始为0?
不过查java doc只有两种构造函数:
Semaphore(int permits)
Creates a Semaphore with the given number of permits and nonfair fairness
setting.
Semaphore(int permits, boolean fair)
Creates a Semaphore with the given number of permits and the given fairness
setting.
难道sem2 = 0不是说0个permit?
求指正。
b***i
发帖数: 3043
49
多谢牛人帮忙啊。
不过不是死在主循环,因为主循环在等semaphoore, 这个semaphore在GUI的keypressed
里面被release,而死机时我却看不到这个keypressed的进入,更没有release。
当时,我就一直按着enter,看到java console里面不停的显示主循环等semaphore, 进
入keypressed, 从某个if 里面release semaphore, 然后突然,停在主循环等待
semaphore那里了。我在keypressed里面入口的地方就显示进入keypressed,这个时候已
经不显示了。我怀疑keytyped把enter consumed了。
主循环在此
while (true){
kb.addPrintText("\n");
kb.prompt();
kb.EnterCommandMode(); // keymode is assigned to be 3
theGUI.waitCommand(); //等待semaphore
q*********u
发帖数: 280
50
看上去应该是dead lock吧, semaphore都出来了, 肯定要仔细检查程序里面的逻辑了,
生产者消费者的关系会不会在那些if里面乱掉了

多谢牛人帮忙啊。
不过不是死在主循环,因为主循环在等semaphoore, 这个semaphore在GUI的keypressed
里面被release,而死机时我却看不到这个keypressed的进入,更没有release。
当时,我就一直按着enter,看到java console里面不停的显示主循环等semaphore, 进
入keypressed, 从某个if 里面release semaphore, 然后突然,停在主循环等待
semaphore那里了。我在keypressed里面入口的地方就显示进入keypressed,这个时候已
经不显示了。我怀疑keytyped把enter consumed了。
主循环在此
while (true){
kb.addPrintText("\n");
kb.prompt();
kb.EnterCommandMode(); // keymode is
1 2 3 4 5 6 7 8 9 下页 末页 (共9页)