[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Avoid Blocking Situation when Reading from a device

Posted on 2005-05-04
6
Medium Priority
?
332 Views
Last Modified: 2010-04-02
Hi,

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


Xenia
0
Comment
Question by:xenia27
6 Comments
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 400 total points
ID: 13932977
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
http://www.eskimo.com/~scs/C-faq/q19.2.html

Cheers!
sunnycoder
0
 
LVL 16

Assisted Solution

by:PaulCaswell
PaulCaswell earned 400 total points
ID: 13933912
Xenia,

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.

Paul
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 400 total points
ID: 13935205
> 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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

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

0
 
LVL 12

Accepted Solution

by:
stefan73 earned 800 total points
ID: 13937969
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(num_bytes_read==-1){
    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.


Cheers!

Stefan
0
 

Author Comment

by:xenia27
ID: 13974764
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???


Xenia
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 how to use strings and some functions related to them in the C programming language.
Suggested Courses

834 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