LinuxC匿名管道实现进程间通信

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值