FCGI single-threaded environment and multithreaded environment examples

Source: Internet
Author: User
0. Background

The content that waits for communication is added to the project, so a single request processing time has increased. Single-threaded fcgi waste CPU and user time, so multithreading is required to reduce user queuing time.

Changing the processing of user requests from single-threaded to multithreaded requires a general understanding of whether the changes will affect performance.

The

concludes that multithreading and single-threaded processes are almost the same as those used in a single thread, so multithreading does not add up to additional burdens. 1. single-threaded Processing Steps 1.1 A simple single-threaded fcgi request C code    #include  <fcgi_stdio.h>      Void main (void)    {       int count = 0;       while (Fcgi_accept ()  >= 0)  {            printf ("content-type: text/html\r\n");            printf ("\ r \ n");           printf ("Hello  world!<br>\r\n ");           printf (" Request  number %d. ",  count++);       }       exit ( 0);  }   1.2 into fcgi_accept.

Enter this  fcgi_accept ()   method inside the file FCGI_STDIO.C. The C code    int fcgi_accept (void)    {       //variable Indicates whether the request is received. Default is Fasle, no request        if (!acceptcalled)  {            //judge whether it is CGI, the variable is global static, the next time will be used.            iscgi = fcgx_iscgi ();     The        //state is changed to receive requests.            acceptCalled = TRUE;            //the end of the request, empty the value to the initial value.            atexit (&fcgi_finish);        } else if (iscgi)  {           // is not the first request, and is a CGI program.            return (EOF);       }       if (iscgi)  {            //cgi initial assignment operation, does not care.            ...       } else  {           fcgx_stream *in, *out, * error;           //char**  string array.            FCGX_ParamArray envp;            //accept the request, this method is described in the following             int acceptresult = fcgx_accept (&AMP;IN,&NBSP;&AMP;OUT,&NBSP;&AMP;ERROR,&NBSP;&AMP;ENVP);            //receives failure, returns &LT;0, which is why the judgment on the loop is  while (fcgi_ Accept ()  >= 0)            if (acceptresult  < 0)  {               return acceptResult;            }            //assigns the resulting data to the corresponding output, input, data.            FCGI_stdin->stdio_stream = NULL;            FCGI_stdin->fcgx_stream = in;            FCGI_stdout->stdio_stream = NULL;            FCGI_stdout->fcgx_stream = out;           FCGI_stderr->stdio_stream = NULL;           FCGI_stderr->fcgx_stream = error;           environ = envp;   &NBSP;&NBsp;  }       //End        return 0;   }   1.3  fcgx_accept  (&in, &out, &error, &ENVP)

Wait for the method to receive the request, in the FCGIAOO.C. C code    static fcgx_request the_request;      int fcgx_accept (fcgx_ STREAM&NBSP;**IN,FCGX_STREAM&NBSP;**OUT,FCGX_STREAM&NBSP;**ERR,FCGX_PARAMARRAY&NBSP;*ENVP)    {    &NBSP;&NBSP;&NBSP;&NBSP;INT&NBSP;RC;//defines the returned variable.    whether the     //has been initialized.        if  (! libinitialized)  {            rc = fcgx_init ();            if  (RC)  {                return rc;           }        }       //receive data, the following describes        rc = fcgx_ Accept_r (&the_request);       //assigns values to corresponding streams and data.    &NBSP;&NBSP;&NBSP;&Nbsp;*in = the_request.in;       *out = the_request.out;        *err = the_request.err;       *envp  = the_request.envp;          return rc;  }    1.4 fcgx_accept_r  (), in fcgiapp.c;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.