有名管道

用有名管道实现进程A与进程B消息一发一送

程序a.c

/*************************************************************************
  > File Name: a.c
  > Author: KrisChou
  > Mail:zhoujx0219@163.com 
  > Created Time: Fri 22 Aug 2014 02:46:06 PM CST
 ************************************************************************/

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])//EXE send_fifo recv_fifo
{
    /* A创建管道1,我们假定其先发消息,再收消息 */
    /* 管道2由B创建 */
    printf("create a fifo_1...
");
    if(-1 == mkfifo(argv[1], 0666))
    {
        perror("mkfifo");
        exit(1);
    }
    printf("finish make fifo_1 !
");
    
    
    /* A以写方式打开管道1,以读方式打开管道2 */
    int fd_send, fd_recv ;
    printf("open fifo....
");
    fd_send = open(argv[1], O_WRONLY);
    fd_recv = open(argv[2], O_RDONLY);
    if(fd_send == -1 || fd_recv == -1)
    {
        perror("open");
        unlink(argv[1]); /* 如果打开管道失败,删除A自己创建的管道1 */
        exit(1);
    }
    printf("open fifo sucess ! 
");
    
    
    /* 发送以及接受消息 */
    /* A先发消息*/
    char send_buf[1024];
    char recv_buf[1024];
    int send_flag = 0 ;    /* 默认没有发完 */
    int recv_flag = 0 ;    /* 默认没有接完 */
    
    /* 只要*/
    while(recv_flag == 0 || send_flag == 0)
    {
            memset(send_buf, 0 ,1024);
            if(send_flag == 0 && fgets(send_buf, 1024, stdin) == NULL) // ctrl+d会使fgets返回NULL,代表发送结束
            {
                close(fd_send);
                send_flag = 1 ;
            }else if (send_flag == 0)
            {
                write(fd_send, send_buf, strlen(send_buf));
            }
            
            /* 假定A没有发完,当程序执行完第一个if语句时,开始执行第二个if语句时,read发生阻塞,
               等待从管道中读消息。此时必须等到B执行第二个if语句,发送消息,即B往管道里写东西,
               A才可以接受消息 */
            /* tip1:只要对方的写端没有关闭,那么read不会返回0,而是等待从管道中读取信息 */
            /* tip2:A执行第一个if语句往管道中写入东西后,B几乎是在同时接受到消息的(B的第一个if语句)*/
            
            memset(recv_buf, 0, 1024);
            if(recv_flag == 0 && read(fd_recv, recv_buf, 1024) == 0)
            {
                recv_flag = 1 ;
                close(fd_recv);
            }else if(recv_flag == 0)
            {
                write(1, recv_buf, strlen(recv_buf));
            }

    }
    printf("A over ! 
");
    unlink(argv[1]);
    //unlink(argv[2]);
    return 0 ;
}

程序b.c

/*************************************************************************
  > File Name: b.c
  > Author: KrisChou
  > Mail:zhoujx0219@163.com 
  > Created Time: Fri 22 Aug 2014 02:46:06 PM CST
 ************************************************************************/

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])//EXE send_fifo recv_fifo
{
    /* B创建管道2,我们假定其先收消息,再发消息 */
    /* 管道1由A创建 */
    printf("create a fifo_2...
")
    if(-1 == mkfifo(argv[2], 0666))
    {
        perror("mkfifo");
        exit(1);
    }
    printf("finish make fifo_2 !
");
    
    
    /* B以读方式打开管道1,以写方式打开管道2 */
    int fd_send, fd_recv ;
    printf("open fifo....
");
    fd_recv = open(argv[1], O_RDONLY);
    fd_send = open(argv[2], O_WRONLY);
    if(fd_send == -1 || fd_recv == -1)
    {
        perror("open");
        unlink(argv[2]); /* 如果打开管道失败,删除B自己创建的管道2 */
        exit(1);
    }
    printf("open fifo sucess ! 
");
    
    
    /* 接收以及发送消息 */
    /* B先接收消息*/
    char send_buf[1024];
    char recv_buf[1024];
    int send_flag = 0 ;    /* 默认没有发完 */
    int recv_flag = 0 ;    /* 默认没有接完 */
    

    while(recv_flag == 0 || send_flag == 0)
    {
            memset(recv_buf, 0, 1024);
            if(recv_flag == 0 && read(fd_recv, recv_buf, 1024) == 0)
            {
                recv_flag = 1 ;
                close(fd_recv);
            }else if(recv_flag == 0)
            {
                write(1, recv_buf, strlen(recv_buf));
            }
            /* 上面第一个if语句执行完后,B读完之前A发送的消息,A与B的第一个if语句几乎同时完成*/
            memset(send_buf, 0 ,1024);
            if(send_flag == 0 && fgets(send_buf, 1024, stdin) == NULL) // ctrl+d会使fgets返回NULL,代表发送结束
            {
                close(fd_send);
                send_flag = 1 ;
            }else if (send_flag == 0)
            {
                write(fd_send, send_buf, strlen(send_buf));
            }
            
    }
    printf("B over ! 
");
    unlink(argv[2]);
    //unlink(argv[2]);
    return 0 ;
}

运行:

./a.exe  fifo1 fifo2

./b.exe  fifo1 fifo2

原文地址:https://www.cnblogs.com/jianxinzhou/p/3929947.html