歡迎您光臨本站 註冊首頁

linux 記錄

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

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>


int select (int n,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,struct timeval *timeout)

poll() 系統調用 解決了select()的若干缺點
#include <sys/poll.h>
int poll(struct pollfd *fds,unsigned int nfds,int timeout);
struct pollfd{
int fd;
short events;
short revents;
};

VFS 單一系統調用可以讀區任何存儲媒介上的任何操作系統 當一個應用程序進行read()系統調用時,C鏈接庫所提供的系統定義在編譯時會被轉換為適當的trap
語句.當一個用戶空間進程陷入內核后,控制權會通過系統調用處理程序交給read()系統調用,於是內核可以找出支持所指定的文件描述符的是何種對象.然後內核會調用與
背後對象相應的讀取函數.對文件系統而言,此函數是文件系統程序代碼的一部分,它會左它該做的事情並且將數據返回給用戶空間的read()調用.於是會從系統調用程序返回,
而系統調用處理程序會將數據複製回用戶空間,接著從用戶空間的read()系統調用返回,然後進程會繼續執行


#inlcude <stdio.h>
FILE *fopen(const char *path,const char *mode)
int fgetc (FILE *stream)
int ungetc(int c,FILE *stream)
char *fgets (char *str,int size,FILE *stream)
int fputc(int c,FILE *stream)
int fputs(const char *str,FILE *stream)
size_t fwrite(void *buf,sizez_t size,size_t nr,FILE *stream);
int fseek(FILE *stream,long offset,int whence);
rewind(stream) = fseek(stream,0,SEEK_SET)




[火星人 ] linux 記錄已經有341次圍觀

http://coctec.com/docs/linux/show-post-48905.html