C代码
1. /*msgserver.c*/
2.
3. #include <stdlib.h>
4. #include <string.h>
5. #include <errno.h>
6. #include <sys/types.h>
7. #include <sys/ipc.h>
8. #include <sys/msg.h>
9. #include <sys/stat.h>
10.
11. #define MSG_FILE "msgserver.c"
12. #define BUFFER 255
13. #define PERM S_IRUSR|S_IWUSR
14. /* 服务端创建的消息队列最后没有删除,我们要使用ipcrm命令来删除的 */
15. /* ipcrm -q <msqid> */
16.
17. struct msgtype
18. {
19. long mtype;
20. char buffer[BUFFER+1];
21. };
22.
23. int main()
24. {
25. struct msgtype msg;
26. key_t key;
27. int msgid;
28.
29. if((key=ftok(MSG_FILE,'a'))==-1)
30. {
31. fprintf(stderr,"Creat Key Error:%s\n", strerror(errno));
32. exit(1);
33. }
34.
35. if((msgid=msgget(key, PERM|IPC_CREAT|IPC_EXCL))==-1)
36. {
37. fprintf(stderr, "Creat Message Error:%s\n", strerror(errno));
38. exit(1);
39. }
40. printf("msqid = %d\n", msgid);
41. while(1)
42. {
43. msgrcv(msgid, &msg, sizeof(struct msgtype), 1, 0);
44. fprintf(stderr,"Server Receive:%s\n", msg.buffer);
45. msg.mtype = 2;
46. msgsnd(msgid, &msg, sizeof(struct msgtype), 0);
47. }
48. exit(0);
49. }
1. /* msgclient.c */
2.
3. #include <stdio.h>
4. #include <stdlib.h>
5. #include <string.h>
6. #include <errno.h>
7. #include <sys/types.h>
8. #include <sys/ipc.h>
9. #include <sys/msg.h>
10. #include <sys/stat.h>
11.
12. #define MSG_FILE "msgserver.c"
13. #define BUFFER 255
14. #define PERM S_IRUSR|S_IWUSR
15.
16. struct msgtype {
17. long mtype;
18. char buffer[BUFFER+1];
19. };
20.
21. int main(int argc, char **argv)
22. {
23. struct msgtype msg;
24. key_t key;
25. int msgid;
26.
27. if(argc != 2)
28. {
29. fprintf(stderr,"Usage:%s string\n", argv[0]);
30. exit(1);
31. }
32.
33. if((key=ftok(MSG_FILE,'a'))==-1)
34. {
35. fprintf(stderr,"Creat Key Error:%s\n", strerror(errno));
36. exit(1);
37. }
38.
39. if((msgid=msgget(key, PERM))==-1)
40. {
41. fprintf(stderr,"Creat Message Error:%s\n", strerror(errno));
42. exit(1);
43. }
44.
45. msg.mtype = 1;
46. strncpy(msg.buffer, argv[1], BUFFER);
47. msgsnd(msgid, &msg, sizeof(struct msgtype), 0);
48. memset(&msg, '\0', sizeof(struct msgtype));
49. msgrcv(msgid, &msg, sizeof(struct msgtype), 2, 0);
50. fprintf(stderr, "Client receive:%s\n", msg.buffer);
51. exit(0);
52. }
分享到:
相关推荐
非常齐全的消息队列应用实例,每个工程都经过调试并且通过
linux 进程通信-消息队列详解,详细阐述了linux或者unix下面的进程通过消息队列通信
Linux进程间通信-消息队列实例.pdf 学习资料 复习资料 教学资源
linux消息队列linux消息队列linux消息队列linux消息队列linux消息队列进程间通信进程间通信
使用linux消息队列实现进程间双向通信。本接口将消息接收封装在一个独立线程中,方便使用。
实现linux两不同进程用消息队列通信 还不错 !
【linux学习笔记-1】使用GDB调试简单的用户程序 【linux学习笔记-2】父子进程...【linux学习笔记--17】POSIX IPC——消息队列 【linux学习笔记--18】POSIX IPC——信号量 【linux学习笔记--19】POSIX IPC——共享内存
Linux进程间通信之消息队列.适用于任意两个进程间,本程序为基本模型,实现了消息队列.
【linux学习笔记--17】POSIX IPC——消息队列.doc 【linux学习笔记--18】POSIX IPC——信号量.doc 【linux学习笔记--19】POSIX IPC——共享内存.doc 【linux学习笔记-10】Linux进程相关系统调用(三).doc 【linux...
详细讲解了管道、信号、 消息队列、 信号灯 、共享内存、 套接口。
本资源中含有有关LINUX进程通信的一些文章,从详细说明了信号量、管道以及消息队列等方式介绍。
Linux系统具有高效的网络功能和稳定的性能,因此被广泛应用于服务器领域,Linux是云计算的核心组成部分,被广泛用于构建云平台和云服务。许多知名的云计算服务提供商都采用Linux系统作为其基础架构,一些游戏平台和...
linux c 多进程间的实例,通信使用共享内存和消息队列
利用消息队列基本函数,实现Linux系统下进程A和进程B之间的消息收发。 msgsend输入‘end’,退出程序
资源中包含了Linux进程间通信的例子,同时有源文件和可执行文件。 源码主要包含了Linux下IPC机制的本地进程通信方式,包含了IPC共享内存,IPC信号量,IPC消息队列的实现,以及Linux下判断进程退出原因的示例程序。
linux c 进程间通信 消息队列 代码 亲测可用
linux下C语言进程通信--消息列队,可用于多个进程之间通信,也可在一个进程内部当作队列来使用,包含了3个demo,使用make编译,得到send发送端,recv1接收端1,recv1接收端2这3个可执行文件,运行就可以了
Linux进程间的通信实现方式,比如管道、消息队列等
Linux操作系统下C语言的进程通信、消息队列
消息队列在Linux线程或进程间通信中的应用.pdf