web服务器处理请求_Web服务器如何侦听新请求?

web服务器处理请求

web服务器处理请求

how-do-web-servers-listen-for-new-requests-00

When learning about web servers and how they work, you may be curious if they are constantly listening for requests or if they wait until they receive a request to go into action. With that in mind, today’s SuperUser Q&A post has the answers to satisfy a reader’s curiosity.

当了解Web服务器及其工作方式时,您可能会好奇它们是否一直在侦听请求,或者是否等待直到收到执行请求。 考虑到这一点,今天的SuperUser Q&A帖子提供了满足读者好奇心的答案。

Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.

今天的“问答”环节由SuperUser提供,它是Stack Exchange的一个分支,该社区是由社区驱动的Q&A网站分组。

Screenshot courtesy of xmodulo/Linux Screenshots (Flickr).

屏幕截图由xmodulo / Linux屏幕截图(Flickr)提供

问题 (The Question)

SuperUser reader user2202911 wants to know how web servers listen for new requests:

超级用户阅读器user2202911想知道Web服务器如何侦听新请求:

I am trying to understand the ‘deeper level’ details of how web servers work. I want to know if a server, say Apache, for instance, is continuously polling for new requests or if it works by some sort of interrupt system. If it is an interrupt, what is sparking the interrupt? Is it the network card driver?

我正在尝试了解Web服务器如何工作的“更深层次”的细节。 我想知道某个服务器(例如说Apache)是否在不断轮询新请求,或者它是否可以通过某种中断系统工作。 如果是中断,是什么引起了中断? 它是网卡驱动程序吗?

How does a web server listen for new requests?

Web服务器如何侦听新请求?

答案 (The Answer)

SuperUser contributor Greg Bowser has the answer for us:

超级用户贡献者Greg Bowser为我们提供了答案:

The short answer is some sort of interrupt system. Essentially, they use blocking I/O, meaning they sleep (block) while waiting for new data.

简短的答案是某种中断系统。 本质上,他们使用阻塞I / O ,这意味着他们在等待新数据时会Hibernate(阻塞)。

  1. The server creates a listening socket and then blocks while waiting for new connections. During this time, the kernel puts the process into an interruptible sleep state and runs other processes. This is an important point; having the process poll continuously would waste CPU resources. The kernel is able to use the system resources more efficiently by blocking the process until there is work for it to do.

    服务器创建一个监听套接字,然后在等待新连接时阻塞。 在这段时间内,内核将进程置于可中断的睡眠状态并运行其他进程。 这是重要的一点; 连续进行进程轮询将浪费CPU资源。 内核可以通过阻塞进程直到有工作要做来更有效地使用系统资源。

  2. When new data arrives on the network, the network card issues an interrupt.

    当新数据到达网络时,网卡将发出一个中断。
  3. Seeing that there is an interrupt from the network card, the kernel, via the network card driver, reads the new data from the network card and stores it in memory. (This must be done quickly and is generally handled inside the interrupt handler.)

    看到网卡有中断,内核通过网卡驱动程序从网卡读取新数据并将其存储在内存中。 (这必须快速完成,并且通常在中断处理程序中进行处理。)
  4. The kernel processes the newly arrived data and associates it with a socket. A process that is blocking on that socket will be marked runnable, meaning that it is now eligible to run. It does not necessarily run immediately (the kernel may decide to run other processes still).

    内核处理新到达的数据并将其与套接字关联。 在该套接字上阻塞的进程将被标记为可运行,这意味着它现在可以运行了。 它不一定立即运行(内核可能决定仍然运行其他进程)。
  5. At its leisure, the kernel will wake up the blocked web server process. (Since it is now runnable.)

    内核会在闲时唤醒被阻止的Web服务器进程。 (因为它现在可以运行了。)
  6. The web server process continues executing as if no time has passed. Its blocking system call returns and it processes any new data. Then go to step 1.

    Web服务器进程将继续执行,就像没有时间过去一样。 它的阻塞系统调用返回并处理任何新数据。 然后转到步骤1。


Have something to add to the explanation? Sound off in the comments. Want to read more answers from other tech-savvy Stack Exchange users? Check out the full discussion thread here.

有什么补充说明吗? 在评论中听起来不错。 是否想从其他精通Stack Exchange的用户那里获得更多答案? 在此处查看完整的讨论线程

翻译自: https://www.howtogeek.com/201958/how-do-web-servers-listen-for-new-requests/

web服务器处理请求

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值