命名为fifo错误 - C编程

时间:2016-04-14 15:35:57

标签: c named-pipes fifo

我试图用一些方法来编写和读取一个名为fifo的...显然当我运行它时无缘无故地卡在write_fifo(0)上...所以我的问题是我使用名为fifo以正确的方式?或者unlink方法是否与我的程序混乱?何时何地可以取消链接已命名的fifo。

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

#define FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)


void write_fifo(int s)
{
    printf("write_fifo");
    int writing, n;
    writing = open("myFIFO",O_WRONLY);
    n = write(writing, &s, sizeof(s));
    printf("write: %i byte.\n", n);
    close(writing);
    unlink("myFIFO");
}

int read_fifo()
{
    printf("read_fifo");

    int reading, n, s;
    reading = open("myFIFO", O_RDONLY);
    n = read(reading , &s, sizeof(s));
    printf("read: %i byte: %d\n", n, s);

    close(reading);
    unlink("myFIFO");

    return s;
}

int main()
{
    printf("beginning");

    printf("removing myFIFO");

    if (mkfifo("myFIFO", FILE_MODE) == -1) 
    {
        perror("myFIFO");
        exit(1);
    }

    write_fifo(0);

    printf("\n\n\n\n\n\n\nReading %d",read_fifo());

    return 0;
}

0 个答案:

没有答案