LinuxC匿名管道实现进程间通信
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
typedef enum Kind {KString, KInt, KChar} Kind;
typedef struct Message{
Kind kind;
size_t size;
}Message, *PMessage;
typedef struct StringMessage{
Message base;
char data[1];
}StringMessage,*PStringMessage;
typedef struct IntMessage{
Message base;
int number;
}IntMessage, *PIntMessage;
typedef struct CharMessage{
Message base;
char data;
}CharMessage, *PCharMessage;
ssize_t sendIntMessage(int out,int num);
PIntMessage readIntMessage(int in, PMessage pm);
ssize_t sendCharMessage(int out,char ch);
PCharMessage readCharMessage(int in,PMessage pm);
ssize_t sendStringMessage(int out, char* str);
PStringMessage readStringMessage(int in, PMessage pm);
int main(int argc, char** argv){
int fd[2];
int result;
result = pipe(fd);
if(result == -1){
printf("fail to pipe");
exit(errno);
}
pid_t cpid;
cpid = fork();
if(cpid < 0){
printf("fail to fork");
exit(errno);
}else if(cpid == 0){
int i;
for(i = 0; i < 3; i++){
PMessage pm = (PMessage)malloc(sizeof(Message));
read(fd[0], pm, sizeof(Message));
switch(pm->kind){
case KString:
{
PStringMessage psm = readStringMessage(fd[0],pm);
printf("\nthe string message is:%s\n",psm->data);
}
break;
case KChar:
{
PCharMessage pcm = readCharMessage(fd[0],pm);
printf("the char message is: %c\n",pcm->data);
}
break;
case KInt:
{
PIntMessage pim = readIntMessage(fd[0],pm);
printf("the int message is: %d \n",pim->number);
}
break;
default:
printf("not find:%d\n", pm->kind);
}
}
_exit(0);
}
close(fd[0]);
sendStringMessage(fd[1], "Hello World!");
sendIntMessage(fd[1],666);
sendCharMessage(fd[1],'A');
return 0;
}
ssize_t sendIntMessage(int out,int num){
PIntMessage pim = (PIntMessage) malloc(sizeof(IntMessage));
pim->base.kind = KInt;
pim->base.size = sizeof(IntMessage);
pim->number= num;
return write(out, (char*) pim, pim->base.size);
}
PIntMessage readIntMessage(int in, PMessage pm){
PIntMessage pim = (PIntMessage)malloc(sizeof(IntMessage));
memcpy(pim, pm, sizeof(Message));
read(in, (char*)pim + sizeof(Message), sizeof(IntMessage) - sizeof(Message));
return pim;
}
ssize_t sendCharMessage(int out,char ch){
PCharMessage pcm = (PCharMessage)malloc(sizeof(CharMessage));
pcm->base.kind = KChar;
pcm->base.size = sizeof(CharMessage);
pcm->data = ch;
return write(out,(char*)pcm,pcm->base.size);
}
PCharMessage readCharMessage(int in,PMessage pm){
PCharMessage pcm = (PCharMessage)malloc(pm->size);
memcpy(pcm,pm,sizeof(Message));
read(in,(char*)pcm+sizeof(Message),sizeof(CharMessage)-sizeof(Message));
return pcm;
}
ssize_t sendStringMessage(int out, char* str){
PStringMessage psm = (PStringMessage)malloc(sizeof(StringMessage) + strlen(str));
psm->base.kind = KString;
psm->base.size = sizeof(StringMessage) + strlen(str);
memcpy(psm->data, str, strlen(str) + 1);
return write(out, (char*)psm, psm->base.size);
}
PStringMessage readStringMessage(int in, PMessage pm){
PStringMessage psm = (PStringMessage)malloc(pm->size);
memcpy(psm, pm, sizeof(Message));
read(in, psm->data, pm->size - sizeof(Message));
return psm;
}