The Unix programming book I have doesn't explain this very well. It says that some I/O requests can block indefinitely while waiting for input from certain sources. This part I understand. But then it shows how to prevent this by setting O_NONBLOCK and then reading blocks of characters (sorry, I couldn't think of a better word) from standard input until some variable equals 0. The code it gives is uncommented and all it gives for explanation is a vague description of what output the program produces. I'm guessing it assumes the reader already has a good understanding of nonblocking I/O.
So how does this work anyway? How does nonblocking I/O "not block"? Does it sleep until the stream is ready for read/write and allow other processing to occur in the meantime? Is nonblocking I/O basically the same thing as asynchronous I/O or is it something entirely different? I'd like some example code in C illustrating how to use it if that's at all possible.
2 replies to this topic
#1
Posted 28 October 2011 - 10:56 AM
Programming is a journey, not a destination.
|
|
|
#2
Posted 28 October 2011 - 11:39 AM
nonblocking = asynchronous I/O
from what I understand; at least in socket programming.
In socket programming, with blocking sockets, you just send and receive data as you want, and you can be sure that it will get there and be received in the order you used; with nonblocking sockets, you have a main program loop, similar to WM_* message processing, like in Win32 user-interface programming. With nonblocking sockets, the send () function (for example) returns right away, no matter if the data has been sent, yet, or not. With blocking sockets, the function doesn't return until the data is sent.
I hope this is enough explanation; I don't have much experience in C, so I don't think I'd have too good of an example code. Though I can provide a bit of JavaScript AJAX example:
from what I understand; at least in socket programming.
In socket programming, with blocking sockets, you just send and receive data as you want, and you can be sure that it will get there and be received in the order you used; with nonblocking sockets, you have a main program loop, similar to WM_* message processing, like in Win32 user-interface programming. With nonblocking sockets, the send () function (for example) returns right away, no matter if the data has been sent, yet, or not. With blocking sockets, the function doesn't return until the data is sent.
I hope this is enough explanation; I don't have much experience in C, so I don't think I'd have too good of an example code. Though I can provide a bit of JavaScript AJAX example:
var http;
if (navigator.appName == 'Microsoft Internet Explorer') http= new ActiveXObject ("Microsoft.XMLHTTP");
else http= new XMLHttpRequest ();
http.open ("POST", "/login.php");
http.onreadystatechange= work_request;
http.send ("username=user1&password=Iwillnotevertellyouthis123");
// Do some other stuff while the request is being processed.
// For example, distract the user, so that the user doesn't notice
// how slow the dial-up connection is.
var color= prompt ("What is your favorite color?", "use default");
function work_request (){
// If the request is processed already (response already received by client).
if (http.readyState == 4){
// If the status is OK (if no errors).
if (http.status == 200){
// If the request was successfully responded to, the response should be in <object>.responseText
alert ("Logged in successfully! Here's the response: \r\n\r\n" + http.responseText);
} else {
alert ("Error. HTTP status code: " + http.status);
}
}
}
#3
Posted 29 October 2011 - 12:07 PM
I would like to differentiate non blocking I/O from Asynchronous I/O in general. That is why they have different API.
Technically a non blocking call such as read() when called with O_NonBlock returns immediately whether data is available to read or not. If not it returns some error and program can move ahead doing some thing. However, it DOES NOT provide an inherent way to call read again whenever data is available.
To achieve that you have to use a busy waiting loop
while (1)
{
read(); // Non blocking read, returns an error if there is no data to read. However, it will only read again in next iteration.
// do something else
}
In the above situation it is possible that you go through a thousand iterations and still no data is available to read. It wastes a lot of cpu cycles.
Compared to this in asynchronous I/O you register a function call aio_read or something which the OS calls itself WHENEVER data is available. It is more like an Event driven mechanism vs looping.
regards,
Fayyaz
Technically a non blocking call such as read() when called with O_NonBlock returns immediately whether data is available to read or not. If not it returns some error and program can move ahead doing some thing. However, it DOES NOT provide an inherent way to call read again whenever data is available.
To achieve that you have to use a busy waiting loop
while (1)
{
read(); // Non blocking read, returns an error if there is no data to read. However, it will only read again in next iteration.
// do something else
}
In the above situation it is possible that you go through a thousand iterations and still no data is available to read. It wastes a lot of cpu cycles.
Compared to this in asynchronous I/O you register a function call aio_read or something which the OS calls itself WHENEVER data is available. It is more like an Event driven mechanism vs looping.
regards,
Fayyaz
Today is the first day of the rest of my life
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









