FIFO

       FIFO指代先进先出(first in, first out),Unix中的FIFO类似于管道。它是一个单向(半双工)数据流。不同于管道的是每个FIFO有一个路径名与之关联,从而允许无亲缘关系的进程访问同一个FIFO。FIFO也称谓有名管道(named pipe)。

       FIFO由mkfifo函数创建。

#include 
#include 

int mkfifo(const char *pathname, mode_t mode);        返回:成功时为0,出错时为-1

       FIFO创建完后,它必须或者打开来读或者打开来写。可以使用open或fopen函数打开。FIFO不能打开来既读又写,因为它是半双工的。

       对管道或FIFO的write总是往末尾添加数据,对它们的read则总是从开通返回数据,如果对管道或FIFO调用lseek,那就会返回ESPIPE错误。
       下面是一个简单的例子:

/*
 * main.cpp
 *
 *  Created on: 2013-10-26
 *      Author: Richard
 */

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define FIFO1 "/tmp/fifo.1"
#define FIFO2 "/tmp/fifo.2"

void client(int readfd, int writefd)
{
    size_t len;
    size_t n;
    char buff[1024];
    fgets(buff, 1024, stdin);
    len = strlen(buff);
    if (buff[len - 1] == '\n')
    {
        len--;
    }
    write(writefd, buff, len);
    while ((n = read(readfd, buff, 1024)) > 0)
    {
        write(STDOUT_FILENO, buff, n);
    }
}

void server(int readfd, int writefd)
{
    int fd;
    ssize_t n;
    char buff[1025];
    if ((n = read(readfd, buff, 1024)) == 0)
    {
        printf("End of file while reading pathname");
        exit(0);
    }
    buff[n] = '\0';
    if ((fd = open(buff, O_RDONLY)) < 0)
    {
        snprintf(buff + n, sizeof(buff) - n, "Cannot open, %s\n",
                strerror(errno));
        n = strlen(buff);
        write(writefd, buff, n);
    }
    else
    {
        while ((n = read(fd, buff, 1024)) > 0)
        {
            write(writefd, buff, n);
        }
        close(fd);
    }

}

int main(int argc, char **argv)
{
    int readfd, writefd;
    pid_t childpid;
    if ((mkfifo(FIFO1, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0)
            && (errno != EEXIST))
    {
        printf("cannot create %s\n", FIFO1);
        return 0;
    }

    if ((mkfifo(FIFO2, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0)
            && (errno != EEXIST))
    {
        unlink(FIFO1);
        printf("cannot create %s\n", FIFO2);
        return 0;
    }

    if ((childpid = fork()) == 0)
    {
        readfd = open(FIFO1, O_RDONLY, 0);
        writefd = open(FIFO2, O_WRONLY, 0);
        server(readfd, writefd);
        exit(0);
    }

    writefd = open(FIFO1, O_WRONLY, 0);
    readfd = open(FIFO2, O_RDONLY, 0);

    client(readfd, writefd);
    waitpid(childpid, NULL, 0);
    close(readfd);
    close(writefd);

    unlink(FIFO1);
    unlink(FIFO2);

    return 0;
}




你可能感兴趣的:(Unix网络编程学习笔记)