讨论mutex与semaphore区别的列表

本文讨论了在嵌入式软件面试中关于互斥锁与二进制信号量的问题。多位专业人士从不同角度解释了这两种同步机制的区别,并探讨了它们在特定操作系统如VxWorks中的实现细节。
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a   Time spent in forums: Reputation Power:
Semaphore/Mutex interview question

So I had a phone interivew for an embedded software position with a major communications company and was asked some questions that I didn't seem to answer to the interviewer's satisfaction. enough, I got called back for an on-site interview. Anyways, I want to run these questions by others to see what the real answer is. 1) What is the difference between a mutex and a binary semaphore? I couldn't really think of a difference but he seemed to be leading me to an answer. He asked if you can use a mutex in an interrupt service routine and then asked if you could use a semaphore within an interrupt service routine. of the questions he asked during the interview was whether I had heard the term "munching". Well, after the interview I looked it up and it is some term specific to VxWorks. Not having worked with VxWorks before I decided to read a VxWorks manual online. It seems like almost all his questions were related to VxWorks even though he didn't say so. Is there really one "correct" answer to the mutex vs binary sempahore question? There definitely does seem to be a difference in VxWorks as mutexes handle priority inversion. It seems to be S / library dependent and thus seems a bit unfair, but correct me if I'm wrong. Thanks.

Reply With Quote
  # 2  
Old September 15th, 2006, 06:20 AM
Boudewijn Dijkstra
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a   Time spent in forums: Reputation Power:
Semaphore/Mutex interview question

Fri, 15 Sep 2006 05:49:51 +0200 schreef joshc <josh.curtz@gmail.com>: So I had a phone interivew for an embedded software position with a major communications company and was asked some questions that I didn't seem to answer to the interviewer's satisfaction. enough, I got called back for an on-site interview. Anyways, I want to run these questions by others to see what the real answer is. > 1) What is the difference between a mutex and a binary semaphore? I guess it is valid to say that because these clumsy primitives are error prone and cause unwanted effects, the answer to this question is irrelevant. But I guess that I am spoilt for knowing easier-to-understand and less ugly ways to make processes communicate. I couldn't really think of a difference but he seemed to be leading me to an answer. He asked if you can use a mutex in an interrupt service routine and then asked if you could use a semaphore within an interrupt service routine. of the questions he asked during the interview was whether I had heard the term "munching". Well, after the interview I looked it up and it is some term specific to VxWorks. Not having worked with VxWorks before I decided to read a VxWorks manual online. It seems like almost all his questions were related to VxWorks even though he didn't say so. > Is there really one "correct" answer to the mutex vs binary sempahore question? There definitely does seem to be a difference in VxWorks as mutexes handle priority inversion. It seems to be S / library dependent and thus seems a bit unfair, but correct me if I'm wrong. The question indeed relies on the interpretations of these concepts by a specific implementor. -- Gemaakt met 's revolutionaire e-mailprogramma: http://www.opera.com/mail/

Reply With Quote
  # 3  
Old September 15th, 2006, 07:20 AM
Ed Prochak
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a   Time spent in forums: Reputation Power:
Semaphore/Mutex interview question

joshc wrote: So I had a phone interivew for an embedded software position with a major communications company and was asked some questions that I didn't seem to answer to the interviewer's satisfaction. enough, I got called back for an on-site interview. Anyways, I want to run these questions by others to see what the real answer is. > 1) What is the difference between a mutex and a binary semaphore? The purpose differs. Expand the name MUTEX is MUTual EXclusion. You use it to prevent two or more processes from executing the same section of code at the same time. Whereas a semaphore is just a signalling mechanism. Why he would specifically use a binary semaphore in the questions if strange. I couldn't really think of a difference but he seemed to be leading me to an answer. He asked if you can use a mutex in an interrupt service routine and then asked if you could use a semaphore within an interrupt service routine. of the questions he asked during the interview was whether I had heard the term "munching". Well, after the interview I looked it up and it is some term specific to VxWorks. Not having worked with VxWorks before I decided to read a VxWorks manual online. It seems like almost all his questions were related to VxWorks even though he didn't say so. > Is there really one "correct" answer to the mutex vs binary sempahore question? There definitely does seem to be a difference in VxWorks as mutexes handle priority inversion. It seems to be S / library dependent and thus seems a bit unfair, but correct me if I'm wrong. > Thanks. hth, ed

Reply With Quote
  # 4  
Old September 15th, 2006, 07:49 AM
joshc
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a   Time spent in forums: Reputation Power:
Semaphore/Mutex interview question

