Non-blocking IO is relative to the traditional blocking IO.
The first thing we need to figure out is what is blocking IO. Apue points out that system calls fall into two categories, low-speed system calls and others, where low-speed system calls are a class of system calls that can cause a process to block forever. However, the system calls related to disk IO are an exception.
We take read and write as an example, the Read function reads stdin, if it is blocking IO, then:
If we do not enter data, then the read function will block until we enter the data.
If non-blocking IO, then:
If there is data, read and then return, if there is no input, then directly return -1,errno to Eagain
We do an experiment with write:
#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<sys/wait.h>#include<errno.h>#include<signal.h>Charbuf[500000];intMainintargcConst Char*argv[]) { intNtowrite, Nwrite; Ntowrite= Read (Stdin_fileno, buf,sizeofbuf); fprintf (stderr,"Read%d bytes\n", Ntowrite); Activate_nonblock (Stdout_fileno, O_nonblock); Char*ptr =buf; intNleft = Ntowrite;//number of bytes remaining while(Nleft >0) {errno=0; Nwrite=write (Stdout_fileno, PTR, nleft); fprintf (stderr,"Nwrite =%d, errno =%d\n", Nwrite, errno); if(Nwrite >0) {ptr+=Nwrite; Nleft-=Nwrite; }} deactivate_nonblock (Stdout_fileno); return 0;}
The program writes 500,000 bytes to the standard output.
If you use:
./test < test.mkv > Temp.file
Then the output is:
Read 500000= 500000, errno = 0
Because the disk IO is faster, it can be written one at a time, the following we use the terminal:
./test < test.mkv 2> stderr.txt
This line of command prints 500000 of the content to the screen, while writing the fprintf recorded information through the standard error stream to Stderr.txt.
We view Stderr.txt files:
Read 500000= 12708, errno ==-1, errno = one=-1, errno = one=-1, errno = one= -1, errno = one=-1, errno = one=-1, errno = one= 11687, errno ==-1, errno = 11
..................................................
Nwrite =-1, errno = 11
Nwrite =-1, errno = 11
Nwrite =-1, errno = 11
Nwrite =-1, errno = 11
Nwrite =-1, errno = 11
Nwrite =-1, errno = 11
Nwrite =-1, errno = 11
Nwrite =-1, errno = 11
Nwrite =-1, errno = 11
Nwrite = 1786, errno = 0
With the command statistics, the total read is 15,247 times, where 1 times is returned 15,203 times, indicating that the number of successful reads is 44 times.
In the example above, this non-blocking IO is called "polling", which is obviously an inefficient way, and non-blocking IO is often used in conjunction with the IO multiplexing model.
Also, set the FD to block and non-blocking function code as follows:
voidActivate_nonblock (intFD) { intret; intFlags =fcntl (FD, F_GETFL); if(Flags = =-1) Err_exit ("Fcntl"); Flags|=O_nonblock; RET=fcntl (FD, F_SETFL, flags); if(ret = =-1) Err_exit ("Fcntl");}voidDeactivate_nonblock (intFD) { intret; intFlags =fcntl (FD, F_GETFL); if(Flags = =-1) Err_exit ("Fcntl"); Flags&= ~O_nonblock; RET=fcntl (FD, F_SETFL, flags); if(ret = =-1) Err_exit ("Fcntl");}
Not to be continued.
Non-blocking IO under Linux (i)