1> 使用消息队列完成两个进程之间相互通信
snd.c
#include <myhead.h>
struct msgbuf
{
long mtype;
char mtext[1024];
};
#define SIZE sizeof(struct msgbuf)-sizeof(long)
int main(int argc,const char *argv[])
{
pid_t pid = fork();
if(pid==-1)
{
perror("fork error");
return -1;
}
else if(pid==0)
{
//子进程
key_t key = ftok("/",'k');
int msqid = msgget(key,IPC_CREAT|0644);
if(msqid==-1)
{
perror("msgget error");
return -1;
}
struct msgbuf buf;
while(1) //接受类型2消息
{
msgrcv(msqid,&buf,SIZE,2,0);
if(strcmp(buf.mtext,"quit")==0)
{
break;
}
printf("收到消息:%s\n",buf.mtext);
}
exit(EXIT_SUCCESS);
}
//父进程
key_t key = ftok("/",'k');
int msqid = msgget(key,IPC_CREAT|0644);
if(msqid==-1)
{
perror("msgget error");
return -1;
}
struct msgbuf buf;
while(1) //发送类型1消息
{
fgets(buf.mtext,SIZE,stdin);
buf.mtext[strlen(buf.mtext)-1]=0;
buf.mtype=1;
msgsnd(msqid,&buf,SIZE,0);
if(strcmp(buf.mtext,"quit")==0)
{
break;
}
}
wait(NULL);
return 0;
}
recv.c
#include <myhead.h>
struct msgbuf
{
long mtype;
char mtext[1024];
};
#define SIZE sizeof(struct msgbuf)-sizeof(long)
int main(int argc,const char *argv[])
{
pid_t pid = fork();
if(pid==-1)
{
perror("fork error");
return -1;
}
else if(pid==0)
{
//子进程
key_t key = ftok("/",'k');
int msqid = msgget(key,IPC_CREAT|0644);
if(msqid==-1)
{
perror("msgget error");
return -1;
}
struct msgbuf buf;
while(1) //发送类型2消息
{
fgets(buf.mtext,SIZE,stdin);
buf.mtext[strlen(buf.mtext)-1]=0;
buf.mtype=2;
msgsnd(msqid,&buf,SIZE,0);
if(strcmp(buf.mtext,"quit")==0)
{
break;
}
}
exit(EXIT_SUCCESS);
}
key_t key = ftok("/",'k');
int msqid = msgget(key,IPC_CREAT|0644);
if(msqid==-1)
{
perror("msgget error");
return -1;
}
struct msgbuf buf;
while(1) //接受类型1消息
{
msgrcv(msqid,&buf,SIZE,1,0);
if(strcmp(buf.mtext,"quit")==0)
{
break;
}
printf("收到消息:%s\n",buf.mtext);
}
wait(NULL);
if(msgctl(msqid,IPC_RMID,NULL)==-1)
{
perror("msgctl error");
return -1;
}
return 0;
}
2> 将共享内存的实现重新敲一遍
snd.c
#include<myhead.h>
#include<sys/user.h>
int main(int argc, const char *argv[])
{
//1、创建key值用于创建共享内存段
key_t key = ftok("/", 't');
if(key == -1)
{
perror("ftok error");
return -1;
}
printf("key = %d\n", key);
//2、创建一个共享内存的对象
int shmid = shmget(key, PAGE_SIZE, IPC_CREAT|0664);
if(shmid == -1)
{
perror("shmget error");
return -1;
}
printf("shmid = %d\n", shmid);
//3、将共享内存段映射到程序中来
char *addr = (char *)shmat(shmid, NULL, 0);
printf("addr = %p\n", addr); //输出映射的地址
//向共享内存中写入数据
strcpy(addr, "hello a hua qing yuan jian\n");
sleep(5);
//取消映射关系
if(shmdt(addr) ==-1)
{
perror("shmdt error");
return -1;
}
while(1);
return 0;
}
recv.c
#include<myhead.h>
#include<sys/user.h>
int main(int argc, const char *argv[])
{
//1、创建key值用于创建共享内存段
key_t key = ftok("/", 't');
if(key == -1)
{
perror("ftok error");
return -1;
}
printf("key = %d\n", key);
//2、创建一个共享内存的对象
int shmid = shmget(key, PAGE_SIZE, IPC_CREAT|0664);
if(shmid == -1)
{
perror("shmget error");
return -1;
}
printf("shmid = %d\n", shmid);
//3、将共享内存段映射到程序中来
char *addr = (char *)shmat(shmid, NULL, 0);
printf("addr = %p\n", addr); //输出映射的地址
//读出共享内存中的数据
printf("消息为:%s", addr);
sleep(5);
if(shmdt(addr) == -1)
{
perror("shmdt error");
return -1;
}
//删除共享内存
if(shmctl(shmid, IPC_RMID, NULL) == -1)
{
perror("shmctl error");
return -1;
}
while(1);
return 0;
}
思维导图