#include #define SIZE 1024
const long id = 1000;//这里规定类型为1000的消息
typedef struct msgqueue{
long mytype;//消息类型
char mytext[SIZE];//消息内容
}msgqueue;
int main(){
key_t k = ftok(".",0x1234);//申请key,确保两个需要通信的进程使用同一个key值获取到的队列id
if(k < 0){
perror("ftok");
return -1;
}
int msgid = msgget(k,IPC_CREAT|IPC_EXCL|0666);//使用key值获取id
if(msgid < 0){
perror("msgget");
return -1;
}
pid_t pid = fork();
if(pid < 0){
perror("fork");
return -1;
}
if(pid > 0){//父进程发消息
msgqueue queue;
queue.mytype = id;//发送的消息类型为1000
char msg[SIZE];
while(1){//循环输入,此时输入的每一条消息,消息类型都是1000
printf("father says# ");
//