加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 黄冈站长网 (http://www.0713zz.com/)- 数据应用、建站、人体识别、智能机器人、语音技术!
当前位置: 首页 > 服务器 > 搭建环境 > Windows > 正文

Linux下的进程间通信:使用管道和消息队列

发布时间:2019-05-14 00:28:20 所属栏目:Windows 来源:Marty Kalin
导读:副标题#e# 学习在 Linux 中进程是如何与其他进程进行同步的。 本篇是 Linux 下进程间通信(IPC)系列的第二篇文章。第一篇文章 聚焦于通过共享文件和共享内存段这样的共享存储来进行 IPC。这篇文件的重点将转向管道,它是连接需要通信的进程之间的通道。管

上面的头文件定义了一个名为 queuedMessage 的结构类型,它带有 payload(字节数组)和 type(整数)这两个域。该文件也定义了一些符号常数(使用 #define 语句),前两个常数被用来生成一个 key,而这个 key 反过来被用来获取一个消息队列的 ID。ProjectId 可以是任何正整数值,而 PathName 必须是一个存在的、可访问的文件,在这个示例中,指的是文件 queue.h。在 senderreceiver 中,它们都有的设定语句为:

  1. key_t key = ftok(PathName, ProjectId); /* generate key */
  2. int qid = msgget(key, 0666 | IPC_CREAT); /* use key to get queue id */

ID qid 在效果上是消息队列文件描述符的对应物。

示例 5. sender 程序

  1. #include <stdio.h>
  2. #include <sys/ipc.h>
  3. #include <sys/msg.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "queue.h"
  7.  
  8. void report_and_exit(const char* msg) {
  9. [perror][6](msg);
  10. [exit][7](-1); /* EXIT_FAILURE */
  11. }
  12.  
  13. int main() {
  14. key_t key = ftok(PathName, ProjectId);
  15. if (key < 0) report_and_exit("couldn't get key...");
  16. int qid = msgget(key, 0666 | IPC_CREAT);
  17. if (qid < 0) report_and_exit("couldn't get queue id...");
  18.  
  19. char* payloads[] = {"msg1", "msg2", "msg3", "msg4", "msg5", "msg6"};
  20. int types[] = {1, 1, 2, 2, 3, 3}; /* each must be > 0 */
  21. int i;
  22. for (i = 0; i < MsgCount; i++) {
  23. /* build the message */
  24. queuedMessage msg;
  25. msg.type = types[i];
  26. [strcpy][11](msg.payload, payloads[i]);
  27.  
  28. /* send the message */
  29. msgsnd(qid, &msg, sizeof(msg), IPC_NOWAIT); /* don't block */
  30. [printf][10]("%s sent as type %in", msg.payload, (int) msg.type);
  31. }
  32. return 0;
  33. }

上面的 sender 程序将发送出 6 个消息,每两个为一个类型:前两个是类型 1,接着的连个是类型 2,最后的两个为类型 3。发送的语句:

  1. msgsnd(qid, &msg, sizeof(msg), IPC_NOWAIT);

被配置为非阻塞的(IPC_NOWAIT 标志),是因为这里的消息体量上都很小。唯一的危险在于一个完整的序列将可能导致发送失败,而这个例子不会。下面的 receiver 程序也将使用 IPC_NOWAIT 标志来接收消息。

示例 6. receiver 程序

  1. #include <stdio.h>
  2. #include <sys/ipc.h>
  3. #include <sys/msg.h>
  4. #include <stdlib.h>
  5. #include "queue.h"
  6.  
  7. void report_and_exit(const char* msg) {
  8. [perror][6](msg);
  9. [exit][7](-1); /* EXIT_FAILURE */
  10. }
  11. int main() {
  12. key_t key= ftok(PathName, ProjectId); /* key to identify the queue */
  13. if (key < 0) report_and_exit("key not gotten...");
  14. int qid = msgget(key, 0666 | IPC_CREAT); /* access if created already */
  15. if (qid < 0) report_and_exit("no access to queue...");
  16.  
  17. int types[] = {3, 1, 2, 1, 3, 2}; /* different than in sender */
  18. int i;
  19. for (i = 0; i < MsgCount; i++) {
  20. queuedMessage msg; /* defined in queue.h */
  21. if (msgrcv(qid, &msg, sizeof(msg), types[i], MSG_NOERROR | IPC_NOWAIT) < 0)
  22. [puts][12]("msgrcv trouble...");
  23. [printf][10]("%s received as type %in", msg.payload, (int) msg.type);
  24. }
  25.  
  26. /** remove the queue **/
  27. if (msgctl(qid, IPC_RMID, NULL) < 0) /* NULL = 'no flags' */
  28. report_and_exit("trouble removing queue...");
  29. return 0;
  30. }

(编辑:PHP编程网 - 黄冈站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读