redis启动时的epoll事件

initServer

1、epoll_create,创建epoll队列

aeCreateEventLoop==>aeApiCreate

    state->epfd = epoll_create(1024); /* 1024 is just a hint for the kernel */

2、epoll_ctl,添加epoll事件

2.1、listenToPort

    /* Open the TCP listening socket for the user commands. */
    if (server.port != 0 &&
        listenToPort(server.port,server.ipfd,&server.ipfd_count) == C_ERR)
        exit(1);

绑定redis服务端口,并用服务端的sockfd,作为

fds[*count] 的第0个成员。count默认=0

int listenToPort(int port, int *fds, int *count) {
###
            /* Bind IPv4 address. */
            fds[*count] = anetTcpServer(server.neterr,port,server.bindaddr[j],
                server.tcp_backlog);
}

此时,server.ipfd[0]=server_sock

2.2、epoll_ctl   添加服务端sock事件

/* Create an event handler for accepting new connections in TCP and Unix
     * domain sockets. */
    for (j = 0; j < server.ipfd_count; j++) {
        if (aeCreateFileEvent(server.el, server.ipfd[j], AE_READABLE,
            acceptTcpHandler,NULL) == AE_ERR)
            {
                serverPanic(
                    "Unrecoverable error creating server.ipfd file event.");
            }
    }

 if (epoll_ctl(state->epfd,op,fd,&ee) == -1) return -1;

这里的state->epfd,来自于epoll_create,fd来自server_sock

3、epoll_wait

    aeMain(server.el);

    retval = epoll_wait(state->epfd,state->events,eventLoop->setsize,
            tvp ? (tvp->tv_sec*1000 + tvp->tv_usec/1000) : -1);

 

 

你可能感兴趣的:(redis启动时的epoll事件)