1. The READ function reads data from open devices or files.
# Include <unistd. h>
Ssize_t read (int fd, void * Buf, size_t count );
Returned value: the number of bytes read successfully. If an error is returned,-1 is returned and errno is set. If the value has reached the end of the file before the read is called
Read returns 0
The read data is stored in the buffer Buf, and the current read/write position of the file is moved back. Note that the read/write location may be different from the read/write location when the C standard I/O library is used. This read/write location is recorded in the kernel, when the C standard I/O library is used, the read/write location is in the user space I/O buffer.
2. The Write function writes data to an opened device or file.
# Include <unistd. h>
Ssize_t write (int fd, const void * Buf, size_t count );
Returned value: the number of written bytes is returned successfully. If an error occurs,-1 is returned and errno is set.
3. Block)
When a process calls a blocked system function, the process is put into sleep state. At this time, the kernel Schedules other processes to run, it is not possible to continue running until the waiting event occurs (for example, the packet is received on the network or the sleep time specified by sleep is called.
The sleep state is relative to the running state. In the Linux kernel, running processes are classified into two types: being scheduled and ready.
Assume that multiple devices are monitored at the same time. If read (device 1) is blocked, as long as device 1 does not have data, it will always block the read call of device 1, even if data arrives at device 2, it cannot be processed. Using non-blocking I/O can avoid timely processing of device 2.
If the o_nonblock flag is specified when a device is opened, read/write will not be blocked. Take read as an example. If the device does not read data for the moment,-1 is returned, and errno is set to ewouldblock (or eagain, the two macros have the same value ), it indicates that it should have been blocked here (wocould block, virtual tone). In fact, it does not block but directly returns an error. The caller should try to read it again (again ). This kind of behavior is called poll. The caller only queries it, instead of blocking it, so that multiple devices can be monitored at the same time.
Non-blocking I/O has a drawback. If no data has been reached for all devices, the caller needs to perform repeated queries for useless work. If the congestion exists, the operating system can schedule other processes for execution, it won't be useless.
The Select (2) function can block multiple devices at the same time, and can also set the timeout time for blocking wait, thus successfully solving this problem.
Iv. Blocking and non-blocking read terminal instances
Non-blocking read Terminal
When the program starts execution, the files automatically opened on the 0, 1, and 2 file descriptors are terminals, but there is no o_nonblock flag. Therefore, standard read input is blocked. You can re-open the device file/dev/tty (indicating the current terminal), which indicates the o_nonblock flag.
# Include <unistd. h>
# Include <fcntl. h>
# Include <errno. h>
# Include <string. h>
# Include <stdlib. h>
# Define msg_try "Try again \ n"
Int main (void)
{
Char Buf [10];
Int FD, N;
FD = open ("/dev/tty", o_rdonly | o_nonblock );
If (FD <0 ){
Perror ("Open/dev/tty ");
Exit (1 );
}
Tryagain:
N = read (FD, Buf, 10 );
If (n <0 ){
If (errno = eagain ){
Sleep (1 );
Write (stdout_fileno, msg_try,
Strlen (msg_try ));
Goto tryagain;
}
Perror ("Read/dev/tty ");
Exit (1 );
}
Write (stdout_fileno, Buf, N );
Close (FD );
Return 0;
}
Blocking read Terminal
# Include <unistd. h>
# Include <stdlib. h>
Int main (void)
{
Char Buf [10];
Int N;
N = read (stdin_fileno, Buf, 10 );
If (n <0 ){
Perror ("read stdin_fileno ");
Exit (1 );
}
Write (stdout_fileno, Buf, N );
Return 0 ;}
Http://blog.chinaunix.net/space.php? Uid = 22086460 & Do = Blog & id = 404970