Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between async and nonblocking in unix socket?

Tags:

I'm seeing such code in nginx:

if(fcntl(ngx_processes[s].channel[0], F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK) == -1) {
...
if (ioctl(ngx_processes[s].channel[0], FIOASYNC, &on) == -1) {
...

Anyone can tell me what's the difference between fcntl(s, F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK) and ioctl(s, FIOASYNC, &on) ,aren't async and nonblocking the same thing??

like image 295
cpuer Avatar asked Jun 07 '11 02:06

cpuer


People also ask

What is difference between asynchronous and non blocking?

Non-Blocking: It refers to the program that does not block the execution of further operations. Non-Blocking methods are executed asynchronously. Asynchronously means that the program may not necessarily execute line by line.

What is non blocking or asynchronous architecture?

Generally, a non-blocking architecture is based on method calls that, while they may execute for a long time on the worker thread, do not block the calling thread. If the calling thread needs to acquire information about or from the task the worker thread is executing, it is up to the calling thread to do that.


1 Answers

FIOASYNC toggles the O_ASYNC flag (which is usually set in open(2) or fcntl(2)) for a file descriptor, which will ask the kernel to send SIGIO or SIGPOLL to the process when the file descriptor is ready for IO.

O_ASYNC is not used often:

  • it is extremely difficult to properly handle IO in signal handlers; they are best left as tiny as possible
  • because signals interrupt the control flow of the program, they 'cost more' to run than standard system calls, such as select(2) or poll(2)
  • signals provide less information than other calls: they only report one fd ready vs many fds that might be ready.

The O_NONBLOCK doesn't provide any notification to the user process that a fd is ready for read(2) or write(2) -- instead, it changes the behavior of read(2) and write(2) and similar calls to return immediately if the file descriptor isn't ready for reading or writing. O_NONBLOCK is typically used in conjunction with select(2) or poll(2) or similar calls to guarantee that the main loop of a client or server won't block on one specific peer, and thus starve all its peers.

like image 110
sarnold Avatar answered Oct 11 '22 08:10

sarnold