简体   繁体   English

C - socketserver:pthread_create - 无法创建线程 errno 12

[英]C - socketserver: pthread_create - Failed to create thread errno 12

I create Threads in a C Program for every incoming Client connection.我在 C 程序中为每个传入的客户端连接创建线程。 While Stress Testing with 400 parallel connections i got error 12 from pthread_create.在使用 400 个并行连接进行压力测试时,我从 pthread_create 收到错误 12。 I know that this can happen when running out of ressources, but should not be the case whith that few connections.. so why is that?我知道在资源耗尽时可能会发生这种情况,但在连接数很少的情况下不应该出现这种情况。那为什么呢?

 #define MAX_CONNECTIONS 1000

pthread_t tid[MAX_CONNECTIONS];
        int i = 0;


        while(1) {

                        //Accept call creates a new socket for the incoming connection
                         addr_size = sizeof serverStorage;

                         // allocate socketfd on the heap to avoid nasty race conditions
                         int * socketfd;
                         socketfd = malloc(sizeof(int));

                         *socketfd = accept(listenfd, (struct sockaddr *) &serverStorage, &addr_size);

                        //create a thread for each client request and assign the client request to it for processing
                        //the main thread can now serve the next request
                        int err = pthread_create(&tid[i], NULL, socketThread, socketfd);
                        if(err) {
                                slog_error(0,"Failed to create thread %d",err);
                                exit(3);
                        }

                        if( i >= MAX_CONNECTIONS)
                        {
                                i = 0;
                                while( i < MAX_CONNECTIONS )
                                {
                                        pthread_join(tid[i++],NULL);
                                }
                                i = 0;
                        }


 

popen part of the thread打开部分线程

 
                   if(injector) {
                        DEBUG slog_debug(0, "injector reached");
                        DEBUG slog_debug(0,"Starting injector helper %s",injector_command);
                        errno = 0;
                        if ( (pop = popen(injector_command, "w") ) == NULL ) {
                        slog_error(0,"popen injector FAILED continue without injection %d",errno);
                        pop = 0;
                        }
                        if (pop) {
                                DEBUG slog_debug(0, "print to injector reached");
                                if( (bytes = fwrite(nm_data,1,strlen(nm_data),pop)) != strlen(nm_data)) {
                                        slog_error(0,"Failed to write to injector %s,%s,%d",hostname,bytes,errno);
                                        pop = (FILE *)0; /* stop writing */
                                        exit(1);
                                }
                        }
                        fflush(pop);
                        pclose(pop);

                    }
                    

pthread_create(&tid[i], NULL, socketThread, &socketfd) creates a nasty race condition. pthread_create(&tid[i], NULL, socketThread, &socketfd) 创建了一个讨厌的竞争条件。 You're going to have real problems if another call to accept() returns before the thread you created dereferences the address to get its socket.如果在您创建的线程取消引用地址以获取其套接字之前对 accept() 的另一个调用返回,您将遇到真正的问题。 see update code in initial question.请参阅初始问题中的更新代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM