进程通信之消息队列
2017-08-05 17:08
183 查看
#include <sys/types.h>
#include <sys/msg.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
struct msg_buf
{
int mtype;
char data[255];
};
int main()
{
key_t key;
int msgid;
int ret;
struct msg_buf msgbuf;
key = ftok("123456", 'a');
printf ("key = {%x}\n", key);
// 创建或获取消息队列
msgid = msgget(key, IPC_CREAT |0666);
if (msgid == -1)
{
printf ("create error!\n");
return -1;
}
// 设置消息类型
msgbuf.mtype = 2;
// 填写消息
strcpy(msgbuf.data, "test data");
// 发送消息
ret = msgsnd(msgid, &msgbuf, sizeof(msgbuf.data), IPC_NOWAIT);
if (ret == -1)
{
printf ("send message err\n");
return -1;
}
return 0;
}
#include <sys/msg.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
struct msg_buf
{
int mtype;
char data[255];
};
int main()
{
key_t key;
int msgid;
int ret;
struct msg_buf msgbuf;
key = ftok("123456", 'a');
printf ("key = {%x}\n", key);
// 创建或获取消息队列
msgid = msgget(key, IPC_CREAT |0666);
if (msgid == -1)
{
printf ("create error!\n");
return -1;
}
// 设置消息类型
msgbuf.mtype = 2;
// 填写消息
strcpy(msgbuf.data, "test data");
// 发送消息
ret = msgsnd(msgid, &msgbuf, sizeof(msgbuf.data), IPC_NOWAIT);
if (ret == -1)
{
printf ("send message err\n");
return -1;
}
return 0;
}
相关文章推荐
- 【进程通信】消息队列简单例子
- 消息队列不同进程之间通信
- [转]Linux进程通信之POSIX消息队列
- linux进程通信---消息队列
- linux基础编程:进程通信之System V IPC:消息队列,信号量,共享内存
- 进程通信消息队列代码及一些注意点
- 关于进程通信的消息队列相关函数
- linux进程通信---消息队列
- 多进程编程:Perl与C进程间的消息队列通信
- 进程通信消息队列(IBM)
- php-通过共享内存实现消息队列和进程通信
- 进程通信程序整理---消息队列(Linux)
- 父子进程通信的两种方式--消息队列和共享内存
- 进程通信(消息队列)
- linux进程通信-消息队列
- 【网络编程基础】Linux下进程通信方式(共享内存,管道,消息队列,Socket)
- 【C语言】【unix c】两个进程通过消息队列实现进程间的通信
- 操作系统 进程间的通信 之 信号 消息队列 共享内存 浅析
- Linux 进程通信之 消息队列
- linux消息队列进程通信