有名管道

用有名管道實現進程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...\n");
    if(-1 == mkfifo(argv[1], 0666))
    {
        perror("mkfifo");
        exit(1);
    }
    printf("finish make fifo_1 !\n");
    
    
    /* A以寫方式打開管道1,以讀方式打開管道2 */
    int fd_send, fd_recv ;
    printf("open fifo....\n");
    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 ! \n");
    
    
    /* 發送以及接受消息 */
    /* 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 ! \n");
    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...\n")
    if(-1 == mkfifo(argv[2], 0666))
    {
        perror("mkfifo");
        exit(1);
    }
    printf("finish make fifo_2 !\n");
    
    
    /* B以讀方式打開管道1,以寫方式打開管道2 */
    int fd_send, fd_recv ;
    printf("open fifo....\n");
    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 ! \n");
    
    
    /* 接收以及發送消息 */
    /* 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 ! \n");
    unlink(argv[2]);
    //unlink(argv[2]);
    return 0 ;
}

運行:spa

./a.exe  fifo1 fifo2code

./b.exe  fifo1 fifo2blog

相關文章
相關標籤/搜索