調用 fcntl() 設置文件描述符 O_NONBLOCK 標誌,調用 read() 和 write() 再也不阻塞,有可能會返回 -1 並設置 errno 爲 EAGAIN。node
用於鎖住文件的某一部分,支持讀鎖和寫鎖。因爲鎖信息 lockf_entry 和 v-node 對應,系統並不關心也不知道鎖和文件描述符的對應關係。若是同一個進程有多個文件描述符(包括 dup() 和 open())指向同一個文件,對任意一個 close() 都會致使所有解鎖。若是在文件尾部加鎖,文件向後增加時,新增內容也加了鎖。fork() 子進程不繼承父進程的鎖。程序員
建議性鎖:由程序員主動檢測鎖,有鎖時主動保證不進行I/O操做;異步
強制性鎖:無論有沒有鎖,直接進行I/O,由內核保證鎖起做用。async
struct flock { short int l_type; /* Type of lock: F_RDLCK, F_WRLCK, or F_UNLCK. */ short int l_whence; /* Where `l_start' is relative to (like `lseek'). */ __off_t l_start; /* Offset where the lock begins. */ __off_t l_len; /* Size of the locked area; zero means until EOF. */ __pid_t l_pid; /* Process holding the lock. */ }; fcntl(fd, F_SETLK or F_SETLKW or F_GETLK, & fl);
主要是用於同時等待多個I/O。阻塞在等待函數 select()、poll() 上,而非特定文件描述符上。select() 和 poll() 不重啓。函數
/* Check the first NFDS descriptors each in READFDS (if not NULL) for read readiness, in WRITEFDS (if not NULL) for write readiness, and in EXCEPTFDS (if not NULL) for exceptional conditions. If TIMEOUT is not NULL, time out after waiting the interval specified therein. Returns the number of ready descriptors, or -1 for errors. This function is a cancellation point and therefore not marked with __THROW. */ // nfds 是 readfds、writefds、exceptfds 裏最大的描述符序號 +1,可能夠是 FD_SETSIZE // timeout 用來設置超時,NULL 爲永遠阻塞 // 返回 -1 表示出錯,如產生信號;其它表示準備好的描述符數量 // readfds、writefds、exceptfds 在返回後保存的是準備好的描述符 extern int select (int __nfds, fd_set *__restrict __readfds, fd_set *__restrict __writefds, fd_set *__restrict __exceptfds, struct timeval *__restrict __timeout); #define FD_SET(fd, fdsetp) __FD_SET (fd, fdsetp) #define FD_CLR(fd, fdsetp) __FD_CLR (fd, fdsetp) #define FD_ISSET(fd, fdsetp) __FD_ISSET (fd, fdsetp) #define FD_ZERO(fdsetp) __FD_ZERO (fdsetp) // 調用時屏蔽 sigmask 裏的信號 extern int pselect (int __nfds, fd_set *__restrict __readfds, fd_set *__restrict __writefds, fd_set *__restrict __exceptfds, const struct timespec *__restrict __timeout, const __sigset_t *__restrict __sigmask);
#define POLLIN 0x001 /* There is data to read. */ #define POLLPRI 0x002 /* There is urgent data to read. */ #define POLLOUT 0x004 /* Writing now will not block. */ /* Data structure describing a polling request. */ struct pollfd { int fd; /* File descriptor to poll. */ short int events; /* Types of events poller cares about. */ short int revents; /* Types of events that actually occurred. */ }; int poll (struct pollfd *__fds, nfds_t __nfds /* fds 的數量 */, int __timeout /* -1 爲永遠阻塞,其它爲毫秒數 */);
/* Enqueue read request for given number of bytes and the given priority. */ extern int aio_read (struct aiocb *__aiocbp) __THROW __nonnull ((1)); /* Enqueue write request for given number of bytes and the given priority. */ extern int aio_write (struct aiocb *__aiocbp) __THROW __nonnull ((1)); /* Retrieve error status associated with AIOCBP. */ extern int aio_error (const struct aiocb *__aiocbp) __THROW __nonnull ((1)); /* Return status associated with AIOCBP. */ extern __ssize_t aio_return (struct aiocb *__aiocbp) __THROW __nonnull ((1)); /* Try to cancel asynchronous I/O requests outstanding against file descriptor FILDES. */ extern int aio_cancel (int __fildes, struct aiocb *__aiocbp) __THROW; /* Suspend calling thread until at least one of the asynchronous I/O operations referenced by LIST has completed. This function is a cancellation point and therefore not marked with __THROW. */ extern int aio_suspend (const struct aiocb *const __list[], int __nent, const struct timespec *__restrict __timeout) __nonnull ((1)); /* Force all operations associated with file desriptor described by `aio_fildes' member of AIOCBP. */ extern int aio_fsync (int __operation, struct aiocb *__aiocbp) __THROW __nonnull ((2)); /* Initiate list of I/O requests. */ extern int lio_listio (int __mode, struct aiocb *const __list[__restrict_arr], int __nent, struct sigevent *__restrict __sig) __THROW __nonnull ((2));
將數據讀入多個緩衝區,或從多個緩衝區寫入數據線程
/* Structure for scatter/gather I/O. */ struct iovec { void *iov_base; /* Pointer to data. */ size_t iov_len; /* Length of data. */ }; /* Read data from file descriptor FD, and put the result in the buffers described by IOVEC, which is a vector of COUNT 'struct iovec's. The buffers are filled in the order specified. Operates just like 'read' (see <unistd.h>) except that data are put in IOVEC instead of a contiguous buffer. This function is a cancellation point and therefore not marked with __THROW. */ extern ssize_t readv (int __fd, const struct iovec *__iovec, int __count) __wur; /* Write data pointed by the buffers described by IOVEC, which is a vector of COUNT 'struct iovec's, to file descriptor FD. The data is written in the order specified. Operates just like 'write' (see <unistd.h>) except that the data are taken from IOVEC instead of a contiguous buffer. This function is a cancellation point and therefore not marked with __THROW. */ extern ssize_t writev (int __fd, const struct iovec *__iovec, int __count) __wur;
將磁盤文件映射到內存中,像操做緩衝區同樣操做文件。rest
#define PROT_READ 0x1 /* Page can be read. */ #define PROT_WRITE 0x2 /* Page can be written. */ #define PROT_EXEC 0x4 /* Page can be executed. */ #define MAP_SHARED 0x01 /* 更新到文件 */ #define MAP_PRIVATE 0x02 /* 不更新到文件 */ #define MAP_FIXED 0x10 /* 要求地址必須是 addr */ void *mmap (void *__addr, size_t __len, int __prot, int __flags, int __fd, __off_t __offset); int munmap (void *__addr, size_t __len); int mprotect (void *__addr, size_t __len, int __prot); #define MS_ASYNC 1 /* Sync memory asynchronously. */ #define MS_SYNC 4 /* Synchronous memory sync. */ int msync (void *__addr, size_t __len, int __flags);