Linux Learning Notes IO

Source: Internet
Author: User
Tags flock posix

First, the basic knowledge.

1: Normal IO type.

1, non-blocking IO: Issue IO operations such as open/read/write, and make these operations not permanently blocked. When it cannot be completed, an error is returned immediately.

1) non-blocking two kinds of flag: Specify the flag: O_nonblock.

2) Non-blocking semantics: changes to the file status flag affect all users of the same file table entry, but are not related to access to the same device through other file table entries. ( associated with a file table entry)

2, asynchronous IO (asynchronous IO).

1) Mechanism: When the descriptor preparation number can process IO, a signal notification process is sent.

1-It can only work if the descriptor reference interrupts the device or network.

2-this signal is only one for each process.

3:io Multi-Channel transfer (IO multiplexing).

1) Mechanism: Constructs a list that contains a descriptor, and then calls a function. The function returns when a process IO operation is ready in the descriptor.

2) The system does not voluntarily inform any information. We need to use the function to fetch the file descriptor.

4:posix Asynchronous IO: Provides an example set of consistent methods for asynchronous IO for different types of files.

1) An asynchronous IO operation must explicitly specify an offset. The asynchronous IO interface does not affect file offsets maintained by the operating system.

2) When using append mode, the Aio_offset field is ignored by the system.

5: Store mapped IO.

1) Mechanism: Map a disk file to a buffer in the storage space.

2: Terminal IO.

1, two modes of operation.

1) canonical mode input processing: By line processing, end by behavior. The default is canonical mode.

2) Non-canonical mode input processing: According to the respective rules.

3) in the canonical mode, there are several situations that can result in read return: The number of bytes requested has been read, the line delimiter is received, and the signal is captured.

4) Four cases of non-canonical mode. Time and Min are the two variables of the C_CC array in the TERMIOS structure.

\ MIN > 0 MIN = = 0
Time > 0

A: Before the timer expires,

Read returns [MIN, Nbytes];

If the timer expires,

Read returns [1, MIN];

(time= bytes between timers.) The caller is blocked indefinitely. )

C: Before the timer expires,

Read returns [1, nbytes];

If the timer expires,

Read returns 0.

(Time=read timer)

Time = = 0

B: When there is data available,

Read return: [MIN, Nbytes]

(callers can block indefinitely)

D:read immediate return [0, Nbytes]

2,POSIX.1 defines 11 special input characters, 9 of which can be changed.

3, terminal equipment is usually located in the kernel of the end-driver program control, each terminal equipment by an input queue and an output queue.

1) When the input queue fills up, the system is dependent on the implementation.

2) When the output queue fills up, the process usually sleeps. Until the queue has free space.

4, summary of terminal IO functions.

Function Description
Tcgetattr Get Properties
Tcsetattr Setting properties
Cfgetispeed Get input speed
Cfgetospeed Get Output speed
Cfsetispeed Set the input speed
Cfsetospeed Setting the output speed
Tcdrain Wait until all outputs are transmitted
Tcflow Suspend transmit or receive
Tcflush Flush pending inputs and/or outputs
Tcsendbreak Send break character
Tcgetpgrp Get foreground process Group ID
Tcsetpgrp Set the foreground process group ID
Tcgetsid Gets the process group ID of the session first process that controls the TTY

5, baud rate: refers to bits per second (bit per second).

1) The general system defines the baud rate outside the protocol.

6, pseudo terminal.

1) Meaning: For an application, it looks like a terminal. But it's actually not a real terminal.

2) Several classic uses P581 (not fully understood. )。

1-Network logon server.

2-Window System terminal emulation.

3-script program.

4-expect program.

5-run a synergistic process.

6-Watch the output of a long-running program.

3:io Related: Record lock function

1, Record lock function (Byte range lock): When a process is reading/modifying a portion of a file, using a record lock prevents other processes from modifying the same file area.

1) It can lock only a section of the file. More precise in control.

2) shared read and exclusive write control modes are similar to read and write locks.

3) When a single process sets a second lock in the same region, the previous lock is overwritten.

    4) When the lock is read, the descriptor must be read open, and the write lock must be write-open.

2, implicit inheritance and release of locks.

1) When a process terminates, the locks it establishes are all released. When a descriptor is closed, the lock set by the process through the descriptor is also freed.

2) The child process generated by the fork does not inherit the lock set by the parent process.

3) After Exec executes, the new program can inherit the lock of the original execution program ( and there seems to be a contradiction in the previous article).

3, Cooperation Process: all functions of a library handle record locks in a consistent manner, which is called the process set that accesses the database using these functions as a cooperative process.

4, Mandatory lock: This lock allows the kernel to see each IO function of the vehicle, verifying that the calling process violates a lock on the file being accessed.

1) Open with: Open its set group ID bit for a specific file, turn off group execution bit ( incomprehensible).

2) What is the meaning of the proposed lock?

3) There are defects in the compulsory lock, which can be avoided.

4: Others.

1, polling: a period of time, call the expected process/function.

1) in a multitasking system, avoid using this method as much as possible.

In 2,bsd derived systems, asynchronous IO is a combination of signal sigio and Sigurg.

1) SIGIO: Universal Asynchronous IO signal.

2) Sigurg: Notifies the process that the out-of-band data on the network connection has arrived.

Second, the correlation function.

1: Multi-Channel Adapter IO

