APUE 函数详解系列(一) lseek/stat/poll

来源:互联网 发布:java jasperreports 编辑:程序博客网 时间:2024/06/05 14:58

1. lseek - reposition read/write file offset

  • SYSNOPSIS
      #include<sys/types.h> and <unistd.h>
      off_t lseek(int fd, off_t offset,int whence);

  • DESCRIPTION
      The lseek() function repositions the offset of the open file associated descriptor fd to the argument offset according to the directive whence as follows:
      SEEK_SET:The offset is set to offset bytes.
      SEEK_CUR:The offset is set to its current location plus offset bytes.
      SEEK_END:The offset is set to the size of file plus offset bytes.

  • RETURE VALUE
      Upon successful completion, lseek() returns the resulting offset location as measured in bytes from the begining of the file. On error, the value (off_t) -1 is returned and errno is set to indicate the error.

  • 特别使用方式:
      1) 将读写位置移到文件开头:lseek(int fd, 0, SEEK_SET);
      2) 将读写位置一道文件末尾:lseek(int fd, 0, SEEK_END);
      3) 取得目前文件位置:lseek(int fd, 0 , SEEK_CUR);


2. stat - display file or file system status

  • SYSNOPSIS
#include<sys/stat.h> . <unistd.h> . <sys/types.h>int stat(const char *path, struct stat *buf)
  • DESCRIPTION
    This function return information about a file.
struct stat{    dev_t    st_dev;    ino_t    st_ino;    mode_t   st_mode;    //文件类型和许可权限    nlink_t  st_nlink;   //文件连接数    uid_t    st_uid;     //用户所有者ID    gid_t    st_gid;     //所属组ID    time_t   st_mtime;   //文件最后修改时间    time_t   st_atime;   //文件最后访问时间    time_t   st_ctime;   //文件属性最后修改时间}

3.Poll

类似select,接口不同

#include<poll.h>int poll(struct pollfd fdarray[], nfds_t nfds, int timeout);struct pollfd{    int fd;    short events;   //events of interest on fd 请求事件    short revents;  //events that occurred on fd 返回事件}
nfds指定fdarray数组中元素,events告诉内核我们关心的是每个描述符哪些事件,返回时,revents成员由内核设置,用于说明每个描述符发生了哪些事件。                                poll中events和revents标志
标志名 说明 以下四个测试可读性 POLLIN 可以不阻塞读取普通数据和优先级数据(等效下两个 POLLRDNORM 可以不阻塞读取普通数据 POLLRDBAND 可以不阻塞读取优先级数据 POLLPRI 可以不阻塞读取高优先级数据 以下三个测试可写性 POLLOUT 可以不阻塞写普通数据 POLLWRNORM 与POLLOUT相同 POLLWRBAND 可以不阻塞写优先级数据 以下三个测试异常条件 POLLERR 已出错 POLLHUP 已挂断 POLLNVAL 描述符没有引用一个打开文件
0 0