Ed Prochak wrote: joshc wrote: So I had a phone interivew for an embedded software position with a major communications company and was asked some questions that I didn't seem to answer to the interviewer's satisfaction. enough, I got called back for an on-site interview. Anyways, I want to run these questions by others to see what the real answer is. > 1) What is the difference between a mutex and a binary semaphore? > The purpose differs. Expand the name MUTEX is MUTual EXclusion. You use it to prevent two or more processes from executing the same section of code at the same time. Whereas a semaphore is just a signalling mechanism. Why he would specifically use a binary semaphore in the questions if strange. > hth, ed Yes, but a semaphore can be used for mutual exclusion too. The differences I found in VxWorks' implementation of mutexes and semaphores are very implementation specific. Any more ideas on common differences between mutexas and semaphores?

Reply With Quote
  # 5  
Old September 16th, 2006, 06:19 AM
Matthew Hicks
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a   Time spent in forums: Reputation Power:
Semaphore/Mutex interview question

Maybe the difference is in how the waitng processes are woken up. Maybe relating to which process and structures are responsible for managing each. I would think that semaphores are individually managed by each owning process while mutexes are centralling managed. This difference may make a big difference with respect to how interrupts impact each. Probably not correct, but my 2 cents. Hicks "joshc" <josh.curtz@gmail.comwrote in message news:1158292191.257574.114150@ So I had a phone interivew for an embedded software position with a major communications company and was asked some questions that I didn't seem to answer to the interviewer's satisfaction. enough, I got called back for an on-site interview. Anyways, I want to run these questions by others to see what the real answer is. > 1) What is the difference between a mutex and a binary semaphore? I couldn't really think of a difference but he seemed to be leading me to an answer. He asked if you can use a mutex in an interrupt service routine and then asked if you could use a semaphore within an interrupt service routine. of the questions he asked during the interview was whether I had heard the term "munching". Well, after the interview I looked it up and it is some term specific to VxWorks. Not having worked with VxWorks before I decided to read a VxWorks manual online. It seems like almost all his questions were related to VxWorks even though he didn't say so. > Is there really one "correct" answer to the mutex vs binary sempahore question? There definitely does seem to be a difference in VxWorks as mutexes handle priority inversion. It seems to be S / library dependent and thus seems a bit unfair, but correct me if I'm wrong. > Thanks. >

Reply With Quote
  # 6  
Old September 17th, 2006, 06:19 AM
Don
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a   Time spent in forums: Reputation Power:
Semaphore/Mutex interview question

joshc wrote: So I had a phone interivew for an embedded software position with a major communications company and was asked some questions that I didn't seem to answer to the interviewer's satisfaction. enough, I got called back for an on-site interview. Anyways, I want to run these questions by others to see what the real answer is. > 1) What is the difference between a mutex and a binary semaphore? I couldn't really think of a difference but he seemed to be leading me to an answer. He asked if you can use a mutex in an interrupt service routine and then asked if you could use a semaphore within an interrupt service routine. of the questions he asked during the interview was whether I had heard the term "munching". Well, after the interview I looked it up and it is some term specific to VxWorks. Not having worked with VxWorks before I decided to read a VxWorks manual online. It seems like almost all his questions were related to VxWorks even though he didn't say so. > Is there really one "correct" answer to the mutex vs binary sempahore question? There definitely does seem to be a difference in VxWorks as mutexes handle priority inversion. It seems to be S / library dependent and thus seems a bit unfair, but correct me if I'm wrong. A mutex is used to ensure that only a single "thread" accesses a resource. A (binary) semaphore is a mechanism that can be used to implement a mutex. Not all semaphores are mutex'es (just like not all counters are semaphores).

Reply With Quote
  # 7  
Old September 18th, 2006, 09:19 PM
Steve
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a   Time spent in forums: Reputation Power:
Semaphore/Mutex interview question

"joshc" <josh.curtz@gmail.comwrote in message news:1158292191.257574.114150@ So I had a phone interivew for an embedded software position with a major communications company and was asked some questions that I didn't seem to answer to the interviewer's satisfaction. enough, I got called back for an on-site interview. Anyways, I want to run these questions by others to see what the real answer is. > 1) What is the difference between a mutex and a binary semaphore? I couldn't really think of a difference but he seemed to be leading me to an answer. He asked if you can use a mutex in an interrupt service routine and then asked if you could use a semaphore within an interrupt service routine. of the questions he asked during the interview was whether I had heard the term "munching". Well, after the interview I looked it up and it is some term specific to VxWorks. Not having worked with VxWorks before I decided to read a VxWorks manual online. It seems like almost all his questions were related to VxWorks even though he didn't say so In VxWorks perspective, a mutex is roughly a semaphore with an initial value of 1. (With a few restrictions and variations). Steve > Is there really one "correct" answer to the mutex vs binary sempahore question? There definitely does seem to be a difference in VxWorks as mutexes handle priority inversion. It seems to be S / library dependent and thus seems a bit unfair, but correct me if I'm wrong. > Thanks. >

