• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
 
The MyServer threads model explained
Giuseppe Scrivanogscrivano@gnu.org
Abstract
In the era of Internet and Web 2.0, web servers have the impor-tant role to serve information as quickly as possible. In the Web 1.0era, web contents were mainly static and rarely modified; AJAX websites now introduce many additional requests for web contents to beupdated dynamically.In this scenario there is need for web servers able to serve manyclients at the same time. Threads are the ideal choice to handle manyrequests: they don’t need much resources and share the same memoryamong them.
1 Introduction
MyServer is a web server based on a multithreaded architecture; many con-nections can be served at the same time by the same process.Other common architectures are the single threaded server, where a pro-cess can handle only one connection at time and the multi-processes server,where a new process is created to handle each request. A process needsmany resources to be allocated. Within UNIX-like operating systems this isdone by duplicating the calling process, through a mechanism called “fork”.Some modern implementations allow the child process to share memory withthe parent process until one of them modifies something (copy-on-write).Although all these are improvements to the original mechanism, creating aprocess is still a slow operation.Aside from processes there are threads or lightweight processes, that doalmost the same things that processes do, with the advantage of being fasterto create than a process. Threads are not only faster to create, but they usefewer resources than processes; and every thread needs to save only its stackand CPU registers status information.Threads of the same process share the same memory and descriptors table,as well as access to any opened resource (files, sockets, etc...). The cost of 1
 
better overall performance is the introduction of many security problems.From this point of view, what was an advantage performance-wise turnsinto a disadvantage. Sharing the same memory needs some synchronizationmechanism between threads; two or more threads must not read or writedata at the same time and a single thread that crashes will cause the theentire server process to crash.Despite these difficulties, MyServer uses a single process-multiple threadsarchitecture that can serve multiple clients using different threads on a singleprocess.Synchronization is performed through different mechanisms: semaphores,mutexes, events. However, an excessive usage of these can transform a mul-tithreaded application’s performance into that of a single-process one, withonly one request served at time.In addition, MyServer has an internal scheduler to handle connections,and allows the possibility of defining a priority for each of them. Givinghigher priority to a connection will make it possible for requests from thisconnection to be processed faster than requests from a connection with alower priority.
2 Threads pool
An allocated threads-pool is supposed to serve any request to the server.Since it is the job of the scheduler to listen for new connections or newrequests,and it internally handles the order of requests to be served, thethreads need to contact the scheduler to be notified of a new request.When the threads contact the scheduler for a new request to handle,they are put in a “sleep” status and wake up when request is active (orwhen some other special case occurs). Synchronization here is done using asemaphore that is increased when a connection is moved to the ready queueand decreased when a thread acquires a connection to process.Figure 1 shows a situation with N allocated threads and a new requestis ready, a thread that previously was in a waiting status is woken up andthe ready connection is sent to it.
2.1 Number of threads
The number of threads is not fixed; more are allocated when there are moreconnections to be processed and removed when the server activity is low.This value depends on two values present in the main configuration file:
NTHREADS STATIC 
and
NTHREADS MAX 
, the first one specifies2
 
Figure 1: Threads poolthe number of threads always active: they will not be destroyed while theserver is alive. The second one specifies the maximum number of threads thatcan be created, considering both static threads and dynamically allocatedones. It is right to state that the number of threads will vary between thesetwo values and the exact value at any given time depends on the server load.Figure 2: New thread creationWhen a thread is not usedfor several seconds it is markedfor removal. It is easy to imag-ine that if there are no requestsfor some seconds, all the threadsare marked and removed untilthe number of threads is exactly
NTHREADS STATIC 
. To pre-vent this behaviour, a mechanismcalled “slow-stop” is used.The name comes from the TCPprotocol’s “slow-startmechanism.The “slow-start” aim is to preventnetwork congestion by increasingthe number of packets to be sentuntil a threshold value is reached.If a packet is not delivered correctlyin this phase, the number of packetsto be sent is reduced to avoid additional congestion.3
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...