1.IO
O_WRONLY|O_CREAT|O_TRUNC
int open(const char *name,int flags);
int open(const char *name,int flags,mode_t mode);
creat()
int fd;
fd=creat(file,0644)
#include <unistd.h>
ssize_t read(int fd,void *buf,size_t len);
非阻擋式讀取操作
read() 的調用在尚無數據可用時受到阻擋,讓調用立即返回,指出尚無數據可用這成為非阻擋式I/O
ssize_t 類型是int 有符號 返回值為-1
#include <unistd.h>
ssize_t write (int fd,const void *buf,size_t count)
EINTR
EAGAIN
ENIVAL 指定的文件描述符被映射到一個不允許寫入操作的對象
#include <unistd.h>
int fsync(int fd);
EBADF 所指定文件描述符無效或者未打開以備寫入
sync()
O_SYNC 會讓寫入操作的用戶時間和內核時間略微變差
int close(int fd);
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd,off_t pos,int origin)
origin SEEK_CUR SEEK_END SEEK_SET
linux 提供read() 和 write()的變體
pread(int fd,void *buf,size_t count,off_t pos);
pwrite(int fd,void *buf,size_t count,off_t pos);
截短文件的系統調用
#include <unistd.h>
#include <sys/types.h>
int ftruncate(int fd,off_t len);
int truncate(const char*path,off_t len);
linux 提供了三種多任務IO解決方案 多任務IO讓一個應用程序可以同時服務多個文件描述符,以及在其中有任何一個就緒都可以讀取或者寫入時收到通知而不會受到阻攔
select poll epoll
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
[火星人 ] linux 記錄已經有341次圍觀