r/C_Programming • u/runningOverA • Jun 22 '26
Lessons learned creating a fcgi webapp in C
Basically wanted to convert my website written in PHP to C.
Previously tried plain old cgi mode, but that had a low req / second rate and server collapsed under spike.
Solution was fcgi. Process doesn't terminate after each request, rather repeatedly gets and sends request / response over unix socket to the web server which runs on the front.
But I don't want to reload the fcgi server every time I change a page's content.
Solution was to compile pages as shared .so library. fcgi will load the so at every call with dlopen(). Render the page and then unload it with dlclose(). So any time I recompile a page, the changes are reflected instantly.
Questions I had been wondering before building it :
- Will it work?
= It does. In spite of the fact that most online opinion was that it won't and I have to uniquely rename the so file with version number for it to work. I still don't know why it works though, but it does. Only fix in code I needed was to clean up the global memories after each cycle. Those aren't automatically reset like when starting from shell.
- Isn't it slower than loading dynamic libraries persistently?
= Not by much. My benchmark shows the same numbers regardless of I make dlopen with RTLD_NODELETE or not.
- What about memory leaks?
= I am using a fixed 128 mb buddy memory manager to allocate memory for each call and destroying it when finished. Unless I zero fill the whole area before each request, there's no increase in memory consumption seen from OS. If a leak appears, memory consumption will increase with each call. I have set worker processes to re spawn after each 8000 call as a precaution anyway. It takes about 2 hours.
- How about crashes?
= Turned on coredump. And if a core dump happens, I can copy the coredump file and view call stack to figure out exactly where the fault happened and why using gbd. Fix it and it's gone. coredump will terminate the worker process and root process will spawn a new one. But the problem : the server's coredump directory gets filled up quickly if a faulty code is loaded. In a crash the http server render one of those 50x internal error.
- How about performance?
= Faster and less memory hungry than PHP, even though PHP recently has gotten a JIT compiler.
•
u/AutoModerator Jun 22 '26
Looks like you're asking about learning C.
Our wiki includes several useful resources, including a page of curated learning resources. Why not try some of those?
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
3
u/komata_kya Jun 22 '26
Why unload it after each call? You could have it always loaded and unload when you change it. Also, instead of fcgi you could use a http library like libevhtp and use your C program as a backend behind your reverse proxy. Maybe that would be even faster.
2
u/dvhh Jun 23 '26 edited Jun 23 '26
Was looking at the approach to reverse proxy a http server listening on unix domain socket, but fcgi is also a correct approach with a more specialized protocol.
I think the main pitfall is to properly recycle all the request handling structures for the next request
3
u/stiggg Jun 22 '26
Interesting, after I started writing C again this year I recently thought about doing something similar for a personal project. I did webdev in the early 2000es but already started with PHP. I always wanted to find out how these very early cgi setups really work in praxis. Do you use apache?
1
u/[deleted] Jun 24 '26
[removed] — view removed comment