///// send
#include<stdio.h>
#include<stdlib.h>
#include<
string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#define MAX_TEXT 1024
struct my_msg_st
{
long int my_msg_type;
char some_text[MAX_TEXT];
};
int main(
void)
{
struct my_msg_st data;
int msgid;
char buffer[MAX_TEXT];
if(-
1==(msgid=msgget((key_t)
1010,
0666 |
IPC_CREAT)))
{
printf("msgget error!\n");
exit(0);
}
while(
1)
{
printf("Enter Message:");
gets(buffer);
data.my_msg_type=
1;
strcpy(data.some_text,buffer);
if(-
1==(msgsnd(msgid,(
void*)&data,MAX_TEXT,
0)))
{
printf("message send error\n");
exit(0);
}
if(strcmp(buffer,
"end")==
0)
break;
}
return 0;
}
/// receive
#include<stdio.h>
#include<stdlib.h>
#include<
string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#define MAX_TEXT 1024
struct my_msg_st
{
long int my_msg_type;
char some_text[MAX_TEXT];
};
int main(
void)
{
struct my_msg_st data;
int msgid;
long int msg_to_receive=
0;
if((msgid=msgget((key_t)
1010,
0666 | IPC_CREAT))==-
1)
{
printf("msgget error!\n");
exit(0);
}
while(
1)
{
if(-
1==msgrcv(msgid,(
void*)&data,MAX_TEXT,msg_to_receive,
0))
{
printf("receive error\n");
exit(0);
}
printf("MSG:%s\n",data.some_text);
if(strcmp(data.some_text,
"end")==
0)
break;
}
return 0;
}
转载于:https://www.cnblogs.com/fmonlyg/p/9230409.html