Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux / Unix 系统编程手册 #62

Open
magicdawn opened this issue Feb 25, 2016 · 1 comment
Open

Linux / Unix 系统编程手册 #62

magicdawn opened this issue Feb 25, 2016 · 1 comment
Labels

Comments

@magicdawn
Copy link
Owner

读书笔记

@magicdawn magicdawn added the Book label Feb 25, 2016
@magicdawn
Copy link
Owner Author

ch63

io model

  • I/O multiplexing (select poll)
  • Singal driven I/O
  • epoll( Linux specific API)

Level triggered & Edge Triggered

水平沿触发 和 边缘触发 http://flychao88.iteye.com/blog/2187267

  • Level-triggered notification: A file descriptor is considered to be ready if it is possible to perform an I/O system call without blocking.
  • Edge-triggered notification: Notification is provided if there is I/O activity (e.g., new input) on a file descriptor since it was last monitored.

select

#include <sys/time.h> /* For portability */ 
#include <sys/select.h>

void FD_ZERO(fd_set *fdset); // 清空
void FD_SET(int fd, fd_set *fdset);  // 增加fd
void FD_CLR(int fd, fd_set *fdset);  // 删除fd
int FD_ISSET(int fd, fd_set *fdset); // 判断fd是否在set里


int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
// Returns number of ready file descriptors, 0 on timeout, or –1 on error

发现OS X与Linux一个不同, timeout参数, struct timeval包含 long tv_sec & long tv_usec, 秒 & 毫秒

epoll

header

#include <sys/epoll.h>

函数

epoll_create

int epoll_create(int size);
  • description: 创建epoll instance
  • argumnet size: 要监视的文件描述符的数量
  • return: 一个文件描述符, 被创建的epoll instance通过这个文件描述符表示

epoll_ctl

int epoll_ctl(int epfd, int op, int fd, struct epoll_event *ev);
  • description: 修改epoll instance中的fd
  • argument epfd: 表示epoll instance
  • argument op: 要进行的操作, enum取值为
    • EPOLL_CTL_ADD 增加
      • 通过 ev 参数表示关心的事件
      • 若已经存在, 返回错误 EEXIST
    • EPOLL_CTL_MOD 修改
      • 通过 ev 参数表示关心的事件
      • 若不存在, 返回错误 ENOENT
    • EPOLL_CTL_DEL 移除
      • 若不存在, 返回错误 ENOENT
  • return
    • 0, success
    • -1, error

argument ev

struct epoll_event {
    uint32_t events; /* epoll events (bit mask) */ 
    epoll_data_t data; /* User data */
};

typedef union epoll_data {
    void        *ptr; /* Pointer to user-defined data */
    int          fd;  /* File descriptor */
    uint32_t     u32; /* 32-bit integer */
    uint64_t     u64; /* 64-bit integer */
} epoll_data_t;
  • ev.events 是 32-bit integer, 是使用 bitmask 来确定各个事件
  • ev.data 是 union 类型, 用于指定数据

epoll_wait

int epoll_wait(int epfd, struct epoll_event *evlist, int maxevents, int timeout);
  • description: 等待epoll instance中关心的事件发生
  • argument epfd: epoll instance
  • evlist: 是 epoll_event 类型的array, 每一个表示有监视的事情发生了. 这个array由调用者初始化, 即是先初始化好, 再传给 epoll_wait, 后面的int参数, 表示length
  • argument timeout:
    • -1, 阻塞住, 直到有感兴趣的事件发生 或者 收到了singal
    • 0, 进行一次非阻塞的检查操作, 查看哪些 fd 是 available的
    • >0, 即是 指定timeout

events bit mask

struct epoll_event ev;
ev.events; // <- this

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant