Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Non-Blocking read() in C

 
gajji sal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gajji sal wrote:
Any tips and ideas on how to do it.

Read the card in a separate thread.
 
gajji sal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply. Can you please elaborate a bit
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic