: This article mainly introduces [reading notes] 1 _ exploring nginx architecture _ 3 _ processing signals and timers. if you are interested in PHP tutorials, refer to it. Original article: http://tengine.taobao.org/book/chapter_02.html
For a basic web server, there are usually three types of events: Network events, signals, and timers. In previous articles, network events can be well solved through asynchronous non-blocking. And how to process the signal and timer?
First, the signal processing. For nginx, some specific signals represent specific meanings. The signal will interrupt the current operation of the program, and continue to execute after the status changes. If it is a system call, the system call may fail and need to be re-imported. For nginx, if nginx is waiting for the event (epoll_wait), if the program receives a signal, after the signal processing function is completed, epoll_wait will return an error, and then the program can access epoll_wait again.
In addition, let's take a look at the timer. Since epoll_wait and other functions can set a timeout time when calling, nginx uses this timeout time to implement the timer. The timer events in nginx are placed in a red/black tree that maintains the timer. each time before entering epoll_wait, the minimum time of all timer events is obtained from the red/black tree, enter epoll_wait after calculating the epoll_wait timeout. Therefore, epoll_wait times out when no event is generated or the signal is interrupted. that is to say, the timer event arrives. In this case, nginx checks all timeout events, sets their status to timeout, and then processes network events. From this we can see that when we write nginx code, when processing the network event callback function, the first thing we usually do is to judge the timeout and then process the network event.
We can use a piece of pseudo code to summarize the nginx event processing model:
while (true) { for t in run_tasks: t.handler(); update_time(&now); timeout = ETERNITY; for t in wait_tasks: /* sorted already */ if (t.time <= now) { t.timeout_handler(); } else { timeout = t.time - now; break; } nevents = poll_function(events, timeout); for i in nevents: task t; if (events[i].type == READ) { t.handler = read_handler; } else { /* events[i].type == WRITE */ t.handler = write_handler; } run_tasks_add(t);}
'). AddClass ('pre-numbering '). hide (); $ (this ). addClass ('Has-numbering '). parent (). append ($ numbering); for (I = 1; I <= lines; I ++) {$ numbering. append ($ ('
'). Text (I) ;}; $ numbering. fadeIn (1700) ;}) ;}; script
The above introduces [reading notes] 1 _ exploring nginx architecture _ 3 _ processing signals and timers, including some content, and hope to help those who are interested in PHP tutorials.