带有连接文件描述符的pthread竞争条件

时间:2012-12-10 02:20:19

标签: c sockets pthreads

我正在为学校作业实现一个简单的多线程Web服务器,并且遇到了与每个连接使用的连接文件描述符的一些同步问题。我最初的问题是,一个线程有时会关闭文件描述符(conn_fd),因为文件描述符也在另一个线程中使用。当另一个线程尝试发送()或recv()时,这会导致错误的文件描述符错误。

我的解决方法是存储当前是否打开每个文件描述符(最多1000个(我知道的任意数量和容易出错))。如果accept()返回的文件描述符已经打开,我的progam调用fcntl(conn_fd,F_DUPFD,0);创建一个重复的文件描述符,以便一个线程不会无意中关闭另一个线程需要使用的连接。我的程序似乎比我开始跟踪打开的文件描述符之前的工作要好,但我仍然有一些同步问题,我无法弄清楚如何解决。每个线程的开始路由中的conn_fd,process_connection_request()似乎都被破坏了。

我在针对我的服务器运行Siege时尝试使用Helgrind来隔离问题。不幸的是,我的代码在Helgrind下运行时从未崩溃。它确实表明conn_fd存在潜在的竞争条件,但我认为在main()和process_connection_request()中包装我周围的互斥锁将解决该问题。我以前从未开发过任何多线程或套接字程序,我怀疑有一些简单的我缺少。我非常感谢您在发送和接收时如何通过错误的文件描述符解决问题的任何见解和建议。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <fcntl.h>
#include <time.h>
#include <signal.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define SERVER_PORT 50040
#define MAX_LISTEN_BACKLOG 1024
#define MAX_FILENAME_LENGTH 255
#define REQUEST_BUFF_SIZE 8192
#define THREAD_POOL_SIZE 16

// function prototypes
int int_len(int i);
void *process_connection_request(void *conn_fd);
void sig_handler(int sig);

pthread_t thread;
pthread_attr_t thread_attr;
pthread_mutex_t conn_fd_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t stats_mutex = PTHREAD_MUTEX_INITIALIZER;

int sock_fd;
int conn_fds_open[1000];

int main(void)
{
    struct sockaddr_in server_addr;
    struct sockaddr_in client_addr;
    int conn_fd;
    int client_len = sizeof(client_addr);

    if((sock_fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("socket() Failed");
        exit(EXIT_FAILURE);     
    }

    // set socket options so that we can reuse the socket
    const int sock_opt_val = 1;
    const socklen_t sock_opt_len = sizeof(sock_opt_val);
    setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (void *) &sock_opt_val, sock_opt_len);

    memset(&conn_fds_open, 0, 1000 * sizeof(int));
    memset(&server_addr.sin_zero, 0, sizeof(server_addr.sin_zero));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(SERVER_PORT);
    server_addr.sin_addr.s_addr = INADDR_ANY;

    if(bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
    {
        perror("bind() Failed");
        close(sock_fd);
        exit(EXIT_FAILURE);         
    }

    if(listen(sock_fd, MAX_LISTEN_BACKLOG) < 0)
    {
        perror("listen() Failed");
        close(sock_fd);
        exit(EXIT_FAILURE);     
    }

    if(pthread_attr_init(&thread_attr) != 0)
    {
        perror("pthread_attr_init Failed");
        close(sock_fd);
        exit(EXIT_FAILURE);         
    }

    if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED) != 0)
    {
        perror("pthread_attr_setdetachstate Failed");
        close(sock_fd);
        exit(EXIT_FAILURE);     
    }

    signal(SIGINT, sig_handler);
    printf("sock_fd %d\n", sock_fd);

    while(1)
    {
        if((conn_fd = accept(sock_fd, (struct sockaddr *)&client_addr, &client_len)) < 0)
        {
            perror("accept() Failed");
            close(sock_fd);
            exit(EXIT_FAILURE);     
        }

        pthread_mutex_lock(&conn_fd_mutex);
        if(conn_fds_open[conn_fd] == 1)
        {
            conn_fd = fcntl(conn_fd, F_DUPFD, 0);
        }

        conn_fds_open[conn_fd] = 1;
        printf("main fd: %d\n", conn_fd);
        pthread_mutex_unlock(&conn_fd_mutex);
        pthread_create(&thread, &thread_attr, process_connection_request, (void *)&conn_fd);
    }
}

int int_len(int i)
{
    return (i == 0) ? 1 : floor(log10(abs(i))) + 1;
}

