LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-24-2014, 06:19 AM   #1
user@123
LQ Newbie
 
Registered: Mar 2014
Posts: 18

Rep: Reputation: Disabled
flush stdin without blocking.


Hello All,

Is there any way to clear stdin if it contains any character without blocking. I have tried fflush(stdin) but it did not work and i get to know its behavior is undefined and is not suitable for stdin. Can anyone tell any other way to flush stdin.
 
Old 06-24-2014, 06:41 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
You could read it in either a tight loop or a loop with some form of yield such as nanosleep() with little or no time. I'm unsure if you invoke nanosleep() with zero time if it then allow for non-blocking, but at the very least you can put in just 1 nanosecond.

I'd make a function either inline or just a regular function which invoked nanosleep with zero time and read the stdin file handle until there was no more data coming from that handle; that would flush stdin.
 
Old 06-24-2014, 06:47 AM   #3
user@123
LQ Newbie
 
Registered: Mar 2014
Posts: 18

Original Poster
Rep: Reputation: Disabled
@rtmistler thanks for immediate reply but here i don't want to wait if stdin is not having any data. Execution of program should proceed further if stdin is empty without waiting for input to receive.
 
Old 06-24-2014, 07:06 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
You can open the file descriptor and read it in non-blocking form.
 
  


Reply

Tags
c programming, linux



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
99% cpu usage due to flush-8:0 & flush-8:16 propofol Debian 4 09-09-2011 03:03 PM
[SOLVED] blocking and non blocking TCP send/recv problem golden_boy615 Programming 5 12-27-2010 03:27 PM
[SOLVED] C - For system calls, is blocking or non-blocking default? golmschenk Programming 4 03-23-2010 10:29 PM
[SOLVED] C - What's the difference between a blocking and a non-blocking call? golmschenk Programming 5 03-06-2010 06:45 PM
mysql "flush logs" or "flush tables with read lock", ... hangs deadeyes Linux - Server 4 01-19-2010 03:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:07 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration