当前位置: 技术问答>linux和unix
linux两个进程通过消息队列互发信息
来源: 互联网 发布时间:2017-03-28
本文导语: #include #include #include #include #include #include #include #include struct my_msg_st { long int my_msg_type; unsigned char msg_data_buf[36]; }; int main(void) { int i; int running=1; int rtn_rcv; int msg_rcv_id; int msg_snd_id; key_t m...
#include
#include
#include
#include
#include
#include
#include
#include
struct my_msg_st
{
long int my_msg_type;
unsigned char msg_data_buf[36];
};
int main(void)
{
int i;
int running=1;
int rtn_rcv;
int msg_rcv_id;
int msg_snd_id;
key_t msg_snd_key;
key_t msg_rcv_key;
struct my_msg_st data_msg1;
long int msg_to_receive=0;
msg_snd_key = ftok("/tmp/TTMS/GM", 'b');
msg_rcv_key = ftok("/tmp/TTMS/GM", 'a');
/*创建消息队列*/
msg_rcv_id=msgget(msg_rcv_key,0666 | IPC_CREAT);
msg_snd_id=msgget(msg_snd_key,0666 | IPC_CREAT);
if(msg_rcv_id==-1)
{
fprintf(stderr,"msgget failed with error: %dn",errno);
exit(EXIT_FAILURE);
}
printf("the message1 rcv id:%dn", msg_rcv_id);
if(msg_snd_id==-1)
{
fprintf(stderr,"msgget failed with error: %dn",errno);
exit(EXIT_FAILURE);
}
printf("the message1 snd id:%dn", msg_snd_id);
for (i=0; i
#include
#include
#include
#include
#include
#include
#include
struct my_msg_st
{
long int my_msg_type;
unsigned char msg_data_buf[36];
};
int main(void)
{
int i;
int running=1;
int rtn_rcv;
int msg_rcv_id;
int msg_snd_id;
key_t msg_snd_key;
key_t msg_rcv_key;
struct my_msg_st data_msg1;
long int msg_to_receive=0;
msg_snd_key = ftok("/tmp/TTMS/GM", 'b');
msg_rcv_key = ftok("/tmp/TTMS/GM", 'a');
/*创建消息队列*/
msg_rcv_id=msgget(msg_rcv_key,0666 | IPC_CREAT);
msg_snd_id=msgget(msg_snd_key,0666 | IPC_CREAT);
if(msg_rcv_id==-1)
{
fprintf(stderr,"msgget failed with error: %dn",errno);
exit(EXIT_FAILURE);
}
printf("the message1 rcv id:%dn", msg_rcv_id);
if(msg_snd_id==-1)
{
fprintf(stderr,"msgget failed with error: %dn",errno);
exit(EXIT_FAILURE);
}
printf("the message1 snd id:%dn", msg_snd_id);
for (i=0; i