2

I am leaning c program . And i have a program shown below.

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

i created a pipe in bash using below command

mkfifo /tmp/test

then execute

./testpipe

the program will be running and writing to the pipe /tmp/test

now in another terminal

if i type

tail -f /tmp/test

I don't find any result

But i tried to do

cat /tmp/test

Able to get the result result continuously

  1. What is the difference between these two.

  2. Apart from it if i terminate cat /tmp/test , it kill the ./testpipe why?

I tried to experiment the above case using a bash. But the result was different First terminal i tried like

mkfifo /tmp/test

while true; do
 echo $(date) >/tmp/test
done

Second terminal

When i type

 cat /tmp/test 

I get output not continuously.

But when i try

tail -f /tmp/test

Getting continuously the output. If i try to terminate tail or cat , It is not killing the while loop.

  1. What is the difference between these two approach, what i am missing?
Vipin
  • 938
  • 5
  • 18
  • 36
  • Please tell me why this question is down voted? What i am missing in this? – Vipin Aug 01 '20 at 20:00
  • 1
    I don't know why you were downvoted. The question seems quite clear and answerable to me! I have no idea what the answer is, but I hope you get one. – zwol Aug 01 '20 at 20:07
  • It seem because of write cosistency for file in the OS. Read this post and you can find some section of your answer: https://unix.stackexchange.com/questions/502349/tail-a-file-while-its-open-for-writing – Majid Hajibaba Aug 01 '20 at 20:12
  • majid hajibaba, Thank you for replying. Can you please tell me, How do i correct this issue. I gone through the link share by you. But unable to understand. Can you please tell me what is the solution? – Vipin Aug 01 '20 at 20:23
  • Please try use `Flush` and see result. Of-course, if you close file after write and the reopen it for new write I think every thing will be OK. – Majid Hajibaba Aug 01 '20 at 20:34
  • OK , what i need to do for flushing in C. Because it is a fifo which is open here... You mean i need to open and close continuously? Any other approach, Because you see my question , i don't get any issue in bash script, this behavior is only for C. – Vipin Aug 01 '20 at 20:49
  • 2
    Your posted C code won't compile. Plus it's never printing out any newlines that `tail` looks for. – Shawn Aug 01 '20 at 21:15
  • 2
    Reading https://man7.org/linux/man-pages/man7/pipe.7.html will explain why your program is killed when the reader process exits. – Shawn Aug 01 '20 at 21:18
  • Shawn, Thanks for pointing out , Related to compile, Are you talking about the return statement. If yes, I corrected it. But the code which i posted in stack overflow question is compiling(gcc testpipe.c). Could you please tell me what was error you are getting? Thanks for pointing out SIGPIPE signal , it helped me to search. After searching internet i got this post https://unix.stackexchange.com/questions/433345/under-what-conditions-exactly-does-sigpipe-happen, When i put signal(SIGPIPE, SIG_IGN); it wont terminate write if the read end terminate. But why in bash it was not raising SIGPIPE? – Vipin Aug 02 '20 at 04:27
  • And not got clue of tail -f and cat command. – Vipin Aug 02 '20 at 04:31
  • I got the answer for why bash is not reporting the SIGPIE, https://stackoverflow.com/a/2131618/8446934 and http://tiswww.case.edu/php/chet/bash/FAQ – Vipin Aug 02 '20 at 05:02
  • Now only one of my question i don't have answer cat and tail -f – Vipin Aug 02 '20 at 05:03
  • behavior of fifo vary for env i have script with working fifo pipe on android not running on lmde2, unless the env is known i doubt that question will answered – alecxs Aug 02 '20 at 10:09
  • Alecxs here i use arch Linux. – Vipin Aug 02 '20 at 10:54

0 Answers0