fastcgi 多线程编程

源代码

/*
 * threaded.c -- A simple multi-threaded FastCGI application.
 */

#ifndef lint
static const char rcsid[] = "$Id: threaded.c,v 1.9 2001/11/20 03:23:21 robs Exp $";
#endif /* not lint */

#include "fcgi_config.h"

#include <pthread.h>
#include <sys/types.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "fcgiapp.h"
#include <string.h>
#include  <stdio.h>


#define THREAD_COUNT 20

static int counts[THREAD_COUNT];
static int counter = 0;
static void *thread_name1_1(void *arg)
{
    char tmp_buf[1024]; 
    char *server_name=NULL;
       char *content_length=NULL;
       char *query_string=NULL;
    char *http_useragent=NULL;
    char *request_method=NULL;
    char *content_type=NULL;
    char *path_info=NULL;
    char *script_name=NULL;
    pid_t pid = getpid();
    unsigned int pthread_id = (unsigned int)pthread_self();
    FCGX_Request *request=NULL;
    request = (FCGX_Request *)arg;
    //memset(&request,0,sizeof(FCGX_Request));
    //memcpy(&request,(FCGX_Request *)arg,sizeof(FCGX_Request));
    
    request_method = FCGX_GetParam("REQUEST_METHOD", request->envp);
    server_name = FCGX_GetParam("SERVER_NAME", request->envp);
    content_length = FCGX_GetParam("CONTENT_LENGTH", request->envp);
    query_string = FCGX_GetParam("QUERY_STRING", request->envp);
    http_useragent = FCGX_GetParam("HTTP_USER_AGENT", request->envp);
    content_type = FCGX_GetParam("CONTENT_TYPE", request->envp);
    content_type = FCGX_GetParam("PATH_INFO", request->envp);
    script_name = FCGX_GetParam("SCRIPT_NAME", request->envp);
    
    memset(tmp_buf,0,sizeof(tmp_buf));
    sleep(5);
    if(0 == strcmp(query_string,"name1=1"))
    {
        snprintf(tmp_buf,sizeof(tmp_buf),\
                    "Content-type: text/html\r\n\r\n" 
                    "<title>FastCGI Hello!</title>"     
                    "<h1>caoft1</h1>"             
                    "<h1>request_method:%s</h1>" 
                    "<h1>server_name:%s</h1>" 
                    "<h1>content_length:%s</h1>" 
                    "<h1>query_string:%s</h1>" 
                    "<h1>http_useragent:%s</h1>" 
                    "<h1>content_type:%s</h1>" 
                    "<h1>pid:%d</h1>" 
                    "<h1>thread_id:%d</h1>" 
                    "<h1>counter:%d</h1>"
                    "<h1>path_info:%s</h1>"
                    "<h1>script_name:%s</h1>"
                ,request_method,\
                server_name,\
                content_length,\
                query_string,\
                http_useragent,\
                content_type,\
                pid,pthread_id,counter,\
                path_info,script_name);
    }
    else if(0 == strcmp(query_string,"name1=2"))
    {
        snprintf(tmp_buf,sizeof(tmp_buf),\
                    "Content-type: text/html\r\n\r\n" 
                    "<title>FastCGI Hello!</title>"     
                    "<h1>caoft2</h1>"             
                    "<h1>request_method:%s</h1>" 
                    "<h1>server_name:%s</h1>" 
                    "<h1>content_length:%s</h1>" 
                    "<h1>query_string:%s</h1>" 
                    "<h1>http_useragent:%s</h1>" 
                    "<h1>content_type:%s</h1>" 
                    "<h1>pid:%d</h1>" 
                    "<h1>thread_id:%d</h1>" 
                    "<h1>counter:%d</h1>"
                    "<h1>path_info:%s</h1>"
                    "<h1>script_name:%s</h1>"
                ,request_method,\
                server_name,\
                content_length,\
                query_string,\
                http_useragent,\
                content_type,\
                pid,pthread_id,counter,\
                path_info,script_name);
    }
    else
    {
        snprintf(tmp_buf,sizeof(tmp_buf),\
                    "Content-type: text/html\r\n\r\n" 
                    "<title>FastCGI Hello!</title>"     
                    "<h1>caoft0</h1>"             
                    "<h1>request_method:%s</h1>" 
                    "<h1>server_name:%s</h1>" 
                    "<h1>content_length:%s</h1>" 
                    "<h1>query_string:%s</h1>" 
                    "<h1>http_useragent:%s</h1>" 
                    "<h1>content_type:%s</h1>" 
                    "<h1>pid:%d</h1>" 
                    "<h1>thread_id:%d</h1>" 
                    "<h1>counter:%d</h1>"
                    "<h1>path_info:%s</h1>"
                    "<h1>script_name:%s</h1>"
                ,request_method,\
                server_name,\
                content_length,\
                query_string,\
                http_useragent,\
                content_type,\
                pid,pthread_id,counter,\
                path_info,script_name);
    }
    FCGX_FPrintF(request->out,tmp_buf);
    FCGX_Finish_r(request);  
    //sleep(5);
    pthread_exit(NULL);
}

static int doit(void )
{
    FCGX_Request request[THREAD_COUNT];
    pthread_t tid[THREAD_COUNT];
    char *query_string=NULL;
    int rc = 0;
    
    for (;;)
    {

        FCGX_InitRequest(&request[counter], 0, 0);    
        rc = FCGX_Accept_r(&request[counter]);

        if (rc < 0)
        {
            return -1;
        }

        rc = pthread_create(&tid[counter], NULL, thread_name1_1, (void*)&request[counter]);
        if(rc != 0)
        {
            return -1;
        }
        counter ++;
        if(counter >= THREAD_COUNT)
        {
            counter = 0;
        }
    }

    return 0;
}

int main(void)
{
    FCGX_Init();
    doit();
    return 0;
}

编译方式

gcc -o caoft.fcgi threaded.c -lpthread -lfcgi

验证方式

http://自己的ip/cgi-bin/caoft.fcgi?name1=1

http://自己的ip/cgi-bin/caoft.fcgi?name1=2

http://自己的ip/cgi-bin/caoft.fcgi

验证方式

https://download.csdn.net/download/caofengtao1314/10560484

请用次blog代码替换掉下载代码中cgi-bin目录下的代码

转载请注明博主

https://mp.csdn.net/postedit/81286960

猜你喜欢

转载自blog.csdn.net/caofengtao1314/article/details/81286960