epoll

nginx安装部署和配置管理

自闭症网瘾萝莉.ら 提交于 2019-11-28 19:53:24
nginx 介绍 Nginx (engine x) 是一个高性能的 HTTP 和 反向代理 服务,也是一个IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。 Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。 在高连接并发的情况下,Nginx是Apache服务器不错的替代品。 创始人伊戈尔·赛索耶夫 为什么选择 nginx Nginx 是一个高性能的 Web 和反向代理服务器, 它具有有很多非常优越的特性: 作为 Web 服务器: 相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这点使Nginx尤其受到虚拟主机提供商的欢迎。能够支持高达 50,000 个并发连接数的响应,感谢 Nginx 为我们选择了epoll

IO模型

梦想的初衷 提交于 2019-11-28 19:23:22
BIO/NIO/AIO区别 首先需要区分几个概念 IO分为内存IO/网络IO/磁盘IO,磁盘IO都是阻塞的 阻塞与非阻塞是通过代码来实现的,区别在于是在于发过来操作请求,数据准备好才返回(阻塞)还是直接返回(非阻塞) IO读取顺序:磁盘(磁盘IO)/网卡(网络IO)—> 内核缓冲区 —> 用户内存,重点在于后面的过程是否是需要进程阻塞等待的 IO模型 同步阻塞(blocking IO) A去钓鱼了,你一直在那等,鱼上钩了,然后把鱼钓上来(全程阻塞) 普遍使用的IO模型,linux默认的IO模型。 进程调用recvfrom一直到recvfrom返回 同步非阻塞(noblocking IO) B去钓鱼了,放好钩后,然后开始看书,刷抖音去了,过一会看看是不是鱼上钩了。然后等鱼上钩了,把鱼钓上来 这个询问其实询问的是操作系统内核,即文件描述缓冲区是否就绪,准备好了,就进行拷贝数据包的操作。没有数据报准备好时,也不阻塞程序,内核直接返回为准备就绪的信号。 但是这个轮询其实是对CPU来说是一个比较大的消耗 很少使用,因为他浪费了大量的CPU资源 进程recvfrom,如果没有准备就绪的话,直接返回EWOULDBLOCK,过段时间再次调用recvfrom,直到正常返回。这个操作其实就是epolling(轮询),epolling内核是一个很占用CPU资源的操作 但是对管道的操作

Linux简单高并发模型——Epoll + 线程池

主宰稳场 提交于 2019-11-28 18:54:56
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_25425023/article/details/70199133 首先是一个locker.h的文件,封装了信号量、互斥量、条件变量。 在线程池中的任务队列需要互斥量的保护,当任务队列中有任务到达时,需要唤醒一个等待pthread_cond_wait()的线程,线程池停止时,需要唤醒所以的线程,调用的是pthread_cond_broadcast()。 locker.h文件: #ifndef _LOCKER_H_ #define _LOCKER_H_ #include <pthread.h> #include <stdio.h> #include <semaphore.h> /*信号量的类*/ class sem_locker { private: sem_t m_sem; public: //初始化信号量 sem_locker() { if(sem_init(&m_sem, 0, 0) != 0) printf("sem init error\n"); } //销毁信号量 ~sem_locker() { sem_destroy(&m_sem); } //等待信号量 bool wait() { return sem

What is an anonymous inode in Linux?

六眼飞鱼酱① 提交于 2019-11-28 18:47:29
I made a google search about "anonymous inode" and it seems it's related to epoll ... but what actually is it? At least in some contexts, an anonymous inode is an inode without an attached directory entry. The easiest way to create such an inode is as such: int fd = open( "/tmp/file", O_CREAT | O_RDWR, 0666 ); unlink( "/tmp/file" ); // Note that the descriptor fd now points to an inode that has no filesystem entry; you // can still write to it, fstat() it, etc. but you can't find it in the filesystem. open with O_TMPFILE This would be a good definition of anonymous inode: it creates an inode

What's the difference between event-driven and asynchronous? Between epoll and AIO?

拥有回忆 提交于 2019-11-28 15:47:27
问题 Event-driven and asynchronous are often used as synonyms. Are there any differences between the two? Also, what is the difference between epoll and aio ? How do they fit together? Lastly, I've read many times that AIO in Linux is horribly broken. How exactly is it broken? Thanks. 回答1: Events is one of the paradigms to achieve asynchronous execution. But not all asynchronous systems use events. That is about semantic meaning of these two - one is super-entity of another. epoll and aio use

What is the purpose of epoll's edge triggered option?

我们两清 提交于 2019-11-28 15:05:10
From epoll's man page: epoll is a variant of poll(2) that can be used either as an edge-triggered or a level-triggered interface When would one use the edge triggered option? The man page gives an example that uses it, but I don't see why it is necessary in the example. James McLaughlin When an FD becomes read or write ready, you might not necessarily want to read (or write) all the data immediately. Level-triggered epoll will keep nagging you as long as the FD remains ready, whereas edge-triggered won't bother you again until the next time you get an EAGAIN (so it's more complicated to code

