FIFO

       FIFO指代先進先出(first in, first out),Unix中的FIFO類似於管道。它是一個單向(半雙工)數據流。不同於管道的是每個FIFO有一個路徑名與之關聯,從而允許無親緣關係的進程訪問同一個FIFO。FIFO也稱謂有名管道(named pipe)。

       FIFO由mkfifo函數創建。

<span style="font-size:14px">#include <sys/types.h>
#include <sys/stat.h>

int mkfifo(const char *pathname, mode_t mode);        返回:成功時爲0,出錯時爲-1</span>

       FIFO創建完後,它必須或者打開來讀或者打開來寫。可以使用open或fopen函數打開。FIFO不能打開來既讀又寫,因爲它是半雙工的。

       對管道或FIFO的write總是往末尾添加數據,對它們的read則總是從開通返回數據,如果對管道或FIFO調用lseek,那就會返回ESPIPE錯誤。
       下面是一個簡單的例子:

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

#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

#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;
}




發佈了28 篇原創文章 · 獲贊 11 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章