First, let me say that if the goal is a lightweight HTTP server serving PHP pages, this is already done. Take a look at nginx .
Regarding the learning experience, you have chosen something that is actually quite difficult.
Multithreading is difficult in the best of times. In C / C ++ (something with manual memory allocation really) this is an order of magnitude heavier.
Added to this is network connectivity. There are quirks to deal with, different versions of HTTP (mostly no problem), all kinds of HTTP headers to work with, etc.
The most intuitive solution to this problem is the process that listens on the port. When it receives a request, it starts a process that can be executed when executing a PHP process, if necessary.
This, however, does not scale. The first (obvious) optimization is to use threads instead of processes and some form of interthread communication. Although this helps, it will only scale so far.
Go beyond that and you are looking at handling an asynchronous socket, which is a pretty low level.
All these, however, are quite large projects.
Is there any specific reason you do this in C / C ++? Or any special reason why you are learning one or both of these languages? These languages โโcertainly have their place, but they are increasingly becoming niche languages. Collected (garbage collector) languages โโ/ platforms are almost completely captured. Joel claims that garbage collection is the only huge increase in programming productivity in the last 20 years, and I tend to agree.
cletus
source share