Reply With Quote
  # 8  
Old September 19th, 2006, 01:49 PM
Adrian
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a   Time spent in forums: Reputation Power:
Semaphore/Mutex interview question

joshc wrote: > 1) What is the difference between a mutex and a binary semaphore? > Thanks. Not sure for all RTS-es, but usually a mutex has priority inheritance, while a semaphore has not. Then a mutex is usually used for mutual exclusion of critical paths, while a semaphore is used for inter-thread synchronization and event notifications.

Reply With Quote
  # 9  
Old September 19th, 2006, 02:19 PM
Adrian
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a   Time spent in forums: Reputation Power:
Semaphore/Mutex interview question

Adrian wrote: joshc wrote: > 1) What is the difference between a mutex and a binary semaphore? > Thanks. > Not sure for all RTS-es, but usually a mutex has priority inheritance, while a semaphore has not. Then a mutex is usually used for mutual exclusion of critical paths, while a semaphore is used for inter-thread synchronization and event notifications. To be more specific, if you have two concurrent threads, with mutexes you will always have a pair of get/put mutex in each thread. With semaphores, you may wait (get) for the semaphore in one thread and put the semaphore in the other thread. Never use this with mutexes

Reply With Quote
  # 10  
Old September 22nd, 2006, 06:20 AM
Steve Watt
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a   Time spent in forums: Reputation Power:
Semaphore/Mutex interview question

