fcgi与c++服务器demo示例

fcgi与c++构建多线程服务器demo示例

须安装fcgi库

yum -y install fcgi;

yum -y install spawn-fcgi;

源码demo.cpp

#include "fcgi_stdio.h"
#include <pthread.h>
#include <stdlib.h>

# define THREAD_NUM 1

void * thread_function(void *)
{
    int rc;
    FCGX_Request request;
    FCGX_InitRequest(&request,0,0);
    for(;;)
    {
	    static pthread_mutex_t req_locker = PTHREAD_MUTEX_INITIALIZER;
	    pthread_mutex_lock(&req_locker);
        rc = FCGX_Accept_r(&request);
        phtread_mutex_unlock(&req_locker);
        if(rc <0)
            break;
        FCGX_FPrintF(request.out,"Content-type: text/html\r\n"
        "\r\n"
        "<title>FastCGI Hello!</title>"
        "<h1>FastCGI Hello!</h1>");
        FCGX_Finish_r(&request);
    }
}


int main(void)
{
    pthread_t tid[THREAD_NUM];
    FCGX_Init();

    for(int i = 0;i <THREAD_NUM;i++)
    {
        pthread_create(&tid[i],NULL,thread_function,NULL);
    }

    for(int i = 0;i <THREAD_NUM;i++)
    {
        pthread_join(tid[i],NULL);
    }
    return 0;
}

编译:g++ demo.cpp -o demo -lpthread -lfcgi

执行脚本start.sh

#!/bin/bash
program=`basename $0`

if [ $# != 1 ]
then
	echo "Usage: {$program} <program_name> [kill],kill is optional agrument"
	exit -1
fi

killall -q -9 $1
killall -q -9 spawn-fcgi

if [ "$2"  == "kill" ] 
then
	exit 0
fi

SPAWNFCGI=/usr/bin/spawn-fcgi
EXEC=$1

$SPAWNFCGI -p 12345 -F 4 -- $EXEC
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值