Linux高性能服务器编程 笔记

1. logger

SYSLOG(3)                          Linux Programmer's Manual                         SYSLOG(3)

NAME
       closelog, openlog, syslog, vsyslog - send messages to the system logger

SYNOPSIS
       #include <syslog.h>

       void openlog(const char *ident, int option, int facility);
       void syslog(int priority, const char *format, ...);
       void closelog(void);

	   // example
	   openlog("mark", LOG_CONS | LOG_PID, 0);
       syslog(LOG_DEBUG, "hello logger \n");
       closelog();

Alt

2. epoll


EPOLL_CREATE(2)                    Linux Programmer's Manual                   EPOLL_CREATE(2)

NAME
       epoll_create, epoll_create1 - open an epoll file descriptor

SYNOPSIS
       #include <sys/epoll.h>

       int epoll_create(int size);
       int epoll_create1(int flags);

EPOLL_CTL(2)                       Linux Programmer's Manual                      EPOLL_CTL(2)

NAME
       epoll_ctl - control interface for an epoll descriptor

SYNOPSIS
       #include <sys/epoll.h>

       int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);

EPOLL_WAIT(2)                      Linux Programmer's Manual                     EPOLL_WAIT(2)

NAME
       epoll_wait, epoll_pwait - wait for an I/O event on an epoll file descriptor

SYNOPSIS
       #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);

模型 驱动方式 内部实现
select / poll 主动轮询
epoll 事件驱动(类似中断) mmap / epitem 红黑树 / rdllink 双链表

Alt

3. POSIX Asynchronous I/O

AIO(7)                             Linux Programmer's Manual                            AIO(7)

NAME
       aio - POSIX asynchronous I/O overview

DESCRIPTION
       The  POSIX asynchronous I/O (AIO) interface allows applications to initiate one or more
       I/O operations that are performed asynchronously (i.e., in the background).  The appli‐
       cation  can  elect  to  be  notified of completion of the I/O operation in a variety of
       ways: by delivery of a signal, by instantiation of a thread, or no notification at all.

       The POSIX AIO interface consists of the following functions:

       aio_read(3)     Enqueue a read request.  This is the asynchronous analog of read(2).

       aio_write(3)    Enqueue a write request.  This is the asynchronous analog of write(2).

       aio_fsync(3)    Enqueue a sync request for the I/O operations  on  a  file  descriptor.
                       This is the asynchronous analog of fsync(2) and fdatasync(2).

4. 高效事件驱动模式

类型 基于 特点
Reactor epoll 主线程只接收与分发通知
Proactor AIO IO 操作都交给主线程

Alt

Alt

5. 高效并发模式

高并发适用于 I/O 密集型 场景,不适用于 计算密集型 场景,并发并发编程主要有 多进程多线程 两种方式。

6. epoll 具体使用


EPOLL_CTL_ADD:注册新的fd到epfd中;
EPOLL_CTL_MOD:修改已经注册的fd的监听事件;
EPOLL_CTL_DEL:从epfd中删除一个fd;

typedef union epoll_data {
    void *ptr;
    int fd;
    __uint32_t u32;
    __uint64_t u64;
} epoll_data_t;

struct epoll_event {
    __uint32_t events; /* Epoll events */
    epoll_data_t data; /* User data variable */
};


events可以是以下几个宏的集合:

EPOLLIN :表示对应的文件描述符可以读(包括对端SOCKET正常关闭);
EPOLLOUT:表示对应的文件描述符可以写;
EPOLLPRI:表示对应的文件描述符有紧急的数据可读(这里应该表示有带外数据到来);
EPOLLERR:表示对应的文件描述符发生错误;
EPOLLHUP:表示对应的文件描述符被挂断;
EPOLLET: 将EPOLL设为边缘触发(Edge Triggered)模式,这是相对于水平触发(Level Triggered)来说的。
EPOLLONESHOT:只监听一次事件,当监听完这次事件之后,如果还需要继续监听这个socket的话,需要再次把这个socket加入到EPOLL队列里

	/* 如何索引 epoll 返回的就绪文件描述符 */
	int ret = epoll_wait(epollfd, events, MAX_EVENT_NUMBER, -1);
	/* 仅遍历就绪的 ret 个文件描述符 */
	for(int i = 0; i < ret; ++i)
	{
		int sockfd = events[i].data.fd;
		/* sockfd肯定就绪,直接处理 */
	}
模式 特点 后续
LT 应用程序可以不立即处理事件 再次向应用程序通告未得到处理的事件
ET 应用程序必须立即处理事件 EPOLLONESHOT 事件防止多线程同时处理一个 FD (需要释放)

7. libevent – an event notification library

  • 轻量级、专注于网络
  • 高性能
  • 跨平台(Linux Unix Windows)
  • 事件驱动、统一事件源(I/O事件、信号、定时器事件)
  • 基于 Reactor 模式(支持多种 I/O 多路复用技术)
  • 线程安全
  • 开源

未完待续 …

猜你喜欢

转载自blog.csdn.net/qq_38408378/article/details/85776333