permaculture playing cards*
The moose likes C / C++ and the fly likes Non-Blocking read() in C Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » C / C++
Bookmark "Non-Blocking read() in C" Watch "Non-Blocking read() in C" New topic
Author

Non-Blocking read() in C

gajji sal
Greenhorn

Joined: Oct 22, 2012
Posts: 2
Hello everybody,

I am trying to read data from an SD card. But the library function of read() is in blocking mode (synchronous). I want to modify this function into non-blocking mode (asynchronous) as it is waste of time waiting for the read operation to complete and I have to do other tasks as well. Its in C.

Any tips and ideas on how to do it.

Thank you in advance
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1035
    
  10

gajji sal wrote:
Any tips and ideas on how to do it.

Read the card in a separate thread.
gajji sal
Greenhorn

Joined: Oct 22, 2012
Posts: 2
Thank you for the reply. Can you please elaborate a bit
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1035
    
  10

gajji sal wrote:Thank you for the reply. Can you please elaborate a bit


Your requirement is to do two things at the same time; read a card and continue processing. This implies you need two threads; in one you read the card and in the other you do whatever you want to do while waiting for the card to be read. You don't say what platform you are working on but I'm pretty certain that whatever it is there will be a version of 'pthreads' available. First check your C compiler library documentation and if that is not enough then use Google .

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Multiple threads is indeed one way to do this, but it has significant implications regarding how your whole program is written. Another way to do this -- and probably still the most common way on Linux -- is to use asynchronous I/O. All operating systems have the notion of calling a function to initiate an I/O operation, and then later checking to see when it's done, or being notified when it's done. On UNIXy systems, it's called select() or pselect(). Here's a tutorial on how to use it. Here is an introduction to doing the same sort of thing on Windows.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Non-Blocking read() in C
 
Similar Threads
Transforming a non blocking InputStream to a blocking one
Asynchronous web service
What is non blocking in nio
Windows, I/O is not interruptable.
cant create a connection keep getting a NotYetConnectedException