如何在libfcgi中中断Accept方法?

时间:2015-11-12 13:31:08

标签: c++ multithreading fastcgi

我正在使用libfcgi(fcgiapp)库编写FastCGI应用程序。问题出在cmake_minimum_required(VERSION 2.8) project(STT_People_Tracker) cmake_policy(SET CMP0016 NEW) # compilation mode setup #set(CMAKE_BUILD_TYPE Release) set(CMAKE_BUILD_TYPE Debug) # set OpenCV directories using find_package. find_package(OpenCV 3 REQUIRED) # set environment variables set(SOURCES_PATH "${CMAKE_SOURCE_DIR}/Sources") set(INCLUDES_PATH "${SOURCES_PATH}/include") if(CMAKE_BUILD_TYPE MATCHES Debug) set(OUTPUT_PATH "${CMAKE_BINARY_DIR}/Debug") # Use absolute path message(STATUS "Compiling in DEBUG mode") elseif(CMAKE_BUILD_TYPE MATCHES Release) set(OUTPUT_PATH "${CMAKE_BINARY_DIR}/Release") message(STATUS "Compiling in RELEASE mode") endif() # Set output directory for STATIC libraries and executables set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${OUTPUT_PATH}) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_PATH}) include_directories(${INCLUDES_PATH}) include_directories(${OpenCV_INCLUDE_DIRS}) # compilation of executables message(STATUS "configuring executables...") add_executable(mainTest ${SOURCES_PATH}/mainTest.cpp) # Use simple name as a target # compilation of libraries message(STATUS "configuring libraries...") add_library(background_substractor ${SOURCES_PATH}/background_substractor.cpp) # Use simple name as a target # set linker options # Command below is no-op: OpenCV libraries enumerated using absolute paths # link_directories(${OpenCV_LINK_DIRECTORIES}) target_link_libraries(mainTest ${OpenCV_LIBS}) # Variable OpenCV_LIBS contains OpenCV libraries needed to link with target_link_libraries(background_substractor ${OpenCV_LIBS}) message(STATUS "cmake configuration complete") 方法中,它等待传入连接并阻塞线程。我可以看到,它没有任何超时,因此在请求到达之前程序流无法继续执行。

我尝试在FCGX_Accept_r方法中设置FCGI_FAIL_ACCEPT_ON_INTR标志以使accept函数中断,但没有成功。

  

设置FCGI_FAIL_ACCEPT_ON_INTR可防止FCGX_Accept()在被中断时重新启动。

我也尝试拨打FCGX_InitRequest,但它似乎拒绝新的传入连接,而不是停止侦听下一个连接。

  

防止lib接受任何新请求。信号处理程序安全。

你能告诉我还有什么方法可以打破FCGX_ShutdownPending等待吗?

3 个答案:

答案 0 :(得分:1)

作为解决方法,只需关闭套接字:

FCGX_Request request;
// ...
close(request.listen_sock);

答案 1 :(得分:1)

如果未设置信号处理程序FCGX_Accept_r标志,

SA_RESTART将在信号上以负返回值突破。

如果您希望您的进程正常关闭信号(例如SIGINT),请注册一个虚拟信号处理程序,如下所示:

struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = your_handler;
sigaction(signo, &act, NULL);

默认情况下,请务必使用sigactionsignal设置SA_RESTART标记,因为它会影响某些基元对信号的行为。

  

宏:int SA_RESTART 此标志控制信号发生时的情况   在某些原语(例如打开,读取或写入)期间传递,以及   信号处理程序正常返回。有两种选择:   库函数可以恢复,或者它可以返回带有错误代码的失败   EINTR。

答案 2 :(得分:0)

我有类似的问题。如果您在linux上运行,则需要关闭套接字以使FCGX_Accept_r返回并设置FCGI_FAIL_ACCEPT_ON_INTR。

我用来在Linux上停止FCGI的代码是:

FCGX_ShutdownPending();

if (miSocket != -1)
{
    shutdown(miSocket, SHUT_RD);
}