讨论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
物联网通信协议测试是保障各类设备间实现可靠数据交互的核心环节。在众多适用于物联网的通信协议中,MQTT(消息队列遥测传输)以其设计简洁低能耗的优势,获得了广泛应用。为确保MQTT客户端服务端的实现严格遵循既定标准,并具备良好的互操作性,实施系统化的测试验证至关重要。 为此,采用TTCN-3(树表结合表示法第3版)这一国际标准化测试语言构建的自动化测试框架被引入。该语言擅长表达复杂的测试逻辑数据结构,同时保持了代码的清晰度可维护性。基于此框架开发的MQTT协议一致性验证套件,旨在自动化地检验MQTT实现是否完全符合协议规范,并验证其Eclipse基金会及欧洲电信标准化协会(ETSI)所发布的相关标准的兼容性。这两个组织在物联网通信领域具有广泛影响力,其标准常被视为行业重要参考。 MQTT协议本身存在多个迭代版本,例如3.1、3.1.1以及功能更为丰富的5.0版。一套完备的测试工具必须能够覆盖对这些不同版本的验证,以确保基于各版本开发的设备应用均能满足一致的质量可靠性要求,这对于物联网生态的长期稳定运行具有基础性意义。 本资源包内包含核心测试框架文件、一份概述性介绍文档以及一份附加资源文档。这些材料共同提供了关于测试套件功能、应用方法及可能包含的扩展工具或示例的详细信息,旨在协助用户快速理解并部署该测试解决方案。 综上所述,一个基于TTCN-3的高效自动化测试框架,为执行全面、标准的MQTT协议一致性验证提供了理想的技术路径。通过此类专业测试套件,开发人员能够有效确保其MQTT实现的规范符合性系统兼容性,从而为构建稳定、安全的物联网通信环境奠定坚实基础。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值