In article <1158292191.257574.114150@h48g2000cwc.googlegroups. com>, joshc <josh.curtz@gmail.comwrote: >So I had a phone interivew for an embedded software position with a >major communications company and was asked some questions that I didn't >seem to answer to the interviewer's satisfaction. enough, I got >called back for an on-site interview. Anyways, I want to run these >questions by others to see what the real answer is. You've gotten a number of slightly different opinions, which should be a strong hint that there's no True Definition >1) What is the difference between a mutex and a binary semaphore? The most obvious answer that pops into my head comes from PSIX: A mutex has an owner, a semaphore (binary or otherwise) does not. Any old thread can post a semaphore, but only the owner of a mutex can unlock it. of the questions he asked during the interview was >whether I had heard the term "munching". Well, after the interview I >looked it up and it is some term specific to VxWorks. Not having worked >with VxWorks before I decided to read a VxWorks manual online. It seems >like almost all his questions were related to VxWorks even though he >didn't say so. That's a new term by me (and an amusing way to add some functionality that simply belongs hidden in the c++ driver, now that I've looked it up). But then, I've done hardly any work with vxWorks, preferring "real"[1] operating systems (with memory protection and process models) for larger projects, and much lighter weight executives for smaller projects. >Is there really one "correct" answer to the mutex vs binary sempahore >question? There definitely does seem to be a difference in VxWorks >as mutexes handle priority inversion. It seems to be S / library >dependent and thus seems a bit unfair, but correct me if I'm wrong. The reason a mutex *can* handle priority inversion is that there's a known owner to boost the priority of when the time comes. I suspect where the interviewer was leading with the "which one can you use in an interrupt handler" is deeper into the vxWorks-only space where an interrupt handler is running in the task environment of whatever thread it happened to interrupt. That makes the meaning of owning a mutex inside an interrupt handler somewhat problematic. quite common way of communicating out of an interrupt handler into task space is to have the interrupt handler signal a semaphore, which the task that's waiting for the interrupt is blocked on. The S will then make that task ready, and when time comes for a scheduling decision, it might even get the/a CPU. [1] K, that's flame bait. My bias showing again -- Steve Watt KD6GGD PP-ASEL-IA ICBM: 121W 56' 57.5" / 37N 20' 15.3" Internet: steve @ Watt.CM Whois: SW32-ARIN Free time? There's no such thing. It just comes in varying prices

Reply With Quote
  # 11  
Old September 28th, 2006, 06:10 AM
Adib Taraben
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a   Time spent in forums: Reputation Power:
Semaphore/Mutex interview question

joshc schrieb: routine and then asked if you could use a semaphore within an interrupt service routine. of the questions he asked during the interview was whether I had heard the term "munching". Well, after the interview I looked it up and it is some term specific to VxWorks. Not having worked with VxWorks before I decided to read a VxWorks manual online. It seems like almost all his questions were related to VxWorks even though he didn't say so. In the interrupt you should not wait a resource so you should only set the semaphore and never wait Adib. Is there really one "correct" answer to the mutex vs binary sempahore question? There definitely does seem to be a difference in VxWorks as mutexes handle priority inversion. It seems to be S / library dependent and thus seems a bit unfair, but correct me if I'm wrong. > Thanks. >

Reply With Quote
  # 12  
Old October 23rd, 2006, 12:25 PM
Registered User
Dev Archives Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 1 vikont User rank is Just a Lowly Private (1 - 20 Reputation Level)   Time spent in forums: 30 m 43 sec Reputation Power: 0
Quote:
Originally Posted by joshc
Is there really one "correct" answer to the mutex vs binary sempahore question? There definitely does seem to be a difference in VxWorks as mutexes handle priority inversion. It seems to be S / library dependent and thus seems a bit unfair, but correct me if I'm wrong. Thanks.
It's really this simple. The task that owns mutex semaphore can take it again, while if it attempts to take again the binary semaphore that it owns, it'll fail. In other words, vxworks mutex sema are recursive (re-entrant), while binary are not. Yes, I have access to vxworks source code, so I looked it up.

Reply With Quote
  # 13  
Old October 11th, 2007, 03:22 PM
Registered User
Dev Archives Newbie (0 - 499 posts)
 
Join Date: Oct 2007
Posts: 1 tnoelhere User rank is Just a Lowly Private (1 - 20 Reputation Level)   Time spent in forums: 28 m 43 sec Reputation Power: 0
Semaphore/Mutex interview question

This is the most dumbest question ever asked in interviews. "What is the diff between a mutex and a semaphore". Well the answer is: "which context are ya referring to ? Linux, VxWorks, Symbian, pThreads, Windows ? " Caus these words have diff connotations in diff Oses. Mutexes and binary semaphores are the same in Linux. Binary semaphores don't exist in pThreads, hence mutexes and semaphores (which the interviewer means to say counting semaphores) are different in pThreads. As already mentioned in this thread there are subtle differences in vxWorks as well. Thanks, Timothy Noel

Reply With Quote
成都市作为中国西部地具有战略地位的核心都市,其人口的空间分布状况对于城市规划、社会经济发展及公共资源配置等研究具有基础性数据价值。本文聚焦于2019年度成都市人口分布的空间数据集,该数据以矢量格式存储,属于地理信息系统中常用的数据交换形式。以下将对数据集内容及其相关技术要点进行系统阐述。 Shapefile 是一种由 Esri 公司提出的开放型地理空间数据格式,用于记录点、线、面等几何要素。该格式通常由一组相互关联的文件构成,主要包括存储几何信息的 SHP 文件、记录属性信息的 DBF 文件、定义坐标系统的 PRJ 文件以及提供快速检索功能的 SHX 文件。 1. **DBF 文件**:该文件以 dBase 表格形式保存各地理要素相关联的属性信息,例如各域的人口统计数值、行政划名称及编码等。这类表格结构便于在各类 GIS 平台中进行查询编辑。 2. **PRJ 文件**:此文件明确了数据所采用的空间参考系统。本数据集基于 WGS84 地理坐标系,该坐标系在全球范围内广泛应用于定位空间分析,有助于实现跨域数据的准确整合。 3. **SHP 文件**:该文件存储成都市各(县)的几何边界,以多边形要素表示。每个多边形均配有唯一标识符,可属性表中的相应记录关联,实现空间数据统计数据的联结。 4. **SHX 文件**:作为形状索引文件,它提升了在大型数据集中定位特定几何对象的效率,支持快速读取显示。 基于上述数据,可开展以下几类空间分析: - **人口密度评估**:结合各域面积对应人口数,计算并比较人口密度,识别高密度低密度域。 - **空间集聚识别**:运用热点分析(如 Getis-Ord Gi* 统计)或聚类算法(如 DBSCAN),探测人口在空间上的聚集特征。 - **空间相关性检验**:通过莫兰指数等空间自相关方法,分析人口分布是否呈现显著的空间关联模式。 - **多要素叠加分析**:将人口分布数据地形、交通网络、环境指标等其他地理图层进行叠加,探究自然人文因素对人口布局的影响机制。 2019 年成都市人口空间数据集为深入解析城市人口格局、优化国土空间规划及完善公共服务体系提供了重要的数据基础。借助地理信息系统工具,可开展多尺度、多维度的定量分析,从而为城市管理学术研究提供科学依据。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值