发布时间:2025-12-09 11:54:50 浏览次数:2
由于CGI解释器的反复加载会使CGI性能低下,FastCGI可以将CGI解释器保持在内存中, 提高性能
相关地址:https://fastcgi-archives.github.io
下载地址:http://redmine.lighttpd.net/projects/spawn-fcgi/wiki
-f 指定调用 FastCGI的进程的执行程序位置
-a 绑定到 地址addr。
-p 绑定到 端口 port。
-s 绑定到 unix domain socket
-C 指定产 生的FastCGI的进程数, 默认为 5。 ( 仅用 于PHP)
-P 指定产 生的进程的PID文件路径。
-F 指定产 生的FastCGI的进程数( C的CGI用 这个)
例子:./spawn-fcgi -a 127.0.0.1 -p 8088 -F 500 -f cgi(启动500个cgi程序,监听的端口为8088,绑定地址为127.0.0.1)
#include "fcgi_stdio.h"#include <stdlib.h>void main(void){ int count = 0; while(FCGI_Accept() >= 0) printf("Content-type: text/html\r\n" "\r\n" "<title>FastCGI Hello!</title>" "<h1>FastCGI Hello!</h1>" "Request number %d running on host <i>%s</i>\n", ++count, getenv("SERVER_NAME"));}非阻塞的实现,将会话状态与requests进行binding即可
ZEN_Message_Queue_Deque<ZEN_MT_SYNCH, uint32_t> t_que(100000);// 这步非常关键,因为FCGX_Accept_r中的数据流绑定是直接和request的ptr进行绑定的// 所以在未处理完数据的前提下,requests的生命周期要和会话的生命周期一致FCGX_Request requests[1000];uint32_t use_idx[1000];void *do_session(void *arg){ int ret = 0; while (1) { uint32_t idx = 0; ret = t_que.try_dequeue(idx); if (ret != 0) { // no data sleep 1ms usleep(1000); continue; } FCGX_Request &request = requests[idx]; std::string out = "Content-type:application/json\r\n\r\n"; Json::Value root; root["ret"] = 1000; root["t_id"] = (int)gettid(); out.append(root.toStyledString()); FCGX_FPrintF(request.out, out.c_str()); FCGX_Finish_r(&request); use_idx[idx] = 0; } return NULL;}int get_free(){ static uint32_t idx = 0; for (; idx < 1000; idx ++) { if (use_idx[idx] == 0) { use_idx[idx] = 1; return idx; } } return -1;}int main(int argc, char **argv){ pthread_t pthread_id; int iThreadNum = 10; for (int index = 0; index != iThreadNum; ++ index) { pthread_create(&pthread_id, NULL, do_session, NULL); } memset(use_idx, 0, sizeof(use_idx)); int ret = FCGX_Init(); if (ret != 0) { printf("init fail\n"); return -1; } while (1) { int idx = get_free(); if (idx < 0) { continue; } FCGX_Request &request = requests[idx]; ret = FCGX_InitRequest(&request, 0, 0); if (ret != 0) { printf("init fail\n"); return -1; } ret = FCGX_Accept_r(&request); if (ret < 0) { continue; } t_que.enqueue(idx); } return 0;}