c的并发

发布于 2025-02-01 03:33:51 字数 1764 浏览 2 评论 0原文

我想在C中制作一个简单的Web服务器,以同时处理请求。到目前为止,我得到了以下代码,该代码叉作过程并在子过程中执行请求_handle。 (至少这是我的目标)但是,执行请求会阻止周期,直到第一个请求完成之前,第二个请求才被接受。我应该修复什么以允许Web服务器同时处理请求?

#include <stdio.h>
#include "request.h"
#include "io_helper.h"

char default_root[] = ".";

//
// ./wserver [-p <portnum>] [-t threads] [-b buffers] 
// 
// e.g.
// ./wserver -p 2022 -t 5 -b 10
// 
int main(int argc, char *argv[]) {
    int c;
    char *root_dir = default_root;
    int port = 10000;
    int threads = 2;
    int buffer_size = 5;

    while ((c = getopt(argc, argv, "p:t:b:")) != -1)
        switch (c) {
            case 'p':
                port = atoi(optarg);
                break;
            case 't':
                threads = atoi(optarg);
                break;
            case 'b':
                buffer_size = atoi(optarg);
                break;
            default:
                fprintf(stderr, "usage: ./wserver [-p <portnum>] [-t threads] [-b buffers] \n");
                exit(1);
            }



    printf("Server running on port: %d, threads: %d, buffer: %d\n", port, threads, buffer_size);

    // run out of this directory
    chdir_or_die(root_dir);

    // now, get to work
    int listen_fd = open_listen_fd_or_die(port);
    while (1) {
        struct sockaddr_in client_addr;
        int client_len = sizeof(client_addr);
        int conn_fd = accept(listen_fd, (sockaddr_t *) &client_addr, (socklen_t *) &client_len);

        pid_t child_pid;
        child_pid = fork();

        if (child_pid == 0) {
            close(listen_fd);
            request_handle(conn_fd);
            close(conn_fd);
            exit(0);
        } else {
            close(conn_fd);
        }
    }
    close(listen_fd);
    return 0;
}

I want to make a simple web server in C that handles requests concurrently. So far I got the following code that forks the process and executes the request_handle in the child process. (That is at least what I aim to have) but still, executing a request blocks the cycle and until the first request is completed, the second request is not accepted. What should I fix to allow the web server to handle requests concurrently?

#include <stdio.h>
#include "request.h"
#include "io_helper.h"

char default_root[] = ".";

//
// ./wserver [-p <portnum>] [-t threads] [-b buffers] 
// 
// e.g.
// ./wserver -p 2022 -t 5 -b 10
// 
int main(int argc, char *argv[]) {
    int c;
    char *root_dir = default_root;
    int port = 10000;
    int threads = 2;
    int buffer_size = 5;

    while ((c = getopt(argc, argv, "p:t:b:")) != -1)
        switch (c) {
            case 'p':
                port = atoi(optarg);
                break;
            case 't':
                threads = atoi(optarg);
                break;
            case 'b':
                buffer_size = atoi(optarg);
                break;
            default:
                fprintf(stderr, "usage: ./wserver [-p <portnum>] [-t threads] [-b buffers] \n");
                exit(1);
            }



    printf("Server running on port: %d, threads: %d, buffer: %d\n", port, threads, buffer_size);

    // run out of this directory
    chdir_or_die(root_dir);

    // now, get to work
    int listen_fd = open_listen_fd_or_die(port);
    while (1) {
        struct sockaddr_in client_addr;
        int client_len = sizeof(client_addr);
        int conn_fd = accept(listen_fd, (sockaddr_t *) &client_addr, (socklen_t *) &client_len);

        pid_t child_pid;
        child_pid = fork();

        if (child_pid == 0) {
            close(listen_fd);
            request_handle(conn_fd);
            close(conn_fd);
            exit(0);
        } else {
            close(conn_fd);
        }
    }
    close(listen_fd);
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文