添加带有互斥锁的共享内存组件后进程池不工作
我正在用 C 语言编写一个小型 Web 服务器,该服务器应该使用进程池来服务多个客户端请求。在添加共享内存组件来存储服务器发送的流量数量之前,进程池工作正常。之前所有请求都是由不同的进程处理的。现在它们都由同一流程处理。我没有更改代码来 fork() 子进程,这让我感到困惑。
typedef struct {
long int traffic;
pthread_mutex_t muxlock;
} shmem;
/*Creating the shared memory and,setting its size, closing | Initializing the mutex*/
shm_unlink("/sum_traffic");
int shmfd = shm_open("/sum_traffic", O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); //Creates the shared memory
ftruncate(shmfd,sizeof(int)*8+sizeof(shmem)); //Sets the size of the shared memory
shmem *memptr = mmap(NULL, (sizeof(int)*8+sizeof(shmem)), PROT_READ | PROT_WRITE, MAP_SHARED, shmfd,0); //Maps a pointer for us to work with
pthread_mutex_init(&(*memval).muxlock,NULL); //Initialize the mutex lock
//Forking 10 worker processes
int i;
for(i=0; i<11;i++){
if(pid>0){
pid=fork();
}
}
if(pid < 0){
fprintf(stderr, "Error forking, error: %d\n",errno);
exit(EXIT_FAILURE);
}
//Worker-processes
while(1)
if(pid == 0){
//Accept connection on list_s
if((conn_s = accept(list_s, NULL, NULL)) < 0){
fprintf(stderr , "Error calling accept \n");
exit(EXIT_FAILURE);
}
httpRequest request;
request = parseRequest(getMessage(conn_s)); //Parses the http GET request
headertraffic=selectHeader(conn_s,request.returncode); //Selects a Header file to send
currenttraffic=printFile(conn_s,request.filename); //Serves the requested file
pthread_mutex_lock(&(*memval).muxlock); //Lock the mutex to write to shared memory
(*memval).traffic=((*memval).traffic+currentdata+headerdata);
pthread_mutex_unlock(&(*memval).muxlock); // Unlock mutex
printf("PID: %d\n",getpid());
(void)close(conn_s);
}
非常感谢帮助!
I am programming a small web server in C that is supposed to serve multiple client requests using a process pool. The process pool was working fine, before adding a shared memory component to store the number of traffic sent by the server. All requests were handled by different processes before. Now they are all handled by the same process. I haven't changed the code to fork() the child-processes, which is what baffles me..
typedef struct {
long int traffic;
pthread_mutex_t muxlock;
} shmem;
/*Creating the shared memory and,setting its size, closing | Initializing the mutex*/
shm_unlink("/sum_traffic");
int shmfd = shm_open("/sum_traffic", O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); //Creates the shared memory
ftruncate(shmfd,sizeof(int)*8+sizeof(shmem)); //Sets the size of the shared memory
shmem *memptr = mmap(NULL, (sizeof(int)*8+sizeof(shmem)), PROT_READ | PROT_WRITE, MAP_SHARED, shmfd,0); //Maps a pointer for us to work with
pthread_mutex_init(&(*memval).muxlock,NULL); //Initialize the mutex lock
//Forking 10 worker processes
int i;
for(i=0; i<11;i++){
if(pid>0){
pid=fork();
}
}
if(pid < 0){
fprintf(stderr, "Error forking, error: %d\n",errno);
exit(EXIT_FAILURE);
}
//Worker-processes
while(1)
if(pid == 0){
//Accept connection on list_s
if((conn_s = accept(list_s, NULL, NULL)) < 0){
fprintf(stderr , "Error calling accept \n");
exit(EXIT_FAILURE);
}
httpRequest request;
request = parseRequest(getMessage(conn_s)); //Parses the http GET request
headertraffic=selectHeader(conn_s,request.returncode); //Selects a Header file to send
currenttraffic=printFile(conn_s,request.filename); //Serves the requested file
pthread_mutex_lock(&(*memval).muxlock); //Lock the mutex to write to shared memory
(*memval).traffic=((*memval).traffic+currentdata+headerdata);
pthread_mutex_unlock(&(*memval).muxlock); // Unlock mutex
printf("PID: %d\n",getpid());
(void)close(conn_s);
}
Help is much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定这是否是您的真实代码,但一些错误检查会有所帮助。
这可能不是您的问题,但一个影响因素是您尚未将互斥体设置为由多个进程共享。您不能使用
pthread_mutex_init()
中的默认属性。您必须使用 pthread_mutexattr_init()初始化互斥属性,然后调用
pthread_mutexattr_setpshared()然后调用
pthread_mutex_init()`。Not sure if this is your real code but some error checking would be helpful.
This may not be your problem but a contributing factor is that you haven't set the mutex to be shared by multiple processes. You can't take the default attributes in
pthread_mutex_init()
. You have to initialize a mutex attribute with pthread_mutexattr_init()and then call
pthread_mutexattr_setpshared()and then call
pthread_mutex_init()`.