c的并发
我想在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论