Linux 下进程间通信机制(二) Pipe和Fifo

来源:互联网 发布:ubuntu ssh key 登录 编辑:程序博客网 时间:2024/05/01 22:03

无名管道pipe:

 

#include<unistd.h>#include<string.h>#include<stdlib.h>#include<stdio.h>#include<errno.h>#define N 128int main(){    char buf_w[N];    char buf_r[N];    int fd[2];    int pid;    pipe(fd); //管道基于文件描述符    if((pid=fork()) == -1)    {        perror("fork");        exit(-1);    }    if(pid == 0)    {        close(fd[0]);        while(1)        {            while(fgets(buf_w, N, stdin) != NULL)//注意fgets函数的用法,谨慎处理字符串中的'\0'和'\n'            {                printf("read from child\n");                buf_w[strlen(buf_w)-1] = 0;//去掉字符串中的'\n'                write(fd[1], buf_w ,strlen(buf_w));            }        }    }    else    {        close(fd[1]);        int n;        while(1)        {            if((n = read(fd[0], buf_r, N)) >0)            {                buf_r[n] ='\n';                write(1, buf_r, n+1);                printf("write form parent%d\n",n);            }            sleep(3);        }    }    exit(0);}


 

有名管道fifo:

//createfifo.c#include<stdio.h>#include<fcntl.h>#include<unistd.h>#include<sys/types.h>#include<error.h>#include<stat.h>#include<stdlib.h>int main(){    if(mkfifo("fifo.demo", 0666) == -1)    {        perror("mkfifo");        exit(-1);    }    return 0;}//writefifo.c//先执行writefifo,不执行readfifo,则阻塞在open函数(可以增加参数,实现非阻塞)
#include<unistd.h>#include<fcntl.h>#include<string.h>#include<stdlib.h>#include<stdio.h>#include<error.h>#define N 64int main(){    int fd;    char buf[N];    if((fd=open("fifo.demo", O_WRONLY)) == -1)    {        perror("open fifo.demo");        exit(-1);    }    while(1)    {        if(fgets(buf, N, stdin) != NULL)        {            write(fd, buf, strlen(buf));            if(strncmp(buf,"exit", 4) == 0)//比较前四个字符,buf第五个字符是'\n'            {                close(fd);                exit(1);            }        }    }}//readfifo.c //先执行readfifo,不执行writefifo,则阻塞在open函数(可以增加参数,实现非阻塞)#include<fcntl.h>#include<stdio.h>#include<unistd.h>#include<stdlib.h>#define N 64int main(){    int fd;    char buf[N];    int n;    if((fd=open("fifo.demo", O_RDONLY))== -1)    {        perror("open fifo.demo");        exit(-1);    }        while(1)    {        if((n = read(fd, buf, N)) >= 0)        {            if(n == 0)            {                exit(1);            }            write(1, buf, n);//读多少,写多少        }    }}


 

两个进程间通过管道拷贝数据:

//readfile.c/*读文件,写管道*///先调用mkfifo命令创建一个fifo文件#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<error.h>#include<stdio.h>#include<stdlib.h>#include<fcntl.h>#define N 128int main(int argc, char *argv[]){    int n;    int fd;    int filefd;    char buf[N];        //    if(argc != 2)    {        fprintf(stderr,"usage:%s  <sourcefile>\n", argv[0]);        perror("");        exit(-1);    }    //open source file    if((filefd=open(argv[1],O_RDONLY)) ==-1)    {        perror("open filefd");        exit(-1);    }    /*create  fifo     if(mkfifo("copyfifo",0666) ==-1)    {        perror("mkfifo");           exit(-1);    }*/        //open fifo    if((fd = open("copyfifo",O_WRONLY))==-1)    {        perror("open copyfifo");        exit(-1);    }             while((n=read(filefd, buf, N)) >=0)        {            if(n == 0)            {                close(fd);                close(filefd);            }            else            {                write(fd, buf, n);//注意读多少,写多少            }           }    }//writefile.c/*读管道,写文件*/#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<error.h>#include<stdio.h>#include<stdlib.h>#include<fcntl.h>#define N 128int main(int argc, char *argv[]){    int n;    int fd;    int filefd;    char buf[N];        //    if(argc != 2)    {        fprintf(stderr,"usage:%s  <sourcefile>\n", argv[0]);        perror("");        exit(-1);    }    //open source file    if((filefd=open(argv[1],O_WRONLY|O_CREAT|O_TRUNC,0666)) ==-1)    {        perror("open filefd");        exit(-1);    }        //open fifo    if((fd = open("copyfifo",O_RDONLY))==-1)    {        perror("open copyfifo");        exit(-1);    }             while((n=read(fd, buf, N)) >=0)//写端关闭,管道中没有文件时返回0,利用返回值0判断写端是否存在,不返回零没有数据则阻塞        {            if(n == 0)            {               close(fd);               close(filefd);               exit(1);            }            else            {                write(filefd, buf, n);            }           }}