gpt4 book ai didi

c - FCGX_Accept_r 失败,返回代码为 -88

转载 作者:行者123 更新时间:2023-11-30 17:04:46 29 4
gpt4 key购买 nike

我正在用 C++ 编写一个 fastcgi 应用程序,并且正在努力解决 FCGX_Accept_r() 退出而不阻塞且返回值为“-88”的问题。经过大量谷歌搜索后,我发现您必须正确初始化 FCGX 结构,更正 uds 套接字上的 umask,不要忘记 FCGX_Init() 并检查 FCGX_Init() 和 FCGX_InitRequest() 的返回代码。

我已经为我的程序完成了上述所有操作,但结果仍然相同。

所以我尝试从 fcgi 站点构建示例 C 应用程序:http://www.fastcgi.com/devkit/examples/threaded.c 然后它立即退出。

奇怪的是,他们没有任何打开的套接字。所以我添加了几行 - 打印出 FCGX_Accept_() 返回值并打开 uds 套接字。

这是生成的代码:

/*
* 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>
#include <sys/stat.h>

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

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


#define THREAD_COUNT 20

static int counts[THREAD_COUNT];

static void *doit(void *a)
{
int rc, i, thread_id = (int)a;
pid_t pid = getpid();
FCGX_Request request;
char *server_name;

FCGX_InitRequest(&request, 0, 0);

for (;;)
{
static pthread_mutex_t accept_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t counts_mutex = PTHREAD_MUTEX_INITIALIZER;

/* Some platforms require accept() serialization, some don't.. */
pthread_mutex_lock(&accept_mutex);
rc = FCGX_Accept_r(&request);
pthread_mutex_unlock(&accept_mutex);

if (rc < 0) {
printf("failed accept: %d\n", rc);
break;
}

server_name = FCGX_GetParam("SERVER_NAME", request.envp);

FCGX_FPrintF(request.out,
"Content-type: text/html\r\n"
"\r\n"
"<title>FastCGI Hello! (multi-threaded C, fcgiapp library)</title>"
"<h1>FastCGI Hello! (multi-threaded C, fcgiapp library)</h1>"
"Thread %d, Process %ld<p>"
"Request counts for %d threads running on host <i>%s</i><p><code>",
thread_id, pid, THREAD_COUNT, server_name ? server_name : "?");

sleep(2);

pthread_mutex_lock(&counts_mutex);
++counts[thread_id];
for (i = 0; i < THREAD_COUNT; i++)
FCGX_FPrintF(request.out, "%5d " , counts[i]);
pthread_mutex_unlock(&counts_mutex);

FCGX_Finish_r(&request);
}

return NULL;
}

int main(void)
{
int i;
pthread_t id[THREAD_COUNT];

FCGX_Init();

umask(0);
FCGX_OpenSocket("/tmp/fcgi.sock", 10);

for (i = 1; i < THREAD_COUNT; i++)
pthread_create(&id[i], NULL, doit, (void*)i);

doit(0);

return 0;
}

猜猜看:

# gcc fcgi.c -lpthread -lfcgi         
# ./a.out
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -failed accept: -88

有人可以指出我做错了什么吗?谢谢!

更新:

有趣的是这里的例子 https://habrahabr.ru/post/154187/作品。我看不出有多大区别。

最佳答案

问题是在示例中他们使用

FCGX_InitRequest(&request, 0, 0);

而第二个参数必须是套接字 ID。伙计们,很好的例子。

关于c - FCGX_Accept_r 失败,返回代码为 -88,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35574723/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com