今天看了VxWorks 里面的进程通讯机制,可能有些会和Linux很像,有些会有差别。现在大致进行一下总结。
VxWorks 支持一系列的 InterTask Communication的机制,包括
1 Shared memory 共享内存
2 Semaphores (这个可能会让人比较崩溃)
3 Mutexes and condition variables
4 Message queues and pipes (消息队列和管道,其实这两个应该差不多,只是消息队列应该是可以提供有格式的数据,而pipes只能提供流。。两者都可以使用C/S的结构进行Task之间的通信)
5 Socket 以及remote procedure calls (主要是也可以在网络间进行task之间的通信)
6 Signals (信号。和linux差不多 也是有些信号,有句比较经典的话是: (Signals are more appropriate for error and exception handling than as a general purpose intertask communication mechanism. 就是信号更适合做一个error 或者exception 的handler的工作,而不是用作通用的inter task间的通信机制。。)
1、 shared Data Structures
VxWorks里面的地址是一个 single linear address space ,这样以来全局变量啊、 linear buffers 、ring buffers,linked lists 还有指针什么的都可以被运行在不同contexts的 task 直接访问。。
如图所示:
2 Mutual Exclusion (互斥量)
很多方法都可以用作 task 对某一资源的独占,比如说:
disabling interrupts , disabling preemption ,resource locking with semaphores
(去中断)、 (去抢占) 、 (信号灯的资源锁)。。。
disabling interrupts :
比如 funcA()
{
int lock=intlock();
/* critical region of code that cannot be interrupted
*/
intUnlock(lock);
}
这个方法阻止了外部中断。。 这样就会使外部中断 latency了。所以在实时系统中不推荐用
disabling preemption :
funcA ()
{
taskLock ();
.
. /* critical region of code that cannot be interrupted */
.
taskUnlock ();
}
taskLock () 阻止了优先级抢占, 可能也会对实时性产生影响,因为它会屏蔽高优先级对资源的占有!
所以推出下面的比较好的方式来实现资源的 “互斥”,这种解决方法就是传说中的 Semaphores 信号量了!
开始介绍传说中的Semaphores 以及 优先级翻转等一些概念:
Semaphores 有三种,分别是:
1、binary 2、mutual exclusion 3、counting