linux 环境下的进程间的通信——消息队列传输结构体
2012-05-15 14:00
447 查看
linux 环境下的进程间的通信方式主要有:管道,有名和无名管道, 这种方式适用于具有亲缘关系的进程之间的通信;
信号:
消息队列:
共享内存:
信号量:
套接字:
这次主要涉及消息队列:
1. 需要用到的函数:
1)得到消息队列的描述子
#include <sys/types.h>
#include <sys/ipc.h>
key_t ftok(const char *pathname, int proj_id);
使用方法:pathname,一般是一个带路径的文件的名字,常取做 “.”
proj_id 这个是有自己设定的。
通过这两个参数确定一个key值,然后用于msgget函数:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int msgget(key_t key, int msgflg);
使用方法:key 来自上面的函数, msgflg 通常的用法与read中的相似,一个进程中:在创建的时候,我们使用IPC_CREAT, 另一个进程:在使用的时候一般采用msgflg的值为0;
2)消息的格式:
struct msbuf 一般具有两个部分,一个部分是 msgtype, 另一个是msgname消息队列的内容,我们一般采用消息队列传输具有一定结构类型的数据;现在假设需要传送的结构为:
typedef struct _S{
int a;
int b;
int c;
}TS;
下面是我们的消息结构:
struct msbuf{
long msg_type;
TS msg_name;
}
3)发送消息和接收消息:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,int msgflg);
使用方法:① int msqid 这是通过上面的两个函数得到的。
②const void *msgop; 只需要在此处填入自己构造的消息的指针即可。此处使用 msbuf *;
③size_t msgsz 这个指的是消息的大小,这个不包括long type,我们在此处使用sizeof(TS);
④ int msgflg 可以为以下:IPC_CREAT、IPC_EXCL、IPC_NOWAIT或三者的或结果。
⑤ long msgtyp, 指明要结构的消息的类型;
范例程序:
// this is ipc write !
#include<stdio.h>
#include<errno.h>
#include
<sys/types.h>
#include
<sys/ipc.h>
#include
<sys/msg.h>
#define BUFLENGTH
10
typedef
struct _S{
int a;
int b;
}TS;
struct msbuf
{
long msg_type;
TS msg_name;
};
int main()
{
struct msbuf student;
TS ts;
key_t key;
int msgid;
int send;
key=ftok(".",3);
ts.a=11;
ts.b=12;
student.msg_type =
1;
student.msg_name = ts;
if(key<0)
{
perror("It is a wrong key\n");
}
msgid=msgget(key,IPC_CREAT);
send=msgsnd(msgid,&student,sizeof(ts),0);
if(send<0)
{
perror("write wrong!\n");
}
return
0;
}
// this is ipc read!
#include<stdio.h>
#include<errno.h>
#include
<sys/types.h>
#include
<sys/ipc.h>
#include
<sys/msg.h>
#define BUFLENGTH
10
typedef
struct _S{
int a;
int b;
}TS;
struct msbuf
{
long msg_type;
TS msg_name;
};
int main()
{
struct msbuf rc;
key_t key;
int msgid;
int recive;
key=ftok(".",3);
if(key<0)
{
perror("It is a wrong key\n");
}
msgid=msgget(key,0);
printf("start rece \n");
recive=msgrcv(msgid,&rc,sizeof(TS),1,0);
if(recive<0)
{
perror("read wrong!\n");
}
else
{
printf("rece \n");
printf("read is :%d\n",rc.msg_name.a);
printf("read is :%d\n",rc.msg_name.b);
}
return
0;
}
信号:
消息队列:
共享内存:
信号量:
套接字:
这次主要涉及消息队列:
1. 需要用到的函数:
1)得到消息队列的描述子
#include <sys/types.h>
#include <sys/ipc.h>
key_t ftok(const char *pathname, int proj_id);
使用方法:pathname,一般是一个带路径的文件的名字,常取做 “.”
proj_id 这个是有自己设定的。
通过这两个参数确定一个key值,然后用于msgget函数:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int msgget(key_t key, int msgflg);
使用方法:key 来自上面的函数, msgflg 通常的用法与read中的相似,一个进程中:在创建的时候,我们使用IPC_CREAT, 另一个进程:在使用的时候一般采用msgflg的值为0;
2)消息的格式:
struct msbuf 一般具有两个部分,一个部分是 msgtype, 另一个是msgname消息队列的内容,我们一般采用消息队列传输具有一定结构类型的数据;现在假设需要传送的结构为:
typedef struct _S{
int a;
int b;
int c;
}TS;
下面是我们的消息结构:
struct msbuf{
long msg_type;
TS msg_name;
}
3)发送消息和接收消息:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,int msgflg);
使用方法:① int msqid 这是通过上面的两个函数得到的。
②const void *msgop; 只需要在此处填入自己构造的消息的指针即可。此处使用 msbuf *;
③size_t msgsz 这个指的是消息的大小,这个不包括long type,我们在此处使用sizeof(TS);
④ int msgflg 可以为以下:IPC_CREAT、IPC_EXCL、IPC_NOWAIT或三者的或结果。
⑤ long msgtyp, 指明要结构的消息的类型;
范例程序:
// this is ipc write !
#include<stdio.h>
#include<errno.h>
#include
<sys/types.h>
#include
<sys/ipc.h>
#include
<sys/msg.h>
#define BUFLENGTH
10
typedef
struct _S{
int a;
int b;
}TS;
struct msbuf
{
long msg_type;
TS msg_name;
};
int main()
{
struct msbuf student;
TS ts;
key_t key;
int msgid;
int send;
key=ftok(".",3);
ts.a=11;
ts.b=12;
student.msg_type =
1;
student.msg_name = ts;
if(key<0)
{
perror("It is a wrong key\n");
}
msgid=msgget(key,IPC_CREAT);
send=msgsnd(msgid,&student,sizeof(ts),0);
if(send<0)
{
perror("write wrong!\n");
}
return
0;
}
// this is ipc read!
#include<stdio.h>
#include<errno.h>
#include
<sys/types.h>
#include
<sys/ipc.h>
#include
<sys/msg.h>
#define BUFLENGTH
10
typedef
struct _S{
int a;
int b;
}TS;
struct msbuf
{
long msg_type;
TS msg_name;
};
int main()
{
struct msbuf rc;
key_t key;
int msgid;
int recive;
key=ftok(".",3);
if(key<0)
{
perror("It is a wrong key\n");
}
msgid=msgget(key,0);
printf("start rece \n");
recive=msgrcv(msgid,&rc,sizeof(TS),1,0);
if(recive<0)
{
perror("read wrong!\n");
}
else
{
printf("rece \n");
printf("read is :%d\n",rc.msg_name.a);
printf("read is :%d\n",rc.msg_name.b);
}
return
0;
}
相关文章推荐
- 【linux高级环境编程学习笔记四】消息队列进程通信
- Linux进程通信 消息队列
- Linux进程通信之POSIX消息队列
- Linux进程间的通信方式:运用消息队列通信
- Linux消息队列进程通信的介绍
- linux进程通信:使用posix消息队列mq进行线程或进程间的通信
- Linux下进程通信之消息队列
- linux进程通信之消息队列
- Linux进程间的通信--消息队列
- linux基础编程:进程通信之System V IPC:消息队列,信号量,共享内存
- Linux——基于共享内存 消息队列和基于Socket的进程间的通信
- Linux C程序练习(3)进程通信之pipe、fifo、消息队列
- Linux进程通信之System V消息队列
- linux编程---进程通信---消息队列
- Unix/Linux C++应用开发-进程通信消息队列
- Linux进程通信之POSIX消息队列
- Linux 进程通信之 消息队列
- Linux进程通信之消息队列
- linux c++进程通信之消息队列
- Linux — IPC进程通信之消息队列详解