LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-14-2010, 09:20 AM   #1
pjr
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Rep: Reputation: 0
Unhappy clearing stdin obtained from a pipe


I have a small program that reads stdin from a pipe using fgets. Now fgets blocks for the first line but after that it will not block.

THE CODE, my_echo.c -

int main(int argc, char **argv)
{ char buf [2000] ;
char* pc ;
printf("hello\n") ;
while (1)
{ buf[0] = (char) 0 ;
pc = fgets(buf, sizeof(buf), stdin);
if (pc != NULL)
printf("%s\n",buf);
}
return 0;
}

HOW ITS CALLED
* In terminal window 1: ./my_echo < my_fifo

* In terminal window 2: echo "1234" > my_fifo

* In terminal window 1: prints hello then 1234.

* Checking with ksysguard or top shows that my_echo
is consuming 40% of CPU time.
Adding a few printf's shows that the gets is not
blocking and returns a null pointer.

* In terminal window 2: echo "qwerty" > my_fifo

* In terminal window 1 qwerty prints.

MY PROBLEM : I want a read function that does in fact block so my program does not tie up CPU time, read does not block. Any ideas anyone?

Last edited by pjr; 03-14-2010 at 09:23 AM.
 
Old 03-14-2010, 01:45 PM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,150

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
The problem may be that you're using a while (1) with no termination condition (i.e., an "infinite CPU loop"). Consider something like this:
Code:
int main(int argc, char **argv) {
   char buf [2000] ;
   char* pc ;
   int waiting=0;
   printf("Hello\n") ;
// Quit if no input is received within 60 seconds.
   while (waiting < 60) {
      buf[0] = (char) 0 ;
      pc = fgets(buf, sizeof(buf), stdin);
      if (pc != NULL) {
         printf("%s\n",buf);
         waiting = 0;
      }
      else {
        ++waiting;
        sleep(1);
     }
   }
   printf("Goodbye.\n")
   return 0;
}
 
Old 03-14-2010, 11:09 PM   #3
pjr
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Hi,
thanks for the solution, but in the much larger program I am writing I need to find a way to make the pipe read block when the pipe is empty.
 
Old 03-16-2010, 06:43 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,150

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Ah, well, on my Fedora system, a man 7 pipe gives a nice overview of pipe operations. That overview suggests that, if you create your pipe using a mkfifo call, then the read will be blocked when the pipe is empty.
 
Old 03-17-2010, 08:20 AM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,455

Rep: Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843
I think the problem is that echo "1234" > my_fifo sends an EOF after finishing, perhaps something like this would work?
Code:
exec 3> my_fifo # opens my_fifo for writing
echo "1234" >&3
echo "qwerty" >&3
 
Old 03-18-2010, 07:58 AM   #6
pjr
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Hi ntubski,
thanks for the thoughts here but I really need the my_echo program to read from the fifo, and get blocked if the fifo is empty.
 
Old 03-18-2010, 09:22 AM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,455

Rep: Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843
It is blocked when the fifo is empty, it's not blocked when the fifo gets an EOF. That's why I suggested not closing the fifo from the other end...
 
Old 03-20-2010, 09:39 PM   #8
pjr
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Original Poster
Rep: Reputation: 0
You are right and it does work. Thanks!
 
  


Reply

Tags
block, fifo, pipe, stdin


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How is MAC being obtained? Randux Linux - Security 4 02-08-2009 11:22 AM
C programming - getchar() and clearing the stdin (new programmer) crm Programming 4 08-10-2006 01:54 PM
Command-line paranoia: Converting a pipe to stdin asciimonster Linux - General 3 06-28-2004 04:57 AM


All times are GMT -5. The time now is 09:49 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration