实现父子进程间的通信
介绍: 1 .以结构体为传输目标 ,先定义一个枚举类型存放消息类型,定义一个结构体 存放消息类型和该消息类型的结构体的大小,定义不同消息类型的消息的结构体
父进程写入消息,子进程读取消息
2 .创建管道 父子进程,公有三个文件 pipe_main.c main函数文件 创建管道的 子父进程 pipe_mes.h 文件 枚举的定义 结构体的定义 各种函数的声明
pipe_mes.c 函数的实现
具体代码段;
pipe_main.c:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<errno.h>
#include "pipe_mes.h"
int main(int argc,char** argv)
{
int fd[2];
int pipe_result;
if(pipe_result = pipe(fd));
if(pipe_result == -1)
{
printf("管道创建失败!\n");
exit(errno);
}
pid_t child_pid;
child_pid = fork();
if(child_pid < 0){
printf("创建子进程失败!\n");
exit(errno);
}
else if(child_pid == 0){
//进入子进程 并接受来自父进程的消息 解析并打印
close(fd[1]);
int i;
for(i = 0; i < 3; i++)
{
HMessage hm = (HMessage) malloc (sizeof(Message));
read(fd[0],hm,sizeof(Message));
switch(hm->msg_kind)
{
case Type_1:
{
HMES_ONE M_ONE = ReadMes_ONE(fd[0],hm);
printf("接收来自Type_1的消息: %d\n",M_ONE->num);
}
break;
case Type_2:
{
HMES_TWO M_TWO = ReadMes_TWO(fd[0],hm);
printf("接收来自Type_2的消息: %s\n",M_TWO->str);
}
break;
case Type_3:
{
HMES_THREE M_THREE = ReadMes_THREE(fd[0],hm);
printf("接收来自Type_3的消息:%d\t%s\n",M_THREE->num,M_THREE->str);
}
break;
default:
printf("不识别的消息类型.......请检查!\n");
break;
}
}
_exit(0);
}
close(fd[0]);
//调用发送消息函数
SendMes_ONE(fd[1],2017);
SendMes_TWO(fd[1],"结束了!");
SendMes_THREE(fd[1],2018,"开始了");
return 0;
}
pipe_mes.h
#ifndef __MESSAGE_H__
#define __MESSAGE_H__
//消息类型
typedef enum KIND{
Type_1,Type_2,Type_3
}Msg_kind;
//Message 是所有消息的共同首部
typedef struct Message{
Msg_kind msg_kind;
size_t size;
}Message,*HMessage;
//消息 ONE
typedef struct TMessage_1{
Message base;
int num;
}MES_ONE,*HMES_ONE;
//消息 TWO
typedef struct TMessage_2{
Message base;
char str[1024];
}MES_TWO,*HMES_TWO;
//消息 THREE
typedef struct TMessage_THREE{
Message base;
int num;
char str[1024];
}MES_THREE,*HMES_THREE;
//消息 ONE 接收和发送函数
HMES_ONE ReadMes_ONE(int in,HMessage hm);
size_t SendMes_ONE(int out,int num);
//消息 TWO 接受和发送
HMES_TWO ReadMes_TWO(int in,HMessage hm);
size_t SendMes_TWO(int out,char* str);
//消息 THREE 接受和发送
HMES_THREE ReadMes_THREE(int in,HMessage hm);
size_t SendMes_THREE(int out,int num,char *str);
#endif
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<errno.h>
#include "pipe_mes.h"
//消息 Type_1
size_t SendMes_ONE(int out,int num)
{
HMES_ONE Mes_One = (HMES_ONE) malloc (sizeof(MES_ONE));
Mes_One->base.msg_kind = Type_1;
Mes_One->base.size = sizeof(MES_ONE);
Mes_One->num = num;
return write(out,(char*)Mes_One,Mes_One->base.size);
}
HMES_ONE ReadMes_ONE(int in,HMessage HMES)
{
HMES_ONE Mes_One = (HMES_ONE) malloc (sizeof(MES_ONE));
memcpy(Mes_One,HMES,sizeof(Message));
read(in,(char*)Mes_One + sizeof(Message),sizeof(MES_ONE) - sizeof(Message));
return Mes_One;
}
//消息 Type_2
size_t SendMes_TWO(int out,char* str)
{
HMES_TWO Mes_Two = (HMES_TWO) malloc (sizeof(MES_TWO) + strlen(str));
Mes_Two->base.msg_kind = Type_2;
Mes_Two->base.size = sizeof(MES_TWO) + strlen(str);
memcpy(Mes_Two->str,str,strlen(str) + 1);
return write(out,(char*)Mes_Two,Mes_Two->base.size);
}
HMES_TWO ReadMes_TWO(int in,HMessage HMES)
{
HMES_TWO Mes_Two = (HMES_TWO) malloc (HMES->size);
memcpy(Mes_Two,HMES,sizeof(Message));
read(in,Mes_Two->str,HMES->size - sizeof(Message));
return Mes_Two;
}
//消息 Type_3
size_t SendMes_THREE(int out,int num,char* str)
{
HMES_THREE Mes_Three = (HMES_THREE) malloc (sizeof(MES_THREE) + strlen(str));
Mes_Three->base.msg_kind = Type_3;
Mes_Three->base.size = sizeof(MES_THREE) + strlen(str);
Mes_Three->num = num;
memcpy(Mes_Three->str,str,strlen(str) + 1);
return write(out,(char *)Mes_Three,Mes_Three->base.size);
}
HMES_THREE ReadMes_THREE(int in,HMessage HMES)
{
HMES_THREE Mes_Three = (HMES_THREE) malloc (HMES->size);
memcpy(Mes_Three,HMES,sizeof(Message));
read(in,(char*)Mes_Three + sizeof(Message),sizeof(MES_THREE) - sizeof(Message));
return Mes_Three;
}
如有错误和疑问 可通过QQ2120397197 交流