一、思维导图
二、练习
使用消息队列完成两个进程之间相互通信
//A进程
#include<myhead.h>
//要发送的消息类型
struct msgbuf
{
long mtype;
char mtext[1024];
};
#define SIZE sizeof(struct msgbuf)-sizeof(long)
int main(int argc, const char *argv[])
{
//1、创建key,用于创建消息队列
key_t key = ftok("/",'c');
if(key == -1)
{
perror("ftok error");
return -1;
}
//2、通过key创建消息队列
int msqid = msgget(key,IPC_CREAT|0664);
if(msqid == -1)
{
perror("msgget error");
return -1;
}
//创建子进程
pid_t pid = fork();
if(pid == -1)
{
perror("A fork error");
return -1;
}
if(pid > 0)
{
//父进程,向消息队列中存入数据
//3、向消息队列中存入消息
struct msgbuf buf;
while(1)
{
//设置消息类型
buf.mtype = 1; //A向B发送的消息类型
// printf("请输入消息内容:");
fgets(buf.mtext,SIZE,stdin);
buf.mtext[strlen(buf.mtext)- 1] = 0; //将换行换成'\0'
//将消息存入消息队列中
msgsnd(msqid,&buf,SIZE,0);
//判断退出标志
if(strcmp(buf.mtext,"quit") == 0)
{
//当向对面发送退出标志时,同时向自己的子进程发送退出标志
//设置消息类型
buf.mtype = 2; //A向A发送的消息类型
strcpy(buf.mtext,"quit");
//将消息存入消息队列中
msgsnd(msqid,&buf,SIZE,0);
break;
}
}
}
else if(pid == 0)
{
//子进程,从消息队列中取出数据
//3、从消息队列中取出数据
struct msgbuf buf;
while(1)
{
//指定读取消息类型为2,以阻塞的形式读取信息
msgrcv(msqid,&buf,SIZE,2,0);
//判断退出标志
if(strcmp(buf.mtext,"quit") == 0)
{
break;
}
printf("B:%s\n",buf.mtext);
}
//退出子进程
exit(EXIT_SUCCESS);
}
//非阻塞回收子进程资源
waitpid(-1,NULL,WNOHANG);
return 0;
}
//B进程
#include<myhead.h>
//要发送的消息类型
struct msgbuf
{
long mtype;
char mtext[1024];
};
#define SIZE sizeof(struct msgbuf)-sizeof(long)
int main(int argc, const char *argv[])
{
//1、创建key,用于创建消息队列
key_t key = ftok("/",'c');
if(key == -1)
{
perror("ftok error");
return -1;
}
//2、通过key创建消息队列
int msqid = msgget(key,IPC_CREAT|0664);
if(msqid == -1)
{
perror("msgget error");
return -1;
}
//创建子进程
pid_t pid = fork();
if(pid == -1)
{
perror("A fork error");
return -1;
}
if(pid > 0)
{
//父进程,向消息队列中存入数据
//3、向消息队列中存入消息
struct msgbuf buf;
while(1)
{
//设置消息类型
buf.mtype = 2; //B向A发送的消息类型
// printf("请输入消息内容:");
fgets(buf.mtext,SIZE,stdin);
buf.mtext[strlen(buf.mtext)- 1] = 0; //将换行换成'\0'
//将消息存入消息队列中
msgsnd(msqid,&buf,SIZE,0);
//判断退出标志
if(strcmp(buf.mtext,"quit") == 0)
{
//当向对面发送退出标志时,同时向自己的子进程发送退出标志
//设置消息类型
buf.mtype = 1; //B向B发送的消息类型
strcpy(buf.mtext,"quit");
//将消息存入消息队列中
msgsnd(msqid,&buf,SIZE,0);
break;
}
}
}
else if(pid == 0)
{
//子进程,从消息队列中取出数据
//3、从消息队列中取出数据
struct msgbuf buf;
while(1)
{
//指定读取消息类型为1,以阻塞的形式读取信息
msgrcv(msqid,&buf,SIZE,1,0);
//判断退出标志
if(strcmp(buf.mtext,"quit") == 0)
{
break;
}
printf("A:%s\n",buf.mtext);
}
//退出子进程
exit(EXIT_SUCCESS);
}
//非阻塞回收子进程资源
waitpid(-1,NULL,WNOHANG);
return 0;
}