<sys/Select.h>1A multi-channel query function.IntSelectint MAXFDP1, Fd_set *restrict Readfds, Fd_set *restrict Writefds, Fd_set *restrict Exceptfds,struct Timeval *Restrict tvptr);//1 Input: Descriptor, Descriptor condition (read/write/exception), wait time.//2 Output: Descriptor total, prepared descriptor condition//3 parameter tvptr: ==null waits Forever, ==0 does not wait,!=0 waits for the concrete time. No const, cannot be guaranteed not to be modified.//4 Fd_set type parameter (write/read/exception condition): This parameter is not cared for when the parameter is ==null.//5 parameter MAXFDP1: Maximum file descriptor number +1.//6 return Value: Error -1,0 indicates a descriptor that is not prepared, and >0 the sum of the prepared descriptors.int Pselect (int MAXFDP1, Fd_set *restrict Readfds, Fd_set *restrict Writefds, Fd_set *restrict Exceptfds,Conststruct Timespec *restrict tsptr,Const SIGSET_T *Restrict Sigmask);//1 provides high accuracy, and the timeout value cannot be changed.//2 can use the signal shielding word.INT poll (struct POLLFD fdarray[], nfds_t Nfds,IntTimeout);//1 Write the descriptor that we are interested in into the POLLFD array.//2 parameter Nfds: Specifies the number of array elements.//3 parameters timeout:-1 always wait, 0 do not wait, >0 wait for the specific time.2 data type Fd_set processing function/Macro (depending on whether you implement a function or a macro).int Fd_isset (int FD, Fd_set *fdset);//Indicates whether the positioning is turned on.void Fd_clr (int FD, Fd_set *fdset);//Clears a bit.void Fd_set (int FD, Fd_set *fdset);//Sets a bit.void Fd_zero (Fd_set *fdset);// All bits are set to 0. // 1 Fd_set type, a file descriptor occupies one bit. In general, a bitwise 1 indicates that the condition is satisfied (not specific to the system implementation).  3 data type POLLFD. struct POLLFD {int FD; Span style= "color: #008000;"    >// file decriptor to check, or <0 to ignore. short events;    // events of interest on FD. short revents;  // events, occurred on Fd.// 1 if you use this structure, you need to return the value until the specific event    

2:posix Asynchronous IO

1AIO control block basic structure (specific system can be added on the standard)structAIOCB {int aio_fildes;//File descriptor off_t Aio_offset;//File offset for IOVolatilevoid *aio_buf;//Buffer for IO size_t aio_nbytes;//Number of bytes to transferint Aio_reqprio;//Prioritystruct Sigevent aio_sigevent;//Signal informationint Aio_opcode;//Operation for List IO}2Basic read and write operations.int Aio_read (struct AIOCB *AIOCB);int Aio_write (struct AIOCB *AIOCB);3Forces the pending asynchronous operation to be written directly.int Aio_fsync (int OP,struct AIOCB *AIOCB);//!!! The specific function is not fully confirmed!!!//1 op parameters have two options: O_dsync---fdatasync, o_sync---fsync4Gets an operation completion status.int Aio_error (Conststruct AIOCB *AIOCB);//1 return value: 0 succeeded,-1 failed errno save error message, einprogress operation Wait,5Gets the return value of the asynchronous operation, if successful. ssize_t Aio_return (Conststruct AIOCB *AIOCB);//1 once called, the system clears the return value.6Only the asynchronous operation is left unfinished, and the process can be blocked by this function. Until the asynchronous operation is complete.int Aio_suspend (Conststruct AIOCB *Const list[],int nent,Conststruct Timespec *Timeout);7Cancels the pending asynchronous operation.int Aio_cancel (int fd, struct AIOCB *AIOCB);  // issuing a cancellation order is not a definite cancellation. /// 1 return value: Aio_alldone All operations completed, no cancellation required, aio_canceled succeeded, aio_notcanceled at least one operation was not canceled, 1 call failed, error saved in errno8  Submit a list of AIO control blocks int lio_listio ( int mode, struct AIOCB *restrict const list[restrict], int nent,  struct sigevent *restrict Sigev); // 1 parameter mode:lio_wait, lio_nowait              

3: Store mapped IO.

1Maps a given file to a storage area.void *nmap (void *addr, size_t len,int prot,int flag,IntFD, off_t off);//1 parameter addr: Specifies the starting address of the mapped area. Set to 0 to get maximum portability.//2 parameter prot: Permissions for the storage area (read/write/execute)//3 parameter flag:map_fixed,map_shared,map_private.2 change mapping permissions. int mprotect (void *addr, size_t len, int prot);  3 writes the store data to the mapped file. int Msync (void *addr, size_t len, int flags); // 1 parameter flags:ms_async, Ms_sync  4 de-map area. int Munmap (void *addr, size_t len); // call this function to not write the store data to the file         

4: A variety of reading and writing functions.

1 Read/Write multiple buffers ssize_t readv (int fd, const struct  Iovec *iov, int iovcnt); ssize_t Writev (int fd, const struct Iovec *iov, int//2 read/ write n bytes of data ssize_t Readn (int FD, void *buf, size_t nbytes); ssize_t writen (int fd, void *buf, size_t nbytes);   

5: Record Lock

intFcnt1 (intFdintCmdstructFlock *flockptr);structflock{ ShortL_type;//F_rdlck (Shared read lock), F_WRLCK (exclusive write lock), F_unlck (Unlocking an area)     ShortL_whence;//the start byte offset of the Add/unlock area. Selectable values: Seek_set, Seek_cur, Seek_endoff_t L_start;//the start byte offset of the Add/unlock area. off_t L_len;//The region byte length. equals 0 o'clock, which indicates that the maximum offset can be added instead of 0. pid_t L_pid;//Current Process id (cmd = = f_getlk)}//1 for record locks, the parameter cmd is: f_getlk (Get lock status), F_SETLK (set Lock), F_SETLKW (blocking version of Set lock, w==wait). //2 parameter FLOCKPTR a structure that records a lock. //3 The operation to get/set the lock using parameter cmd is not an atomic operation. 

Three

Linux Learning Notes IO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.