![]() |
install | documentation | examples | articles | changelog 16.6.0 released on Mar 08, 2023 | articles updated on Mar 20, 2023
|
Plain C programs (linked with fcgi, the FastCGI library) can run as server applications with vf FastCGI program manager, which is used to start, stop and manage such applications.See below
#include "fcgi_stdio.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
static int busy = 0; // busy processing request?
static int end_program = 0; // flag to terminate process
// Handle termination signal by setting flag
void signal_handler(int sig) {
if (sig == SIGTERM) {
// if not busy, exit now. If busy, exit when the main loop ends
if (busy == 0) exit(0);
end_program = 1;
return;
}
}
// Main FCGI process
void main(void) {
// Set termination signal handler
struct sigaction psa;
memset (&psa, 0, sizeof (psa));
psa.sa_handler = signal_handler;
sigaction(SIGTERM, &psa, NULL);
// main FCGI request-processing loop, simply display Hello world page each time
while (FCGI_Accept() >= 0) {
busy = 1; // program busy now
printf("Content-type: text/html\r\n"
"\r\n"
"<title>Hello World!</title>"
"<h1>Hello there!</h1>");
busy = 0; // program no longer busy
// exit graceful at the end of request processing loop
if (end_program) exit(0);
}
}
sudo vf -i -u $USER plain_fcgi
For OpenSUSE, use the following:gcc -o /var/lib/vv/bld/plain_fcgi/plain_fcgi plain_fcgi.c -lfcgi
gcc -o /var/lib/vv/bld/plain_fcgi/plain_fcgi plain_fcgi.c -lfcgi -I /usr/include/fastcgi
Or if you are using TCP to connect to your application, in this case TCP port 2300:vf -w 2 plain_fcgi
vf -w 2 -p 2300 plain_fcgi
If you wish to stop the FastCGI server and your application:http://127.0.0.1/plain_fcgi
Whenever you recompile your C program, vf will automatically reload it. For more information on all the options available, see vf.vf -m quit plain_fcgi