主要内容翻译自QNX开发文档,根据自己理解翻译,错误之处在所难免,目的还是提供给大家对照阅读,我自己也写了一些代码,抽空后继也会放上
<!-- /* Font Definitions */ @font-face {font-family:Wingdings; panose-1:5 0 0 0 0 0 0 0 0 0; mso-font-charset:2; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:0 268435456 0 0 -2147483648 0;} @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:Cambria; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:0; mso-generic-font-family:roman; mso-font-pitch:variable; mso-font-signature:-1610611985 1073741899 0 0 159 0;} @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:-1610611985 1073750139 0 0 159 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; text-indent:10.0pt; mso-char-indent-count:2.0; line-height:150%; mso-pagination:none; font-size:12.0pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-font-kerning:1.0pt;} h2 {mso-style-priority:9; mso-style-qformat:yes; mso-style-link:"标题 2 Char"; mso-style-next:正文; margin-top:13.0pt; margin-right:0cm; margin-bottom:13.0pt; margin-left:0cm; text-align:justify; text-justify:inter-ideograph; text-indent:10.0pt; mso-char-indent-count:2.0; line-height:173%; mso-pagination:lines-together; page-break-after:avoid; mso-outline-level:2; font-size:16.0pt; font-family:"Cambria","serif"; mso-ascii-font-family:Cambria; mso-ascii-theme-font:major-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:major-fareast; mso-hansi-font-family:Cambria; mso-hansi-theme-font:major-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:major-bidi; mso-font-kerning:1.0pt;} h3 {mso-style-priority:9; mso-style-qformat:yes; mso-style-link:"标题 3 Char"; mso-style-next:正文; margin-top:13.0pt; margin-right:0cm; margin-bottom:13.0pt; margin-left:0cm; text-align:justify; text-justify:inter-ideograph; text-indent:10.0pt; mso-char-indent-count:2.0; line-height:173%; mso-pagination:lines-together; page-break-after:avoid; mso-outline-level:3; font-size:16.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-font-kerning:1.0pt;} p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph {mso-style-priority:34; mso-style-unhide:no; mso-style-qformat:yes; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; text-indent:21.0pt; mso-char-indent-count:2.0; line-height:150%; mso-pagination:none; font-size:12.0pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-font-kerning:1.0pt;} span.2Char {mso-style-name:"标题 2 Char"; mso-style-priority:9; mso-style-unhide:no; mso-style-locked:yes; mso-style-link:"标题 2"; mso-ansi-font-size:16.0pt; mso-bidi-font-size:16.0pt; font-family:"Cambria","serif"; mso-ascii-font-family:Cambria; mso-ascii-theme-font:major-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:major-fareast; mso-hansi-font-family:Cambria; mso-hansi-theme-font:major-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:major-bidi; font-weight:bold;} span.3Char {mso-style-name:"标题 3 Char"; mso-style-priority:9; mso-style-unhide:no; mso-style-locked:yes; mso-style-link:"标题 3"; mso-ansi-font-size:16.0pt; mso-bidi-font-size:16.0pt; font-weight:bold;} .MsoChpDefault {mso-style-type:export-only; mso-default-props:yes; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:327371524; mso-list-type:hybrid; mso-list-template-ids:1246772636 67698705 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l0:level1 {mso-level-text:"%1/)"; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:45.0pt; text-indent:-21.0pt;} @list l1 {mso-list-id:1010639450; mso-list-template-ids:67698717;} @list l1:level1 {mso-level-text:%1; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:21.25pt; text-indent:-21.25pt;} @list l1:level2 {mso-level-text:"%1/.%2"; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:49.6pt; text-indent:-1.0cm;} @list l1:level3 {mso-level-text:"%1/.%2/.%3"; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:70.9pt; text-indent:-1.0cm;} @list l1:level4 {mso-level-text:"%1/.%2/.%3/.%4"; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:99.2pt; text-indent:-35.4pt;} @list l1:level5 {mso-level-text:"%1/.%2/.%3/.%4/.%5"; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:127.55pt; text-indent:-42.5pt;} @list l1:level6 {mso-level-text:"%1/.%2/.%3/.%4/.%5/.%6"; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:163.0pt; text-indent:-2.0cm;} @list l1:level7 {mso-level-text:"%1/.%2/.%3/.%4/.%5/.%6/.%7"; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:191.35pt; text-indent:-63.8pt;} @list l1:level8 {mso-level-text:"%1/.%2/.%3/.%4/.%5/.%6/.%7/.%8"; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:219.7pt; text-indent:-70.9pt;} @list l1:level9 {mso-level-text:"%1/.%2/.%3/.%4/.%5/.%6/.%7/.%8/.%9"; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:255.1pt; text-indent:-85.0pt;} @list l2 {mso-list-id:1983578164; mso-list-type:hybrid; mso-list-template-ids:-527251092 67698689 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l2:level1 {mso-level-number-format:bullet; mso-level-text:; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:45.0pt; text-indent:-21.0pt; font-family:Wingdings;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} -->
除了支持消息传递方式 (message) 来同步之外, Photon 也支持脉冲 (pulse) 。一个进程如果仅仅为了通知对方而不需要回复的情况下,可以使用 pulse 。例如,在 server 发送消息可能导致双方互锁 (SEND-blocked) 引起的死锁 (deadlocked) 的情况下,应该使用 pulse 。
一个 Photon pulse 由一个 PtAppAddInput() 中 pid 参数的 PID 标识, PID 对你的应用程序而言是本地的。如果你希望接收另一应用程序发送的 pulse ,你必须使用 PtPulseArm() 函数来“瞄准 (arm) ”对方。该函数创建一个可以通过消息发送给其他进程的 PtPulseMsg_t 对象。这样其他进程就可以通过 MsgDeliverEvent() 函数发送 pulse 。
注意 :
在 QNX Neutrino OS 6 中, PtPulseMsg_t 是一个 sigevent 类型的结构。 msg.sigev_value.sival_int 位对应的 _NOTIFY_COND_MASK 被清除,但发送 pulse 的应用程序可以进行设置。更多内容参见 QNX Neutrino Library Reference 。
PtPulseArm() (Photon Library Reference 中有说明 ) 函数参数中包含一个 sigevent 结构类型变量。 PtPulseArmFd() 和 PtPulseArmPid() 兼容于早期 QNX Neutrino OS 和 Photon microGUI 版本。
1.1 Photon application that receives a pulse
对于 pulse 接收端 ( 我们称为 server) ,需要做如下工作:
1) 创建 pulse 。
2) 瞄准 (arm)pulse.
3) 发送 pulse 消息到发送 pulse 端 ( 我们称之为 client) 。
4) 注册 pulse 处理函数。
5) 如有必要,发送 pulse 到自身。
6) 当 pulse 不需要时删除。
注意 :退出前请通知发送端停止发送 pulse 。
1.1.1 Creating a pulse
调用 PtAppCreatePulse() 函数创建一个 Photon pulse :
pid_t PtAppCreatePulse( PtAppContext_t app,
int priority );
参数列表:
app
PtAppContext_t 结构类型标识的应用程序上下文地址,使用 NULL 表示使用默认上下文。
priority
pulse 优先级,使用 -1 表示调用程序使用的优先级。
PtAppCreatePulse() 返回一个非零整数表示的 pulse process ID 。
1.1.2 Arming a pulse
Arm 一个 pulse 填充一个可以被大部分 QNX Neutrino 调用使用的 sigevent 结构。
注意 :
多个 client 发出相同的 pulse 是正常的,尽管 server 不知道是由哪个进程发出的。
PtPulseArm() 函数原型如下:
int PtPulseArm( PtAppContext_t app,
pid_t pulse,
struct sigevent *msg );
参数如下:
app
当前应用上下文地址。 ( 一般设置为 NULL) 。
pulse
由 PtAppCreatePulse() 创建的 pulse 。
msg
函数创建的 pulse message 指针,我们应该将其传递到 client 。
函数返回 pulse message ID ,后面将会用到。
1.1.3 Sending the pulse message to the deliverer
发送 pulse message 到 client ,如下:
l 对于 resource manager ,使用 ionotify() :
ionotify (fd, _NOTIFY_ACTION_POLLARM, _NOTIFY_COND_INPUT,
&pulsemsg);
l 对于其他类型进程,可以使用 MsgSendv() :
/* Create your own message format: */
msg.pulsemsg = pulsemsg;
MsgSendv (connection_id, &msg, msg_parts, &rmsg, rmsg_parts);
1.1.4 Registering an input handler
注册 pulse 的消息处理与 message 的处理基本相同。将 PtAppCreatePulse() 返回的 pulse ID 作为参数传递到 PtAppAddInput() 即可。
Rcvid 参数没有必要与 pulse ID 相同:在 _NOTIFY_DATA_MASK 位上与 pulse ID 相同 ( 参见 QNX Neutrino Library Reference 中 ionotify() 部分 ) ,但是其余位自 Neutrino pulse 接收。
1.1.5 Delivering a pulse to yourself
如果应用程序需要发送 pulse 到自身,可以使用 PtAppPulseTrigger() 函数:
int PtAppPulseTrigger( PtAppContext_t app,
pid_t pulse );
两个参数分别是应用程序上下文和 PtAppCreatePulse() 返回的 pulse ID 。
1.1.6 Example — message queues
以下是接收 Photon pulse 的例子。该例子打开一个消息队列 (message queue)( 默认使用 /dev/mqueue/testqueue) ,设置一个 pulse ,并且在从消息队列读取到内容时使用 mq_notify() 给自身一个 pulse 。
<!-- /* Font Definitions */ @font-face {font-family:Wingdings; panose-1:5 0 0 0 0 0 0 0 0 0; mso-font-charset:2; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:0 268435456 0 0 -2147483648 0;} @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:Cambria; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:0; mso-generic-font-family:roman; mso-font-pitch:variable; mso-font-signature:-1610611985 1073741899 0 0 159 0;} @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:-1610611985 1073750139 0 0 159 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; text-indent:10.0pt; mso-char-indent-count:2.0; line-height:150%; mso-pagination:none; font-size:12.0pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-font-kerning:1.0pt;} h2 {mso-style-priority:9; mso-style-qformat:yes; mso-style-link:"标题 2 Char"; mso-style-next:正文; margin-top:13.0pt; margin-right:0cm; margin-bottom:13.0pt; margin-left:0cm; text-align:justify; text-justify:inter-ideograph; text-indent:10.0pt; mso-char-indent-count:2.0; line-height:173%; mso-pagination:lines-together; page-break-after:avoid; mso-outline-level:2; font-size:16.0pt; font-family:"Cambria","serif"; mso-ascii-font-family:Cambria; mso-ascii-theme-font:major-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:major-fareast; mso-hansi-font-family:Cambria; mso-hansi-theme-font:major-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:major-bidi; mso-font-kerning:1.0pt;} p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph {mso-style-priority:34; mso-style-unhide:no; mso-style-qformat:yes; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; text-indent:21.0pt; mso-char-indent-count:2.0; line-height:150%; mso-pagination:none; font-size:12.0pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-font-kerning:1.0pt;} span.2Char {mso-style-name:"标题 2 Char"; mso-style-priority:9; mso-style-unhide:no; mso-style-locked:yes; mso-style-link:"标题 2"; mso-ansi-font-size:16.0pt; mso-bidi-font-size:16.0pt; font-family:"Cambria","serif"; mso-ascii-font-family:Cambria; mso-ascii-theme-font:major-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:major-fareast; mso-hansi-font-family:Cambria; mso-hansi-theme-font:major-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:major-bidi; font-weight:bold;} .MsoChpDefault {mso-style-type:export-only; mso-default-props:yes; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:504367954; mso-list-type:hybrid; mso-list-template-ids:122736336 67698689 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l0:level1 {mso-level-number-format:bullet; mso-level-text:; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:45.0pt; text-indent:-21.0pt; font-family:Wingdings;} @list l1 {mso-list-id:1010639450; mso-list-template-ids:67698717;} @list l1:level1 {mso-level-text:%1; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:21.25pt; text-indent:-21.25pt;} @list l1:level2 {mso-level-text:"%1/.%2"; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:49.6pt; text-indent:-1.0cm;} @list l1:level3 {mso-level-text:"%1/.%2/.%3"; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:70.9pt; text-indent:-1.0cm;} @list l1:level4 {mso-level-text:"%1/.%2/.%3/.%4"; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:99.2pt; text-indent:-35.4pt;} @list l1:level5 {mso-level-text:"%1/.%2/.%3/.%4/.%5"; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:127.55pt; text-indent:-42.5pt;} @list l1:level6 {mso-level-text:"%1/.%2/.%3/.%4/.%5/.%6"; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:163.0pt; text-indent:-2.0cm;} @list l1:level7 {mso-level-text:"%1/.%2/.%3/.%4/.%5/.%6/.%7"; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:191.35pt; text-indent:-63.8pt;} @list l1:level8 {mso-level-text:"%1/.%2/.%3/.%4/.%5/.%6/.%7/.%8"; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:219.7pt; text-indent:-70.9pt;} @list l1:level9 {mso-level-text:"%1/.%2/.%3/.%4/.%5/.%6/.%7/.%8/.%9"; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:255.1pt; text-indent:-85.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} -->
1.1 Photon application that delivers a pulse
发送 pulse 的 Photon 应用程序 (Client 端 ) 需要做以下工作:
l 使用一个 input handler 处理 Server 发送的 pulse 消息。使用 input handler 来处理包含 pulse 消息在内的各种消息,并且通知发送方停止发送 pulse 。注:保存消息中的 rcvid 值来发送 pulse ,你可能需要用来发送该 pulse 。
l 调用 MsgDeliverEvent() 发送 pulse 。