讲解Unix 消息队列应用

系统 其他OS
文章中,我们会以举例说明一个Unix 消息队列在 C/S 模式中的应用来讲解一下关于Unix 消息队列的应用知识。希望大家能够提高自己的技术。

今天,我们来学习一下Unix 消息队列的知识,首先,我们下面举例说明一个Unix 消息队列在 C/S 模式中的应用。 C/S 的一个重要特性就是非对等性, 多个客户机同时向一个服务器提出不同的请求 , 服务器接收并处理客户机的请求后 , 将结果返回给相应的客户机。在这里 , 我们利用两个Unix 消息队列、一个服务进程、若干客户进程来模拟客户 / 服务应用。

由各客户进程将请求信息写入队列 1( 键值 0x16), 消息类型定为 10; 服务进程从队列 1 中读取类型为 10 的消息进行处理 , 并将处理结果放入Unix 消息队列 2( 键值0x17), 将类型置为原客户进程号 , 这样每个客户进程可根据其中的消息类型即进程号再从队列 2 中读出属于自己的结果 。

具体操作时先启动服务进程 (server.c), 创建两个Unix 消息队列 , 键值分别为十六进制 16和 17; 这时再启动若干客户进程 (client.c), 输入一任意字符串 , 这时服务进程则显示客户进程号和原字符串 , 客户进程则回显***个字符被改为 "_" 的处理后的字符串和消息类型( 自身进程号 ) 。如输入字符 "q", 则删除原Unix 消息队列 , 同时退出。源程序清单附后 , 该程序在 UNIX3.2 、 4.2 环境下编译调试通过。

这只是进程通信在机器内部的实现 , 要实现进程在网络机器间的通信还要借助 UNIX 网际系统的套接字来实现。

源程序清单如下 :
 

  1. 服务进程 :(server.c)   
  2. #include <stdio.h>   
  3. #include <sys/types.h>   
  4. #include <sys/ipc.h>   
  5. #include <sys/msg.h>   
  6. #define KEY16 (key_t)16   
  7. #define KEY17 (key_t)17   
  8. #include (ctype.h)   
  9. int i,msgid1,msgid2,val;   
  10. char *s   
  11. struct{   
  12. long mtype;   
  13. short stype;   
  14. char mtext[40];   
  15. }buf;   
  16. main()   
  17. {   
  18.  
  19. cr_q(); /* 创建消息队列 */   
  20. /* 读消息队列 1, 消息队列类型为 10, 等待客户请求 */   
  21. while   (msgrcv(msgid1,&buf,42,(long)10, ~ IPC_NOWAIT)>=0)   
  22. {printf("\n text from client is:[%s]",buf.mtext);   
  23.  printf ("client pid is <%d> \n",buf.stype);   
  24.  process();/* 将处理结果写入消息队列 2*/   
  25. }   
  26. }   
  27. cr_q()   
  28. {  msgid1=msgget(KEY16,IPC_CREAT|0666);   
  29. if (msgid1<0) {perror("key16 msgget failed");exit(1);}   
  30. msgid2=msgget(KEY17,IPC_CREAT|0666);   
  31. if (msgid2<0) {perror("key17 msgget failed");exit(1);}   
  32. }   
  33. process()   
  34. {   
  35. bufbuf.mtype=buf.stype;/* 原客户进程号 */   
  36. buf.mtext[0]= ‘ _ ’ ;   
  37. if(msgsnd(msgid2,&buf,42,IPC_NOWAIT)<0)   
  38. {perror("key17 msgsnd failed");exit(1);}   
  39. }   

 

  1. 客户进程 :(client.c)   
  2. #include <stdio.h>   
  3. #include <sys/types.h>   
  4. #include <sys/ipc.h>   
  5. #include <sys/msg.h>   
  6. define KEY16 (key_t)16   
  7. define KEY17 (key_t)17   
  8. include <ctype.h>   
  9. int msgid1,msgid2;   
  10. struct{   
  11. long mtype;   
  12. short stype;   
  13. char mtext[40];   
  14. }buf;   
  15. char s[41];   
  16. int clean()   
  17. main()   
  18. {   
  19. get_q();/* 取消队列标志符 */   
  20. while (1)   
  21. {   
  22. printf ("\n @ input mtext:");/* 输入字符串 */   
  23. scanf("%s",s);   
  24. if ((strcmp(s,"q")==0)) break;   
  25. strcpy(buf.mtext,s);   
  26. buf.mtype=10; /* 消息类型置为 10*/   
  27. buf.stype=getpid();/* 客户进程号 */   
  28. /* 写消息队列 1, 向服务进程提出请求 */   
  29. if (msgsnd(msgid1,&buf,40, ~ IPC_NOWAIT)<0)   
  30. {perror("key16 msgsnd failed");exit(1);}   
  31. /* 读消息队列 2, 类型为自身进程号 , 接收处理结果 */   
  32. if (msgrcv(msgid2,&buf,42,getpid(), ~ IPC_NOWAIT)<0)   
  33.  {perror("key17 msgrcv failed");exit(1);}   
  34. printf("\n the answer from server is:[%s]",buf.mtext);   
  35.  printf("mtype is:[%d]",buf.mtype);   
  36.  }   
  37. clean();/* 删除消息队列 */   
  38. }   
  39. clean()   
  40. {   
  41. if(msgct1(msgid1,IPC_RMID,(struct msgid*)NULL)<0)   
  42. {perror("key16 msgct1 failed");exit(1);}   
  43. if(msgct1(msgid2,IPC_RMID,(struct msgid*)NULL<0)   
  44. {perror("key17 msgct1 failed");exit(1);}   
  45. printf("msg queue removed\n");   
  46. exit(0);   
  47. }   
  48. get_q()   
  49. {   
  50. msgid1=msgget(KEY16,0);   
  51. if (msgid1<0) {perror("key16 msgget failed");exit(1);}   
  52. msgid2=msgget(KEY17,0);if (msgid2<0) {perror("key17 msgget failed");exit(1);}   
  53. }  

关于Unix 消息队列的应用,我们今天就讲解到这里啦,希望大家能够好好的学习,我们会带来更多的Unix 消息队列应用的知识。

【编辑推荐】

  1. Linux多线程同步之消息队列
  2. 详解Unix消息队列知识
  3. VB.NET消息队列相关内容详细介绍
  4. WCF消息队列系列介绍
  5. 说明WCF消息队列具体问题
责任编辑:小霞
相关推荐

2010-04-21 12:39:48

Unix 消息队列

2010-04-21 14:49:13

Unix消息队列

2010-04-13 17:00:43

Unix消息队列

2010-04-21 14:39:59

Unix消息队列

2018-04-26 15:18:49

RTOS应用MPU

2017-10-11 15:08:28

消息队列常见

2010-04-21 15:20:31

Unix线程

2010-05-05 17:30:04

Unix MBB

2010-05-05 13:13:55

Unix内核

2010-05-04 11:59:39

Unix系统

2010-05-05 17:41:03

IBM Unix

2010-05-05 10:19:51

Unix系统

2010-05-05 16:05:36

Unix cfengi

2010-04-30 13:27:26

Unix cronta

2010-05-05 13:45:21

Unix Telnet

2010-04-30 13:38:51

Unix at命令

2010-05-04 09:22:10

Unix文件

2010-05-04 12:25:28

Unix链接

2010-02-26 13:40:28

WCF消息头

2010-05-04 16:33:39

Unix系统
点赞
收藏

51CTO技术栈公众号