Avoid Blocking Situation when Reading from a device

Posted on 2005-05-04
Last Modified: 2010-04-02

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??

Question by:xenia27
    LVL 45

    Assisted Solution

    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

    LVL 16

    Assisted Solution


    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.

    LVL 55

    Assisted Solution

    by:Jaime Olivares
    > 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.
    LVL 22

    Expert Comment

    Another way is to call some function that tells you how many bytes are available to be read, then read only that many.

    LVL 12

    Accepted Solution

    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.



    Author Comment

    Sorry for replying after such a long 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 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???


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now