英文:
Why does this multi-threaded program get blocked?
问题
以下是您要翻译的代码部分:
这是我尝试在Web服务器中实现多线程的代码。
#include <stdio.h>
#include <pthread.h>
#include "request.h"
#include "io_helper.h"
char default_root[] = ".";
int open_connections = 0;
int n_buffers = 1;
pthread_mutex_t mutex;
pthread_cond_t cond_empty;
pthread_cond_t cond_full;
int *conn_fds;
// 缓冲区消费者
void *manage_conn() {
while (1) {
for (int i = 0; i < n_buffers; ++i) {
pthread_mutex_lock(&mutex);
while (!open_connections) {
printf("IN.\n");
pthread_cond_wait(&cond_full, &mutex);
printf("OUT.\n");
}
if (conn_fds[i]) {
request_handle(conn_fds[i]);
close_or_die(conn_fds[i]);
conn_fds[i] = 0;
open_connections--;
pthread_cond_signal(&cond_empty);
}
pthread_mutex_unlock(&mutex);
}
}
}
int main(int argc, char *argv[]) {
enum {
FIFO,
SFF
};
int c;
char *root_dir = default_root;
int port = 10002;
int n_threads = 1;
int schedalg = FIFO;
while ((c = getopt(argc, argv, "d:p:t:b:s:")) != -1) {
switch (c) {
case 'd':
root_dir = optarg;
break;
case 'p':
port = atoi(optarg);
break;
case 't':
n_threads = atoi(optarg);
break;
case 'b':
n_buffers = atoi(optarg);
printf("This.\n");
break;
case 's':
if (!strcmp("FIFO", optarg)) schedalg = FIFO;
else if (!strcmp("SFF", optarg)) schedalg = SFF;
else {
printf("Invalid scheduling algorithm.\n");
return 0;
}
break;
default:
fprintf(stderr, "usage: wserver [-d basedir] [-p port]\n");
exit(1);
}
}
pthread_t *cid = malloc(n_threads * sizeof(*cid));
conn_fds = malloc(n_buffers * sizeof(*conn_fds));
for (int i = 0; i < n_buffers; i++) {
conn_fds[i] = 0;
}
for (int i = 0; i < n_threads; ++i) {
pthread_create(&cid[i], NULL, manage_conn, NULL);
}
// 切换到指定目录
chdir_or_die(root_dir);
// 现在开始工作
int listen_fd = open_listen_fd_or_die(port);
int i = 0;
while (1) {
struct sockaddr_in client_addr;
int client_len = sizeof(client_addr);
pthread_mutex_lock(&mutex);
while (open_connections >= n_buffers) {
pthread_cond_wait(&cond_empty, &mutex);
}
int conn_fd = accept_or_die(listen_fd, (sockaddr_t *) &client_addr, (socklen_t *) &client_len);
conn_fds[i] = conn_fd;
i = (i + 1) % n_buffers;
open_connections++;
pthread_cond_signal(&cond_full);
pthread_mutex_unlock(&mutex);
printf("New connection opened.\n");
}
return 0;
}
希望这能帮助您。如果您有任何进一步的问题,请随时提出。
英文:
This is my attempt of implementing multi-threading in a web server.
#include <stdio.h>
#include <pthread.h>
#include "request.h"
#include "io_helper.h"
char default_root[] = ".";
int open_connections = 0;
int n_buffers = 1;
pthread_mutex_t mutex;
pthread_cond_t cond_empty;
pthread_cond_t cond_full;
int *conn_fds;
//Buffer consumer
void *manage_conn() {
while(1) {
for (int i = 0; i < n_buffers; ++i)
{
pthread_mutex_lock(&mutex);
while(!open_connections) {
printf("IN.\n");
pthread_cond_wait(&cond_full, &mutex);
printf("OUT.\n");
}
if(conn_fds[i]) {
request_handle(conn_fds[i]);
close_or_die(conn_fds[i]);
conn_fds[i] = 0;
open_connections--;
pthread_cond_signal(&cond_empty);
}
pthread_mutex_unlock(&mutex);
//printf("New connection closed.\n");
}
}
}
int main(int argc, char *argv[]) {
enum {
FIFO,
SFF
};
int c;
char *root_dir = default_root;
int port = 10002;
int n_threads = 1;
int schedalg = FIFO;
while ((c = getopt(argc, argv, "d:p:t:b:s:")) != -1) {
switch (c) {
case 'd':
root_dir = optarg;
break;
case 'p':
port = atoi(optarg);
break;
case 't':
n_threads = atoi(optarg);
break;
case 'b':
n_buffers = atoi(optarg);
printf("This.\n");
break;
case 's':
if(!strcmp("FIFO", optarg)) schedalg = FIFO;
else if(!strcmp("SFF", optarg)) schedalg = SFF;
else {
printf("Invalid scheduling algorithm.\n");
return 0;
}
break;
default:
fprintf(stderr, "usage: wserver [-d basedir] [-p port]\n");
exit(1);
}
}
pthread_t *cid = malloc(n_threads * sizeof(*cid));
conn_fds = malloc(n_buffers * sizeof(*conn_fds));
for(int i = 0; i < n_buffers; i++) {
conn_fds[i] = 0;
}
for (int i = 0; i < n_threads; ++i)
{
pthread_create(&cid[i], NULL, manage_conn, NULL);
}
// run out of this directory
chdir_or_die(root_dir);
// now, get to work
int listen_fd = open_listen_fd_or_die(port);
int i = 0;
while (1) {
struct sockaddr_in client_addr;
int client_len = sizeof(client_addr);
pthread_mutex_lock(&mutex);
while(open_connections >= n_buffers) {
pthread_cond_wait(&cond_empty, &mutex);
}
int conn_fd = accept_or_die(listen_fd, (sockaddr_t *) &client_addr, (socklen_t *) &client_len);
conn_fds[i] = conn_fd;
i = (i + 1) % n_buffers;
open_connections++;
pthread_cond_signal(&cond_full);
pthread_mutex_unlock(&mutex);
printf("New connection opened.\n");
}
return 0;
}
The main thread takes care of accepting new connections and places their corresponding file descriptors into a buffer.
Through the command line arguments, the user specifies how many additional threads will be created and how big the buffer for the file descriptors will be. This new threads will respond to and close the new connections via manage_conn
.
For now I'm testing the server with these arguments:
> -b 20 -t 5
5 additional threads will manage the connections and the buffer size is 20.
I execute the server program and a client program that tells the server to run spin.cgi (a program that just spins for the specified amount of time).
Normally, less than 5 threads run and the main one accepts the new connection request but everything gets blocked after that.
First of all, I don't understand what happens to the other threads that are never executed.
However, my main question is why the threads that were waiting for cond_full never wake up when it is supposed to be signaled by the main thread after opening a new connection.
I already tried to debug this, but the error never shows up (I suppose it is because of the different timing).
Edit:
I forgot to initialize the mutex and the condition variables. I already changed that but the problem is is still there.
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond_empty = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond_full = PTHREAD_COND_INITIALIZER;
答案1
得分: 1
This line was causing the problem.
这行导致了问题。
int conn_fd = accept_or_die(listen_fd, (sockaddr_t *) &client_addr, (socklen_t *) &client_len);
它一直在等待新的连接请求,但同时它一直保持着互斥锁的锁定,所以它没有让其他线程运行。
它一直在等待新的连接请求,但同时它一直保持着互斥锁的锁定,所以它没有让其他线程运行。
What I did to prevent this is unlock the mutex before executing it and lock again after it accepted a new request.
为了防止这个问题,我在执行之前解锁了互斥锁,然后在接受新请求后再次锁定了它。
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&mutex);
int conn_fd = accept_or_die(listen_fd, (sockaddr_t *) &client_addr, (socklen_t *) &client_len);
int conn_fd = accept_or_die(listen_fd, (sockaddr_t *) &client_addr, (socklen_t *) &client_len);
pthread_mutex_lock(&mutex);
pthread_mutex_lock(&mutex);
英文:
This line was causing the problem.
int conn_fd = accept_or_die(listen_fd, (sockaddr_t *) &client_addr, (socklen_t *) &client_len);
It kept waiting for a new connection request, but at the same time it was keeping the mutex locked, so it didn't let the other threads run.
What I did to prevent this is unlock the mutex before executing it and lock again after it accepted a new request.
pthread_mutex_unlock(&mutex);
int conn_fd = accept_or_die(listen_fd, (sockaddr_t *) &client_addr, (socklen_t *) &client_len);
pthread_mutex_lock(&mutex);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论