从管道中读取值时,c和bash中的tail-f和cat之间有什么区别

从管道中读取值时,c和bash中的tail-f和cat之间有什么区别,c,bash,C,Bash,我正在学习c程序。我有一个如下所示的程序 在testpipe.c中 #include<stdio.h> #include<string.h> #include<unistd.h> #include<fcntl.h> const char *fifo ="/tmp/test"; int main(){ int fd = open(fifo, o_wronly); char *text ="hello this i

我正在学习c程序。我有一个如下所示的程序

在testpipe.c中

#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>

const char *fifo ="/tmp/test";

int main(){
 int fd = open(fifo, o_wronly);
 char *text ="hello this is the text";
 while(1){
   write(fd,text,strlen(text));
 }
 return 0;
}
然后执行

./testpipe
程序将运行并写入管道/tmp/test

现在在另一个航站楼

如果我打字

tail -f /tmp/test
我没有发现任何结果

但是我试着去做

cat /tmp/test
能够持续得到结果

  • 这两者的区别是什么

  • 除此之外,如果我终止cat/tmp/test,它会杀死./testpipe为什么

  • 我尝试使用bash来试验上述情况。但结果不同 我试过的第一个终端

    mkfifo /tmp/test
    
    while true; do
     echo $(date) >/tmp/test
    done
    
    第二终端

    我打字的时候

     cat /tmp/test 
    
    我不连续地得到输出

    但是当我尝试的时候

    tail -f /tmp/test
    
    不断地获得输出。 如果我试图终止tail或cat,它不会杀死while循环

  • 这两种方法之间的区别是什么,我遗漏了什么

  • 请告诉我为什么这个问题被否决了?我错过了什么?我不知道你为什么被否决了。这个问题似乎很清楚,我可以回答!我不知道答案是什么,但我希望你能得到一个。这似乎是因为操作系统中文件的写入一致性。阅读这篇文章,你会发现你答案的某些部分:你发布的C代码无法编译。另外,它永远不会打印出任何
    tail
    查找的换行符。读取将解释程序在读取器进程退出时被终止的原因。
    tail -f /tmp/test