Nginx + fastcgi multithreading Nginx + fastcgi multithreading nginx nginx

Nginx + fastcgi multithreading


The problem is solved. Use FCGX_OpenSocket and umask(0) in order to let socket accept. This will give you real multithreaded application.

#include <pthread.h> #include <sys/types.h> #include <stdio.h> #include "fcgi_config.h" #include "fcgiapp.h" #define THREAD_COUNT 8 #define SOCKET_PATH "/var/run/myfcgiserver.sock" // your unix socket filestatic int socketId; static void *doit(void *a) {     int rc, i;     FCGX_Request request;     char *server_name;     if(FCGX_InitRequest(&request, socketId, 0) != 0)     {         printf("Can not init request\n");         return NULL;     }     printf("Request is inited\n");     for(;;)     {         static pthread_mutex_t accept_mutex = PTHREAD_MUTEX_INITIALIZER;         printf("Try to accept new request\n");         pthread_mutex_lock(&accept_mutex);         rc = FCGX_Accept_r(&request);         pthread_mutex_unlock(&accept_mutex);         if(rc < 0)         {             printf("Can not accept new request\n");             break;         }         printf("request is accepted\n");         server_name = FCGX_GetParam("SERVER_NAME", request.envp);         FCGX_PutS("Content-type: text/html\r\n", request.out);         FCGX_PutS("\r\n", request.out);         FCGX_PutS("<html>\r\n", request.out);         FCGX_PutS("<head>\r\n", request.out);         FCGX_PutS("<title>FastCGI Hello! (multi-threaded C, fcgiapp library)</title>\r\n", request.out);         FCGX_PutS("</head>\r\n", request.out);         FCGX_PutS("<body>\r\n", request.out);         FCGX_PutS("<h1>FastCGI Hello! (multi-threaded C, fcgiapp library)</h1>\r\n", request.out);         FCGX_PutS("<p>Request accepted from host <i>", request.out);         FCGX_PutS(server_name ? server_name : "?", request.out);         FCGX_PutS("</i></p>\r\n", request.out);         FCGX_PutS("</body>\r\n", request.out);         FCGX_PutS("</html>\r\n", request.out);         FCGX_Finish_r(&request);     }     return NULL; } int main(void) {     int i;     pthread_t id[THREAD_COUNT];     FCGX_Init();     printf("Lib is inited\n");     umask(0);       socketId = FCGX_OpenSocket(SOCKET_PATH, 2000);     if(socketId < 0)     {            return 1;     }     printf("Socket is opened\n");     for(i = 0; i < THREAD_COUNT; i++)     {         pthread_create(&id[i], NULL, doit, NULL);     }     for(i = 0; i < THREAD_COUNT; i++)     {         pthread_join(id[i], NULL);     }     return 0; }