Linux进程间通信--管道

Linux进程间通信–管道

管道是是Unix中最古老的进程间通信的形式。 我们把从一个进程连接到另一个进程的一个数据流称为一个“管道”。
管道分为匿名管道和命名管道。
- 匿名管道
匿名管道只能实现具有亲缘关系进程之间通信,而不能实现没有关系的两个本地进程之间的通信使用

匿名管道的创建

#include<unistd.h>
int pipe(int fd[2]);

下面是利用匿名管道实现父子进程通信的代码实例

   1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<string.h>
  4 #include<unistd.h>
  5 
  6 int main()
  7 {
  8       int fd[2]={ 0};
  9      if (pipe(fd)<0)
 10     {
 11       perror(" pipe\n");
 12       return 1;
 13     }
 14      pid_t pid=fork();
 15      if(pid<0)
 16      {
 17          perror(" fork\n");
 18          return 2;
 19      }
 20     else if(pid==0)
 21     {
 22         close(fd[0]);
 23         char *msg=" I am child";
 24         while(1)
 25         {
 26             write(fd[1],msg,strlen(msg));
 27             sleep(1);
 28         }
 29     }
 30     else
 31     {
 32         close(fd[1]);
 33         char buf[1024];
 34         ssize_t s=read(fd[0],buf,sizeof(buf));
 35         while(1)
 36       {
 37           if (s>0)
 38          {
 39              buf[s]=0;
 40              printf(" client->father:%s\n",buf);
 41           }
 42     }
 43     }
 44     return 0;
 45 }

命名管道
在不相关的进程之间通信,可以使用FIFO文件来做这项工作,它经常被称为命名管道 。

命名管道的创建

int main(int argc, char *argv[]) 
{   
    mkfifo("p2", 0644);   
    return 0;
}

下面是利用命名管道实现两个进程之间通信的代码实例

编写Makefile

//Makefile
.PHONY:all
all:client server
client:client.c
     gcc -o $@ $^

server:server.c
     gcc -o $@ $^

.PHONY:clean
clean:
     rm -f client server

client.c(写数据)

  1 #include<stdio.h>
  2 #include<sys/types.h>
  3 #include<sys/stat.h>
  4 #include<fcntl.h>
  5 #include<unistd.h>
  6 #include<stdlib.h>
  7 #include<string.h>
  8 
  9 int main()
 10 {
 11     int wfd=open("mypipe",O_WRONLY);
 12     if(wfd<0)
 13     {
 14         perror("open");
 15         return 1;
 16         }
 17         char buf[1024];
 18         while(1)
 19         {
 20             buf[0]=0;
 21             printf(" Please Enter:\n");
 22             fflush(stdout);
 23             ssize_t s=read(0,buf,sizeof(buf)-1);
 24              if(s>0)
 25              {
 26                  buf[s]=0;
 27                  write(wfd,buf,strlen(buf));
 28              }
 29               else if(s<=0)
 30              {
 31               perror("read");
 32               return 2;
 33              }
 34             }
 35             close(wfd);
 36             return 0;
 37     }

server.c(读数据)

 1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<sys/types.h>
  4 #include<sys/stat.h>
  5 #include<fcntl.h>
  6 #include<unistd.h>
  7 
  8 int main()
  9 {
 10         umask(0);
 11         if(mkfifo("mypipe",0644)==-1)
 12         {
 13             perror("mkfifo");
 14             return 1;
 15             }
 16         int rfd=open("mypipe",O_RDONLY);
 17         if(rfd<0)
 18         {
 19             perror("open");
 20             return 2;
 21             }
 22             char buf[1024];
 23             while(1)
 24             {
 25                 printf(" Please wait...\n");
 26                 ssize_t s=read(rfd,buf,sizeof(buf)-1);
 27                 if(s>0)
 28                 {
 29                     buf[s-1]=0;
 30                     printf("client say:%s\n",buf);
 31                     }
 32                   else if(s==0)
 33                   {
 34                       printf(" client quit,exit now!\n");
 35                       break;
 36                       }
 37                 else{
 38                         perror("read");
 39                         return 3;
 40                           }
 41                 }
 42                 close(rfd);
 43                 return 0;
 44     }

运行结果如下
这里写图片描述

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值