linux环境编程之进程间通信(pipe & FIFO)

 /***********************rw.h*******************/
#include <unistd.h>

int Read(int fd, void *buf,size_t count)
{
    void *p =buf;
    int totle = count;
    int current = 0;
    int iRet = 0;

    while(current < totle)
    {
        iRet = read(fd,p + current,totle - current);
        if(iRet < 0)
        {
            perror("Read Error");
            return -1;
        }
        else if(iRet == 0)
        {
            return current;

        }
        else
        {
            current += iRet;
        }
    }
    return current;

}



int Write(int fd,void *buf,size_t count)

{
    void *p = buf;
    int totle = count;
    int current = 0;
    int iRet = 0;

    while(current < totle)
    {
        iRet = write(fd,p + current,totle - current);
        if(iRet < 0)
        {
            perror("Write Error");
            return -1;
        }
        else if(iRet == 0)
        {
            return current;
        }
        else 
        {
            current += iRet;
        }
    }
    return current;
}




/********************************************************
* name : pipe_print.c
* author : cyher
* date :2008-8-5
* descirption : 两个进程,父进程和子进程。
*               建立pipe父进程读取文件名给子进程读文件给父进程。
*               父进程打印在屏幕上。
* ********************************************************/

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include "rw.h"//读管道的时候需要用这个自己写的read循环读,否则一次可能读不全
#define MAX 1024
int main (int argc,char *argv[])
{
    int fd1[2];
    int fd2[2];
    int pipe1,pipe2;
    size_t n=1;
    pid_t pid;
    char buf1[MAX] = {0};
    char buf2[MAX] = {0};
    char buf3[MAX] = {0};
    int fd;
    ssize_t read_num;
    if(argc != 2)
    {
        printf("usage:\"pipe_print name\n\"");
        exit(0);
    }
    if((pipe1=pipe(fd1)) == -1 )//父向子传信息的管道
    {
        perror("pipe1");
        exit(1);
    }

    if((pipe2=pipe(fd2)) == -1)//子向父传信息的管道
    {
        perror("pipe2");
        exit(1);
    }

    if((pid=fork()) < 0)
    {
        perror("fork");
        exit(1);
    }
    else if(pid == 0)//子进程
    {
        close(fd1[1]);/*关管道1写端*/
        close(fd2[0]);/*关管道2读端*/
        sleep(1);
        if(read(fd1[0],buf2,MAX) == -1)/*读取父进程传来的文件名*/
        {
            exit(1);
        }
    if(( fd = open(buf2,O_RDONLY,0) ) < 0)/*打开文件*/
    {
        perror("open error");
        exit(1);
    }

        while(Read(fd,buf1,MAX) > 0)/*读文件放入buf1*/
        {
            Write(fd2[1],buf1,MAX);/*写入管道2*/
            memset(buf1,0,MAX);/*清空buf1以免出错*/
        }
        close(fd1[0]);
        close(fd2[1]);
        exit(0);
    }
    else//父进程
    {
    
        close(fd2[1]);/*关管道2的写端*/
        close(fd1[0]);/*关管道1的读端*/
        n=strlen(argv[1]);
        if(Write(fd1[1],argv[1],n) == -1)/*把命令行参数发给管道1*/
        {
            exit(1);
        }
        while((read_num=Read(fd2[0],buf3,MAX)) > 0 )/*读取管道2中的信息放在buf3中*/
        {
            if(read_num == -1)
            {
                perror("read");
                exit(1);
            }
            
            if(Write(1,buf3,MAX) < 0)/*把buf3中的信息写在表准输出上*/
            {
                perror("write");
                exit(1);
            }
            memset(buf3,0,MAX);/*清空buf3以免出错*/
        }
        putchar('\n');
        close(fd1[1]);
        close(fd2[0]);
        if(waitpid(pid,NULL,0) == -1)/*父进程等待子进程关闭回收子进程资源*/
        {
            perror("wait");
            exit(1);
        }
    }
}




/********************************************************
* name : fifo.c
* author : cyher
* date :2008-8-5
* descirption : 两个进程,父进程和子进程
*               建立FIFO父进程读取文件名传给子进程读取文件传回父进程
*               父进程再打印在屏幕上
* ********************************************************/

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include "rw.h"
#define MAX 1024
int main (int argc,char *argv[])
{
    int fd1,fd2,fd3,fd4;
    int fifo,fifo2;
    size_t n=1;
    pid_t pid;
    char buf1[MAX] = {0};
    char buf2[MAX] = {0};
    char buf3[MAX] = {0};
    int fd;
    ssize_t read_num;
    if(argc != 2)
    {
        printf("usage:\"pipe_print name\n\"");
        exit(0);
    }
    if((fifo=mkfifo("/tmp/cyher088",0777)) == -1)
    {
        perror("fifo");
        exit(1);

    }
    if((fifo2=mkfifo("/tmp/cyher089",0777)) == -1)
    {
        perror("fifo");
        exit(1);

    }
    if((pid=fork()) < 0)
    {
        perror("fork");
        exit(1);
    }
    else if(pid == 0)//子进程
    {
        if( ( fd1 = open("/tmp/cyher088",O_RDONLY,0666) ) == -1)
        {
            perror("open1");
            exit(1);
        }
        sleep(1);
        if(read(fd1,buf2,MAX) == -1)
        {
            perror("read");
            exit(1);
        }
        if(( fd = open(buf2,O_RDONLY,0) ) < 0)
        {
            perror("open error");
            exit(1);
        }
        if( ( fd4 = open("/tmp/cyher089",O_WRONLY ,0666) ) == -1)
        {
            perror("open1");
            exit(1);
        }
        
        while(Read(fd,buf1,MAX) > 0)
        {
            Write(fd4,buf1,MAX);
            memset(buf1,0,MAX);
        }
        close(fd1);
        close(fd4);
        exit(0);
    }
    else//父进程
    {
    
        if( ( fd3 = open("/tmp/cyher088",O_WRONLY ,0666) ) == -1)
        {
            perror("open1");
            exit(1);
        }
        n=strlen(argv[1]);
        if(Write(fd3,argv[1],n) == -1)
        {
            exit(1);
        }
        if( ( fd2 = open("/tmp/cyher089",O_RDONLY,0666) ) == -1)
        {
            perror("open1");
            exit(1);
        }
        while((read_num=Read(fd2,buf3,MAX)) > 0 )
        {
            if(read_num == -1)
            {
                perror("read");
                exit(1);
            }
            
            if(Write(1,buf3,MAX) < 0)
            {
                perror("write");
                exit(1);
            }
            memset(buf3,0,MAX);
        }
        putchar('\n');
        close(fd3);
        close(fd2);
        if(waitpid(pid,NULL,0) == -1)
        {
            perror("wait");
            exit(1);
        }
    }
}

你可能感兴趣的:(linux,通信,职场,pipe,休闲)