linux 消息序列(进程间通信)
来源:互联网 发布:淘一兔淘宝信誉查询 编辑:程序博客网 时间:2024/06/09 14:47
linux 消息序列(进程间通信)
分类: 文章2012-09-13 00:00 108人阅读 评论(0) 收藏 举报
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- //需要自己定义的消息队列结构
- struct msgStuct
- {
- long int msgType;
- char strMsg[1024];
- };
- int
- main ()
- {
- struct msgStuct msg_data;
- int msgid;
- char buffer[1024];
- //创建一个消息队列
- if ((msgid= msgget ((key_t) 2234, 0666 | IPC_CREAT)) == -1)
- {
- perror ("msgget failed with error: %d\n");
- exit (EXIT_FAILURE);
- }
- while (1)
- {
- printf ("Send message: ");
- fgets (buffer, 1024, stdin);
- //初始化消息类型
- msg_data.msgType = 1;
- strcpy (msg_data.strMsg, buffer);
- //发送消息
- if (msgsnd (msgid, (void *) &msg_data, 1024, 0) == -1)
- {
- fprintf (stderr, "msgsnd failed\n");
- exit (EXIT_FAILURE);
- }
- if (strncmp (buffer, "end", 3) == 0)
- {
- break;
- }
- }
- exit (EXIT_SUCCESS);
- }
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- //需要自己定义的消息队列结构
- struct msgStuct
- {
- long int msgType;
- char strMsg[1024];
- };
- int
- main ()
- {
- int msgid;
- struct msgStuct msg_data;
- //接收消息优先级
- long int msgPriority = 0;//从队列中取第一个
- //创建一个消息队列
- if ((msgid= msgget ((key_t) 2234, 0666 | IPC_CREAT)) == -1)//类似open()创建一个文件返回它的文件描述符,这里是消息序列
- {
- perror ("msgget failed with error");
- exit (EXIT_FAILURE);
- }
- while (1)
- {
- //接收消息
- if (msgrcv (msgid, (void *) &msg_data, 1024,
- msgPriority, 0) == -1)
- {
- perror ("msgrcv failed with error");
- exit (EXIT_FAILURE);
- }
- printf ("Received message: %s", msg_data.strMsg);
- if (strncmp (msg_data.strMsg, "end", 3) == 0)
- {
- break;
- }
- }
- //删除消息队列
- if (msgctl (msgid, IPC_RMID, 0) == -1)
- {
- fprintf (stderr, "delete messagequeue error\n");
- exit (EXIT_FAILURE);
- }
- exit (EXIT_SUCCESS);
- }
第一个是send.c,第二个是recieve.c
- linux 消息序列(进程间通信)
- linux 消息序列(进程间通信)
- linux 消息序列(进程间通信)
- Linux进程间通信(消息队列)
- Linux进程间通信(简单的消息队列通信)
- linux进程通信-消息
- Linux环境进程间通信(三) (消息队列)
- Linux环境进程间通信(三):消息队列
- Linux环境进程间通信系列(三):消息队列
- Linux环境进程间通信(三)消息队列
- Linux环境进程间通信(三):消息队列
- Linux环境进程间通信(三)--- 消息队列
- Linux环境进程间通信系列(三):消息队列
- Linux环境进程间通信(三):消息队列
- Linux环境进程间通信(三):消息队列
- Linux环境进程间通信(三):消息队列
- Linux环境进程间通信(三):消息队列
- linux进程间通信(posix消息队列)实例
- centos 挂载 windows 中文目录
- oracle 如何增加一天,增加一小时,增加一分钟
- 如何修改NavigationController的后退按钮标题
- 无线网络-LTE (05) 通信过程之 S1 Release procedure
- 基于openCV的png读取透明图片
- linux 消息序列(进程间通信)
- linux 进程调度
- UVA 10189
- servlet小结~面试用
- Android Camera(三)
- Ajax 通过XMLHttpRequest 对象完成首页的登录功能
- CPU卡的线路加密与线路保护(附源码)
- 今生,不再许你那一世的春暖花开
- android http