void *process_connection_request(void *conn_fd_ptr)
{
    FILE *requested_file = NULL;
    FILE *stats_file = NULL;
    char *request_buff;
    char *response_buff;
    char *file_buff;
    char *stats_buff;
    char *filename_start;
    char *filename_stop;
    char requested_filename[MAX_FILENAME_LENGTH];
    int conn_fd = *(int *)conn_fd_ptr;
    int requested_file_size;
    int response_buff_size;
    int stats_buff_size;
    int amt_sent = 0;
    int response_code;
    time_t now;
    char time_buff[30];
    time(&now);
    strftime(time_buff, 30, "%a, %d %b %Y %X GMT", gmtime(&now));

    printf("thread fd: %d\n", conn_fd);

    if((request_buff = calloc(REQUEST_BUFF_SIZE, sizeof(char))) == NULL)
    {
        perror("Calloc Failed");
        close(conn_fd);
        close(sock_fd);
        exit(EXIT_FAILURE);     
    }

    if(recv(conn_fd, (void *)request_buff, REQUEST_BUFF_SIZE, 0) < 0)
    {
        perror("recv() Failed");
        close(conn_fd);
        close(sock_fd);
        exit(EXIT_FAILURE); 
    }

    // extract the filename from the request header
    filename_start = &request_buff[5];
    filename_stop = strstr(request_buff, " HTTP");

    if((strncmp(request_buff, "GET /", 5) != 0) || (filename_stop == NULL))
    {
        perror("Invalid Request");
        close(conn_fd);
        close(sock_fd);
        exit(EXIT_FAILURE);         
    }

    strncpy(requested_filename, &request_buff[5], filename_stop - filename_start);
    free(request_buff);
    requested_filename[filename_stop - filename_start] = '\0';

    if((requested_file = fopen(requested_filename, "r")) != NULL)
    {
        response_code = 200;
        fseek(requested_file, 0, SEEK_END);
        requested_file_size = ftell(requested_file);
        fseek(requested_file, 0, SEEK_SET);
        file_buff = calloc(requested_file_size + 1, sizeof(char));
        response_buff = calloc((83 + strlen(time_buff) + int_len(requested_file_size) + requested_file_size), sizeof(char));

        if(file_buff == NULL || response_buff == NULL)
        {
            perror("Calloc Failed");
            close(conn_fd);
            close(sock_fd);
            exit(EXIT_FAILURE);             
        }

        fread(file_buff, 1, requested_file_size, requested_file);
        response_buff_size = sprintf(response_buff, "HTTP/1.1 200 OK\nDATE: %s\nContent-Length: %d\nConnection: close\nContent-Type: text/html\n\n%s", time_buff, requested_file_size, file_buff);
        free(file_buff);
        fclose(requested_file);     
    }
    else
    {
        response_code = 404;
        response_buff = malloc(25 * sizeof(char));
        strcpy(response_buff, "HTTP/1.1 404 Not Found\n\n");
        response_buff_size = 25;
    }

    while(amt_sent < response_buff_size)
    {
        int ret = send(conn_fd, response_buff + amt_sent, response_buff_size - amt_sent, 0);
        if (ret < 0)
        {
            perror("send() Failed.");
            close(conn_fd);
            close(sock_fd);
            exit(EXIT_FAILURE); 
        }
        amt_sent += ret;        
    }

    free(response_buff);
    pthread_mutex_lock(&conn_fd_mutex);
    conn_fds_open[conn_fd] = 0;
    close(conn_fd);
    pthread_mutex_unlock(&conn_fd_mutex);

    // yield to any other connection threads before writing to the stats file
    pthread_yield();    

    pthread_mutex_lock(&stats_mutex);
    if((stats_file = fopen("stats.txt", "a")) != NULL)
    {
        if((stats_buff = malloc((strlen(time_buff) + 51 + strlen(requested_filename)) * sizeof(char))) != NULL)
        {
            stats_buff_size = sprintf(stats_buff, "Date - %s | Response Code - %d | Requested File - %s\n", time_buff, response_code, requested_filename);
            fwrite(stats_buff, stats_buff_size, 1, stats_file);
            free(stats_buff);
        }

        fclose(stats_file);
    }
    pthread_mutex_unlock(&stats_mutex);
}

void sig_handler(int sig)
{
    close(sock_fd);
    exit(0);
}

1 个答案:

答案 0 :(得分:5)

发生可能的竞争条件是因为accept()返回的套接字(文件)描述符通过引用传递给线程函数。

然后异步地指定套接字(文件)描述符的线程函数特定副本。由于下一次调用accept(),后面的 引用的套接字(文件)描述符的值发生了变化。

要更改此

  • 要么将传递给线程函数的void指针误用为整数(不推荐)
  • 或动态创建int的实例,以便将accept()来电的结果分配给。{/ li>

另外(由 Nemo评论accept()总是返回一个新的套接字。