Linux 相关笔记记录1

来源:互联网 发布:北京国税开票软件下载 编辑:程序博客网 时间:2024/06/05 15:01

C++11/C++14 7. Threads with Shared Memory and Mutex - 2017

[IPC:Shared Memory]

int shmget(key_t key, size_t size, int shmflg);
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
void *shmat(int shmid, const void *shmaddr, int shmflg);int shmdt(const void *shmaddr);

getsockopt, setsockopt

int getsockopt(int sockfd, int level, int optname,                      void *optval, socklen_t *optlen);int setsockopt(int sockfd, int level, int optname,                      const void *optval, socklen_t optlen);

TCP option SO_LINGER (zero) - when it’s required

The typical reason to set a SO_LINGER timeout of zero is to avoid large numbers of connections sitting in the TIME_WAIT state, tying up all the available resources on a server.

2、若设置了SO_LINGER并确定了非零的超时间隔,则closesocket()调用阻塞进程,直到所剩数据发送完毕或超时。这种关闭称为“优雅”或“从容”关闭。请注意如果套接口置为非阻塞且SO_LINGER设为非零超时,则closesocket()调用将以WSAEWOULDBLOCK错误返回。

epoll_wait()

epoll_wait,  epoll_pwait  -  wait  for  an I/O event on an epoll file descriptor.#include <sys/epoll.h>int epoll_wait(int epfd, struct epoll_event *events,                      int maxevents, int timeout);int epoll_pwait(int epfd, struct epoll_event *events,                      int maxevents, int timeout,                      const sigset_t *sigmask);

epoll provides both edge-triggered and level-triggered modes. In edge-triggered mode, a call to epoll_wait will return only when a new event is enqueued with the epoll object, while in level-triggered mode, epoll_wait will return as long as the condition holds.

For instance, if a pipe registered with epoll has received data, a call to epoll_wait will return, signaling the presence of data to be read. Suppose the reader only consumed part of data from the buffer. In level-triggered mode, further calls to epoll_wait will return immediately, as long as the pipe’s buffer contains data to be read. In edge-triggered mode, however, epoll_wait will return only once new data is written to the pipe.

epoll_ctl

epoll_ctl - control interface for an epoll file descriptor#include <sys/epoll.h>int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);