o****d 发帖数: 2835 | 1 首先 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 |
|
g*******s 发帖数: 2963 | 2 mutex: 一个厕所,A在用,B来敲了下进不去,就走开一会,等会再试。
spinlock: 一个厕所,A在用,B很急,就一直站那敲门,直到A完事。
semaphore: N个厕所,来一个人占一个,直到N个都满,就谢绝入内,除
非一个人完事后面的人才能进。
另外很多实际应用是hybrid,先spinlock敲两下,如果等一小会还不出来就进入mutex
状态。 |
|
m********2 发帖数: 89 | 3 spinlock 不 reschedule.
mutex 会 reschedule.
spinlock 跟 mutex 比更合适。 |
|
y****1 发帖数: 58 | 4 谢谢!
spinlock 没有 context switch,用他的操作需要是 atomic operation
这是最主要的吗?
谢谢 |
|
l*******e 发帖数: 127 | 5 还有某些情况下不允许你context switch,必须busy wait,比如kernel mode的driver
经常用。
谢谢!spinlock 没有 context switch,用他的操作需要是 atomic operation这是最
主要的吗?谢谢 |
|
|
b*******s 发帖数: 5216 | 7 补充一点,spinlock是busy waiting,在并发冲突少的时候效率高
如果并发冲突频繁,还是要用锁 |
|
K******g 发帖数: 1870 | 8 不是这样的吧?
据我的理解,如果另一个hold lock的进程如果时间比较长的话,spinlock在UniProc上
会增加scheduler的负担,大量的时间消耗在swap上,导致hold lock的那个进程的进展
很慢,结果系统的performance降低了。如果在MultiProc上,hold lock的那个进程不会
因为spinlock而减慢,这样子spinlock的影响就没有UniProc上那么大了.
如果spinlock真的要disable preemption的话,那么它只能出现在MultiProc上。但是,spinlock的概念在UniProc上也存在。
()
spin_ |
|
t****t 发帖数: 6806 | 9 1st question i am not sure what he want.
for one thing, you can use atomic to implement spinlock, which is a kind of
mutex. but atomic won't yield CPU, so it's spinlock only.
for another thing, in some cases, you may use atomic for simple shared data,
to avoid mutex (which is expensive).
2nd question, he's asking spinlock and mutex, not cv. spinlock and mutex can
replace each other (while the efficiency are certainly different). you've
already covered difference of spinlock and mutex. but cv is ... 阅读全帖 |
|
s*****m 发帖数: 8094 | 10 那个只是说hybrid spinlock+mutex,和用spinlock+queue实现mutex没几毛关系吧。
这题目出得不清不楚的。感觉好像要真能实现mutex,要把这spinlock的runtime
quantum搞得很小?
不然怎么着spinlock都要lock啊! |
|
s********k 发帖数: 6180 | 11 比如好几个thread同时access某段代码做spinlock,某个thread拿到了之后,但是耗得
时间太多了,被kernel重新schedule INT了,就被context switch出去了,但是它的
spinlock还没有释放,其他同时access spinlock threads就会一直busy waiting,直
接拖死系统了。 |
|
s********r 发帖数: 403 | 12 The semantics of reader / writer lock is:
[1] Allow multiple readers for read_op(), when reader reads, no writer can
write
[2] When writer perform write_op(), no reader can read_op() successfully.
Would you think that one mutex implementation can describe the above
semantics?
Besides, what do you mean:
"Even in kernel, it is very hard to get the guarantee that the lock holder
does not block on anything and will release the lock in a timely manner."
In Linux kernel, the spinlock works on hardwar... 阅读全帖 |
|
s********r 发帖数: 403 | 13 [1] That one mutex method shows what the reader / writer lock in semantics,
if you watch carefully, I did not provide the interface such as r_lock()
or w_lock().
That is sufficient to explain the functionality, if they do not try to hold
the mutex again, which does not make any sense. The write() operation only
do write job, it does not hold any mutex.
[2] spinlock() functions in hardware threads environment. It is pointless to
use spinlock() is a uniprocessor environment. And it is not used to... 阅读全帖 |
|
S*A 发帖数: 7142 | 14 是吗,你是刚学 system call 和 spinlock 一定什么
东西都要扯上吗?
我只是觉得这个 1000 cycle 的差别和 system call
和 spinlock 关系不大。
请你这个 CS 专业的来示范一下如何用 system call
和 spinlock 解释这个 1000 cycle 的差别,让我们
看看是不是很牵强。 |
|
a**********k 发帖数: 1953 | 15 preemption means time slice support.
so
on UP with preemption, spinlock = disabling preemption = no time slice;
on UP without preemption, spinlock = no op;
Hope this helps. |
|
S**I 发帖数: 15689 | 16 ☆─────────────────────────────────────☆
yuhanlin (Yuhan) 于 (Mon Aug 29 00:18:17 2011, 美东) 提到:
周五面完最后一个onsite, 累的惨兮兮的, 好容易爬回家. 不管结果如何, 这段时间找
工作算是告一段落了.
下面把这段时间面试中被问到的题目整理一下, 供大家参考. 我也就不说具体是那些公
司了, 都是很典型的面试题, 到哪里都有可能会被问到.
* implement memcpy? How to improve? how to determine if a system is
32bit or 64bit?
* how is static keyword used in Java?
* a list of intervals, no overlapping and sorted, write a function to
insert an interval into the list and still keep the list sorted and no
overlapping.... 阅读全帖 |
|
y****1 发帖数: 58 | 17 spinlock 和 semaphore, mutex 的 区别????
可是感觉没有回答到点上。
spinlock: 1,hold lock 时间短,2,busy wait, 中断里用
semaphore 和 mutex: 1, hold lock 时间长, 2,block wait,中断里不可用
不知道还有其他吗??
谢谢!! |
|
c**1 发帖数: 71 | 18 any lock has an implicit requirement: no self deadlock. your implementation
does not meet this requirement.
what do you mean by "one hardware thread call spinlock affects itself"? any
lock involves multiple threads.
kernel uses spinlock because sometimes it is not safe to go to sleep. In
order for it to spin, every user of the lock has to be careful not to go to
sleep and release the lock quickly. |
|
y*u 发帖数: 111 | 19 系统软件工程师,一共七轮,水平有限,还请指正。
1. 中国人:
I. 我Resume上的Semaphore是如何实现的,写下来。我写了一半,有一点忘记了。
问了spinlock使用的问题。
II. 智力题:小明一家过河
2. Hiring manager白人:
I. 一张七道题关于C语言的卷子!!!其它再无交流空间,给试卷做,当时就闷了!
题目有:str[]和*str的区别;数组指针大小;包含不同类型变量的struct大小;实现
一个程序能查看自身的内存占用情况;还有三题记不得了
3. 阿三带吃饭:
I. 问了我一些基本概念,学校学习情况。然后开始说:我们边吃边想,思 考
一个程序题目吧。你别停啊,多吃。 题目倒是不难,两个string表示的二进制,一
个是32位,一个8位。里面都是1和0。问怎么判断B是A的substring?我答了两种:第一
种最简单的,从左往右遍历,O(m*n)。第二种: 我们用一个8位的mask把A罩起来,然后
和B做XOR,如果结果是0,那么就是substring,如果不是,那么继续shift再找。这人
... 阅读全帖 |
|
I*******d 发帖数: 108 | 20 spinlock是忙等待,mutex不是,也就是说一个线程在等待mutex的时候可以做别的事情
,意味着自己隔一段时间检测能不能拿到spinlock, 拿不到的话做queue里别的事情? |
|
N****w 发帖数: 21578 | 21 TCP 的问题课本上都有
spinlock 看看 Linux 的那两本书里似乎有,就是 Understanding Linux Kernel
和 Linux Device Driver
好像是用 spinlock 就可以不用关硬件中断了,其它 CPU 还可以响应硬件中断。
length), |
|
z***q 发帖数: 907 | 22 【 以下文字转载自 Chemistry 讨论区 】
发信人: zypsq (妈妈的小猪), 信区: Chemistry
标 题: 核磁问题求教
发信站: BBS 未名空间站 (Mon Jun 21 11:34:41 2010, 美东)
最近要做一个TOCSY类(HCCNH-TOCSY)的实验,需要计算TOCSY spinlock and pulse
power.查了相关文献,但是文献中给的spinlock都是以赫兹为单位的,还有给rf field
(Hz or ppm)的,想请教NMR达人,怎么将文献中的数值换成实验上可用的parameter
?我们用的是bruker 600MHz cryoprobe.有人曾做过类似实验么?可否教我怎么设定这
类实验?我是nmr新人,太多东西要学了,有点不知所措,还望达人们不吝赐教!万分
感谢! |
|
z***q 发帖数: 907 | 23 最近要做一个TOCSY类(HCCNH-TOCSY)的实验,需要计算TOCSY spinlock and pulse
power.查了相关文献,但是文献中给的spinlock都是以赫兹为单位的,还有给rf field
(Hz or ppm)的,想请教NMR达人,怎么将文献中的数值换成实验上可用的parameter
?我们用的是bruker 600MHz cryoprobe.有人曾做过类似实验么?可否教我怎么设定这
类实验?我是nmr新人,太多东西要学了,有点不知所措,还望达人们不吝赐教!万分
感谢! |
|
z***q 发帖数: 907 | 24 【 以下文字转载自 Chemistry 讨论区 】
发信人: zypsq (妈妈的小猪), 信区: Chemistry
标 题: 核磁问题求教
发信站: BBS 未名空间站 (Mon Jun 21 11:34:41 2010, 美东)
最近要做一个TOCSY类(HCCNH-TOCSY)的实验,需要计算TOCSY spinlock and pulse
power.查了相关文献,但是文献中给的spinlock都是以赫兹为单位的,还有给rf field
(Hz or ppm)的,想请教NMR达人,怎么将文献中的数值换成实验上可用的parameter
?我们用的是bruker 600MHz cryoprobe.有人曾做过类似实验么?可否教我怎么设定这
类实验?我是nmr新人,太多东西要学了,有点不知所措,还望达人们不吝赐教!万分
感谢! |
|
C*****e 发帖数: 13 | 25 Maybe wrong, just according to my knowledge.
Shared memory is not syncronized autmatically. You should use some
other kernel routines to avoid collision, like semopher operations.
Because semctl and semget is in kernel mode, the internal data is not
protected in traditional single CPU kernel. In multi-CPU mode, these
internal data maybe "hotspot" by several CPUs, so the kernel uses
spinlocks to protect them. The spinlocks usually use hardware syncronization
mechenism to keep syncronized. In X86 |
|
x****u 发帖数: 44466 | 26 人家号称几条汇编写个spinlock,MS的系统调用当然是SB了。 |
|
P*******u 发帖数: 28 | 27 1. some server uses unsafe c functions, such as gets() to receive data
from client. The client could send a very large buffer, which overflows
the server buffer. The malicious client could change the return address in
the oversized buffer to run its own code.
4. when the client application crashes, the kernel will close the
connection to the other side.
5. define a struct and associated functions in a library.
6. this one is still arguable. use spinlock may possibly save the context
switch. Howe |
|
a**********k 发帖数: 1953 | 28 That's correct.
on UP without preemption, spinlock is no-op. |
|
b*********n 发帖数: 464 | 29 spinlock是busy waiting。在multiprocessor的情况下才比较有用(减少context
switch产生的cost等),uniprocessor下比较浪费cpu,要在调度(被动)后才能让出
cpu,效果不如一般的lock。 |
|
l*******y 发帖数: 1498 | 30 请再去看看书,spin_lock就是kernel nonpreemptive的标志
是,spinlock的概念在UniProc上也存在。 |
|
j*****u 发帖数: 1133 | 31 没看你的链接不过我觉得意思是:
monitor:只有一个thread能够拿到lock,其他threads被block
monitor跟mutex类似,区别是monitor更轻量级一点,mutex是kernal object,因此可以
interprocess
synchronization有很多种,不同语言有不同实现但方法大同小异,建议找一个语言仔细
看看
不同方法有不同的适用范围,通常越efficient的限制越大
比如volatile,Interlock,SpinLock等non-blocking的synchronization适用范围肯定没
有monitor广,同理monitor也不像mutex/semaphare可以interprocess同步 |
|
c******w 发帖数: 102 | 32 经过半年多的征战,现在总算要进入尾声。 为了回馈jobhunting版, 特发面经,感谢
所有对我有过帮助的战友们,并希望后来者都能有好的offer。 声明: 谢绝某些喜欢
吓唬小mm的WSN来看贴,要看去火星看。
某Palo Alto公司(P):
Interview 1:
1. 两个sorted array, 如果merge成一个array。
2. 如果这两个array没有sort呢?并分析复杂度。
3. 如果有K个没有sorted的array怎么办呢?
4. 如果当前机器有K个cpu, 怎么处理问题3呢?复杂度分析。(考虑
multithreading)
Interview 2:
1. 给定一个array,如何找到两个数字, 他们的和等于一个target number。 需
要提供几种不同的算法,并比较分析。
2. 关于数据库的。 什么是Key, 什么是foreign key。 为什么要用foreign key?
3. 怎么提高数据库查询的速度? (indexing)。 Indexing是如何实现的。
4. 如果有一个数据库现在运行速... 阅读全帖 |
|
s*****n 发帖数: 5488 | 33 答案是什么?我想用spinlock但是估计 不对。 |
|
t****t 发帖数: 6806 | 34 Q1:
wait for n-th bit to become 1 (by hardware or other thread).
problem: should make p point to volatile, otherwise may go deadloop.
multiple evaluation of (1<
if atomic is supported, better use atomic. polling efficiency is low, but
this depends on situation, for example in firmware, or in spinlock, this is
quite ok. if it is used to for multithreads, you may want to use thread
communication functions instead.
Q2: test whether a is 2^n. if tha... 阅读全帖 |
|
t****t 发帖数: 6806 | 35 that's right. usually for several hundreds cycles wait time, spinlock is
faster than heavy-weight mutex. |
|
t****t 发帖数: 6806 | 36 Q1:
wait for n-th bit to become 1 (by hardware or other thread).
problem: should make p point to volatile, otherwise may go deadloop.
multiple evaluation of (1<
if atomic is supported, better use atomic. polling efficiency is low, but
this depends on situation, for example in firmware, or in spinlock, this is
quite ok. if it is used to for multithreads, you may want to use thread
communication functions instead.
Q2: test whether a is 2^n. if tha... 阅读全帖 |
|
t****t 发帖数: 6806 | 37 that's right. usually for several hundreds cycles wait time, spinlock is
faster than heavy-weight mutex. |
|
y******n 发帖数: 47 | 38 周五面完最后一个onsite, 累的惨兮兮的, 好容易爬回家. 不管结果如何, 这段时间找
工作算是告一段落了.
下面把这段时间面试中被问到的题目整理一下, 供大家参考. 我也就不说具体是那些公
司了, 都是很典型的面试题, 到哪里都有可能会被问到.
* implement memcpy? How to improve? how to determine if a system is
32bit or 64bit?
* how is static keyword used in Java?
* a list of intervals, no overlapping and sorted, write a function to
insert an interval into the list and still keep the list sorted and no
overlapping.
* given a function on sorted dictionary to retrieve word by index,
string getWord(int i), how to i... 阅读全帖 |
|
M**u 发帖数: 10158 | 39 恩
spinlock在kernel里头用得比较多,有不少都是减小了context switch的开销
busy |
|
k**w 发帖数: 376 | 40 >>fork,exec ipc,how many types of ipc
after fork, does new process get file handles and locks
>>what's are spin lock? are they better than mutex?
how many spinlocks work on smp and up architecure?
>>what will happen /can u have printf/printk inside an interrupt handler?
>>what's the difference between wake_up() and wake_up_interruptible() apis
in the linux kernel
when should sude which one, how it should be decided?
>>what's the difference between sleeep_on() and interruptible_sleep_on()
>>what ... 阅读全帖 |
|
|
r****t 发帖数: 10904 | 42 【 以下文字转载自 Quant 讨论区 】
发信人: hsteven (Steven), 信区: Quant
标 题: Re: 别了,纽约
发信站: BBS 未名空间站 (Sat Feb 11 12:55:07 2012, 美东)
GS是Quant,专门做HFT的team,2 sigma是developer,自然也是HFT。
GS悲剧意料之中,BS equation都不怎么懂。至于2 sigma,我觉得悲剧的主要原因就是语言和开
发环境那些细节没答好,而且mathematic programming不熟。总结了一下2 sigma没答好的题
目(包括了另外一个CS的碰到的题目),题目原题就不贴了,就贴点考察的概念吧。如果没特别说明
都是C或者C++语言环境。
atomic怎么代替mutex
spinlock和mutex区别
global variable的初始化默认value(比如char arr[]),是否取决compiler
为什么STL list iterator 没有 < operator
程序运行时计算机硬件怎么确定一个指针对应的value(具体说明)
怎么提高BST tra... 阅读全帖 |
|
m**********0 发帖数: 356 | 43 Here is what I found:
spinlock:
It is a big CPU sucker and its biggest risk is being interrupted by OS
scheduler while holding the lock.
One needs to be very careful during implementation to reduce unnecessary
busy waiting
In general, works only with hardware atomic support such as test-and-set
mutex: resource can be accessed only by 1 at a time
semaphore: # of accessors to the resource at a time depending on the
initialization of the semaphore. When it is initialized to 1, it has same
effect as... 阅读全帖 |
|
j*****I 发帖数: 2626 | 44 看了一下,好像spinlock和mutex的一个主要区别是后者是系统维护,前者是thread自
己check. short waiting的情况下,前者比较efficient.
mutex和semaphore的区别你研究的太深了吧。any number和binary一般足够了。说多了
反而容易露马脚吧。
hold
快。 |
|
s********k 发帖数: 6180 | 45 更正一下,spin lock也可以被context switch出去,这样更危险,因为如果thread没
有释放spinlock就被INT了,其他的thread也拿不到这个资源。基本相当于系统锁死了。 |
|
j*****I 发帖数: 2626 | 46 文档说spinlock适合那种short waiting.你说的这种情况应该是典型不符合。 |
|
|
|
s********r 发帖数: 403 | 49 #1 You are right about the notification by condition number.
My point is that using one mutex is the easiest way to explain what a basic
reader / writer lock is, before moving to more details.
#2 In certain situations, for SMP, local spin can be better, that is how
spinlock works in kernel. |
|
c**1 发帖数: 71 | 50 if an implementation is incorrect, it doesn't matter whether it is simple.
In user space, thread can go to sleep at any time, nothing can guarantee
that a spin can always get the lock, so all spinlocks in user space have a
limit on how long it can spin, and if it still does not get the lock, it
suspends itself, waiting for other to wake it up. It is impossible to use
only mutex to implement this suspend-and-wait-for-notify.
Even in kernel, it is very hard to get the guarantee that the lock hol... 阅读全帖 |
|