c - fflush 在命名管道中不起作用

标签 c

我有这段代码: 当第一个 child 向第二个 child 发送数据时,程序卡住了, 第二个 child 做 fscanf 然后卡住了,因为他无法阅读我找不到它的原因。 请帮助我找出如何确保 fflush 将传输数据。

int main()
{
    pid_t childPid ; //Child's and father process id.
    int i ; //An index to create children in loop.
    unsigned int st_search_prime = 0 ;

    if((mkfifo(FIRST_FIFO, S_IFIFO | 0644) == FAIL && errno != EEXIST) ||
       (mkfifo(SECOND_FIFO, S_IFIFO | 0644) == FAIL && errno != EEXIST))
    {
        perror("Cannot create fifo file") ;
        exit(EXIT_FAILURE) ;
    }
    //create the children
    for(i = 0 ; i < NUM_OF_CHILDS  ; i++)
    {
        childPid = fork() ;
        if(childPid < 0)    //Fork failed.
        {
            perror("Cannot fork()") ;
            exit(EXIT_FAILURE) ;
        }
        else if(childPid == CHILD)  //child process
        {
            if(i == FIRST_SON)  //the 1st child process
                doChild1(st_search_prime) ;
            else        //the 2nd child process.
                doChild2(st_search_prime) ;
        }
    }
    //wait for the children to exit.
    for(i = 0 ; i < NUM_OF_CHILDS ; i++)
        wait(&childPid) ;

    return(EXIT_SUCCESS) ;
}

void doChild1(unsigned int st_search_prime)
{
    int counter = 0 ; //Counter for N successfully raffle .
    FILE* fdw1 ;
    FILE* fdr2 ;

    if((!(fdw1 = fopen(FIRST_FIFO, "w"))))
    {
        perror("Cannot open fifo file for w/r") ;
        exit(EXIT_FAILURE) ;
    }

    if((!(fdr2 = fopen(SECOND_FIFO, "r"))))
    {
        perror("Cannot open fifo file for w/r") ;
        exit(EXIT_FAILURE) ;
    }

    for(; counter < N ; counter++)
    {
        st_search_prime = raffle_prime(st_search_prime) ;
        **fprintf(fdw1, "%u\n", st_search_prime) ;
        fflush(fdw1) ;**
        printf("%u\n", st_search_prime) ;
        fscanf(fdr2, "%u\n", &st_search_prime) ;
    }
    fclose(fdw1) ;
    fclose(fdr2) ;

    exit(EXIT_SUCCESS) ;
}

void doChild2(unsigned int st_search_prime)
{
    int counter = 0 ; //Counter for N successfully raffle .
    FILE* fdw2 ;
    FILE* fdr1 ;

    if((!(fdr1 = fopen(FIRST_FIFO, "r"))))
    {
        perror("Cannot open fifo file for w/r") ;
        exit(EXIT_FAILURE) ;
    }
    if(!(fdw2 = fopen(SECOND_FIFO, "w")))
    {
        perror("Cannot open fifo file for w/r") ;
        exit(EXIT_FAILURE) ;
    }

    for(; counter < N ; counter++)
    {
        **fscanf(fdr1, "%u\n", &st_search_prime);**
        st_search_prime = raffle_prime(st_search_prime) ;
        fprintf(fdw2, "%u\n", st_search_prime) ;
        fflush(fdw2) ;
        printf("%u\n", st_search_prime) ;
    }
    fclose(fdr1) ;
    fclose(fdw2) ;

    exit(EXIT_SUCCESS) ;
}

最佳答案

尝试添加调用 setlinebuf(fdw1) - 这应该会有所帮助。

此调用强制在换行后刷新缓冲区。您也可以使用带有参数 _IOLBFsetvbuf() 来获得相同的效果,或者使用 _IONBF 来完全禁用缓冲。

关于c - fflush 在命名管道中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13776383/

相关文章:

c - 强烈返回错误的值

c - 删除数组的元素

c++ - 如何在 C++ 程序中使用 C 头文件?

c - 修改 fs/binfmt_elf.c 以获取自定义添加的部分内容

c - 如何在 C 中运行计时器

c - 如何在 C 中分离缓冲区结果

C:静态结构

c++ - C++ 中的 fprintf 和 vfprintf 有什么区别?

c - 有一个测试卡在无限循环中,你能找到它吗?

c++ - 将星号放在名称之前的指针中是典型的编程实践吗?