• C

Avoid Blocking Situation when Reading from a device


I would like to know how can I avoid this situation.  I open a device and try to write and read to the device.  Sometimes I will be blocked when I try to read from the device.  So how can I know the device does not response in 2 secs??  Is there any timer or some way I can avoid this situation??

Who is Participating?
stefan73Connect With a Mentor Commented:
Hi xenia27,
Assuming you're on Unix, you could use non-blocking I/O (you need unbuffered IO for this):

#include <sys/ioctl.h>

int mode = 1;
if ( ioctl(readFileDescriptor, FIONBIO, &mode) == -1) {
      /* complain about error here */

Now you can read some data into your buffer:

ssize_t num_bytes_read;

num_bytes_read = read(readFileDescriptor, &buffer, buffer_size);

In case the read would block, you'll get -1 and errno==EAGAIN:

    if(errno == EAGAIN){
        /* I/O would block process */
    } else {
        /* Some other error */
} else if (num_bytes_read == 0){
    /* EOF */
} else {
    /* Normal case */

This will not wait for two seconds - but you could easily implement this by checking the time since I/O start in case you're blocked. Just be aware that time.h's granularity of 1 second won't be fine enough - you'll get false alerts.


sunnycoderConnect With a Mentor Commented:
Hi xenia27,

alarm (2);
read ( ... );
After 2 seconds you will get an alarm and you would be out of read call and into SIGALRM signal handler.

If you are on a *nix, you can also use select with timeout to read from your file descriptor.
Also check

PaulCaswellConnect With a Mentor Commented:

As sunny points out, there are significant differences between operating systems in this area. If you could tell us what system you are coding for we may be able to help more.

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
> As sunny points out, there are significant differences between operating systems in this area.
Agree, I see in your profile you use to work with Linux (not my expertise area), but there are specific non-blocking access methods to read a serial port or any other device in Windows, by example, but using any OS, the key is to use multithreading.
Another way is to call some function that tells you how many bytes are available to be read, then read only that many.

xenia27Author Commented:
Sorry for replying after such a long time...my OS is a unix-base system.  I tried  "ioctl" but I am not sure whether it's working or not...

Here is my situation,

I open a device and a network socket.  I receive some commands from the network socket and send the command to the device.  I receive answer from the device and send the answer to the client through the network socket.  What I see is the client on the network socket cannot receive the answer on time...and I do receive some answer from the device but not able to send it on time...so next the client request some data through network socket, the client will receive some trash data from the previous request.  Any idea how can I solve this problem???

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.