Nginx面试题

Deadly 提交于 2019-11-28 10:34:36
1、请解释一下什么是Nginx? 答:Nginx是一个web服务器和反向代理服务器,用于HTTP、HTTPS、SMTP、POP3和IMAP协议。 2、请列举Nginx的一些特性? 答:Nginx服务器的特性包括: 1)反向代理/L7负载均衡器 2)嵌入式Perl解释器 3)动态二进制升级 4)可用于重新编写URL,具有非常好的PCRE支持 3、nginx和apache的区别? 1)轻量级,同样起web 服务,比apache 占用更少的内存及资源 2)抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下nginx 能保持低资源低消耗高性能 3)高度模块化的设计,编写模块相对简单 4)最核心的区别在于apache是同步多进程模型,一个连接对应一个进程;nginx是异步的,多个连接(万级别)可以对应一个进程 4.nginx是如何实现高并发的 一个主进程,多个工作进程,每个工作进程可以处理多个请求,每进来一个request,会有一个worker进程去处理。但不是全程的处理,处理到可能发生阻塞的地方,比如向上游(后端)服务器转发request,并等待请求返回。那么,这个处理的worker继续处理其他请求,而一旦上游服务器返回了,就会触发这个事件,worker才会来接手,这个request才会接着往下走。由于web

epoll使用

主宰稳场 提交于 2019-11-28 09:02:36
epoll通过下面3个epoll系统调用为用户提供服务。 (1)epoll_create系统调用 epoll_create在C库中的原型如下: int epoll_create(int size); epoll_create返回一个句柄,之后epoll的使用都将依靠这个句柄来标识。参数size是告诉epoll所要处理的大致事件数目。不再使用epoll时,必须调用close关闭句柄。 注:size参数只是告诉内核这个epoll对象会处理的事件大致数目,而不是能够处理的事件的最大个数。在Linux最新的一些内核版本的实现中,这个size参数没有任何意义。 (2)epoll_ctl系统调用 epoll_ctl在C库中的原型如下: int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event); epoll_ctl向epoll对象中添加、修改或者删除感兴趣的事件,返回0表示成功,否则返回-1,此时需要根据errno错误码判断错误类型。epoll_wait方法返回的事件必然是通过epoll_ctl添加到epoll中的。 参数epfd是epoll_create返回的句柄,而op参数的意义见表: op的取值 意义 EPOLL_CTL_ADD 添加新的事件到epoll中 EPOLL_CTL_MOD 修改epoll中的事件

epoll使用总结

非 Y 不嫁゛ 提交于 2019-11-28 09:02:22
epoll的使用总结 使用epoll来实现一个tcp server,中间碰到了不少使用细节上的问题,总结一下。 man epoll里推荐的使用方法 epoll使用代码 #define MAX_EVENTS 10 struct epoll_event ev, events[MAX_EVENTS]; int listen_sock, conn_sock, nfds, epollfd; /* Set up listening socket, 'listen_sock' (socket(), bind(), listen()) */ epollfd = epoll_create(10); if (epollfd == -1) { perror("epoll_create"); exit(EXIT_FAILURE); } ev.events = EPOLLIN; // 不要写成ev.events = EPOLLIN | EPOLLET; ev.data.fd = listen_sock; if (epoll_ctl(epollfd, EPOLL_CTL_ADD, listen_sock, &ev) == -1) { perror("epoll_ctl: listen_sock"); exit(EXIT_FAILURE); } for (;;) { nfds = epoll_wait

epoll使用详解(精髓)

社会主义新天地 提交于 2019-11-28 09:02:13
epoll - I/O event notification facility 在linux的网络编程中,很长的时间都在使用select来做事件触发。在linux新的内核中,有了一种替换它的机制,就是epoll。 相比于select,epoll最大的好处在于它不会随着监听fd数目的增长而降低效率。因为在内核中的select实现中,它是采用轮询来处理的,轮询的fd数目越多,自然耗时越多。并且,在linux/posix_types.h头文件有这样的声明: #define __FD_SETSIZE 1024 表示select最多同时监听1024个fd,当然,可以通过修改头文件再重编译内核来扩大这个数目,但这似乎并不治本。 epoll的接口非常简单,一共就三个函数: 1. int epoll_create(int size); 创建一个epoll的句柄,size用来告诉内核这个监听的数目一共有多大。这个参数不同于select()中的第一个参数,给出最大监听的fd+1的值。需要注意的是,当创建好epoll句柄后,它就是会占用一个fd值,在linux下如果查看/proc/进程id/fd/,是能够看到这个fd的,所以在使用完epoll后,必须调用close()关闭,否则可能导致fd被耗尽。 2. int epoll_ctl(int epfd, int op, int fd, struct