主题 : LINUX下多线程编程,运行出错:Create pthread error! Resource temporarily unavailable 复制链接 | 浏览器收藏 | 打印
级别: 侠客
UID: 6846
精华: 0
发帖: 55
金钱: 425 两
威望: 215 点
综合积分: 110 分
注册时间: 2009-06-19
最后登录: 2012-03-21
楼主  发表于: 2009-09-03 09:04

 LINUX下多线程编程,运行出错:Create pthread error! Resource temporarily unavailable

LINUX下多线程编程,运行出错:Create pthread error! Resource temporarily unavailable

把相关的代码贴上来,请各位大大们和版主大人帮我分析一下!谢谢!

void pthread_comsend(void)  {
//    printf("begin 485 send:%s !\n",tcpbuf);

//    printf("pthread_comsend : tcpbuf=%s\n",tcpbuf);
    char *tcpbuftmp;
    
    pthread_mutex_lock(&mut);
    tcpbuftmp=malloc(sizeof(tcpbuf));
    tcpbuftmp=tcpbuf;
    pthread_mutex_unlock(&mut);
    
    rs485_send(rsfd,comfd,tcpbuftmp);
    
    memset(tcpbuf, 0, TCP_BUF_SIZE);  //when sent, clear the send buffer
    
//    printf("end 485 send!\n");
}









    while(1)  {
        tmp_inset = inset;
        sin_size=sizeof(struct sockaddr_in);
        
        //memset(tcpbuf, 0, TCP_BUF_SIZE);
        
        if ( select(MAX_SOCK_FD, &tmp_inset, NULL, NULL, 0) <= 0)  //&timeout
        {
            perror("select");
            close(sockfd);
            exit(1);
        }
        
        for (selectfd = 0; selectfd < MAX_SOCK_FD; selectfd++)
        {
            if (FD_ISSET(selectfd, &tmp_inset) > 0)
            {
                if (selectfd == sockfd)    { //there's a client waiting for connect server !
                    if ((client_fd = accept(sockfd, (struct sockaddr *)&client_sockaddr, &sin_size))== -1)
                    {
                        perror("accept");
                        exit(1);
                    }
                    FD_SET(client_fd, &inset);
                    strcpy(clientip[client_fd], inet_ntoa(client_sockaddr.sin_addr));     //record the selectfd ip address

                    printf("New connection from %d(socket)\n", client_fd);
                    printf("ip address is :%s \n", inet_ntoa(client_sockaddr.sin_addr));
                }
                else //recieve order or data !
                {
                
                    pthread_mutex_lock(&mut);
                    ret= recv(selectfd, tcpbuf, TCP_BUF_SIZE, 0 );
                    pthread_mutex_unlock(&mut);
                    
                    
                    if ( ret > 0) {
                        pthread_mutex_lock(&mut);
                        printf("Received a message from %d  ip=%s : %s\n", selectfd,clientip[selectfd], tcpbuf);
                        pthread_mutex_unlock(&mut);
                        
                        recentfd=selectfd;
                        //printf("main(hadn't writen): tcpbuf=%s\n",tcpbuf);
                        
                        fputs("recieve from ",fp);
                        fputs(clientip[selectfd],fp);
                        fputc(':',fp);
                        pthread_mutex_lock(&mut);
                        fputs(tcpbuf,fp);
                        pthread_mutex_unlock(&mut);
                        fputc('\n',fp);
                        rewind(fp);

                        //printf("main(had writen): tcpbuf=%s\n",tcpbuf);
                        
                        ret=pthread_create(&pth_id2,NULL,(void *) pthread_comsend,NULL);        //pthread instead of fork !!
                        if(ret!=0){
                            //printf("pth_id2=%d\n",pth_id2);
                            pprintf("error tcpbuf",tcpbuf,20);
                            printf ("Create pthread error! %s \n",strerror(ret));  //////////////////////////////////////////////////////////////////////////////程序出错点在这里!
                            exit (1);
                        }
                        
                        //printf("PTHREAD_THREADS_MAX=%d\n",PTHREAD_THREADS_MAX);
                                   //back send the recieve string
                        /*            if ((sendbytes = send(selectfd, tcpbuf, strlen(tcpbuf), 0)) == -1) {
                                          perror("send");
                                          exit(1);
                                    }
                                     printf("send a message: %s\n", tcpbuf);      */
                    }
                    else          //recieve exit order , and print the message
                    {
                        close(selectfd);
                        FD_CLR(selectfd, &inset);
                        strcpy(clientip[selectfd], "\0");  //clear IP table
                        pthread_mutex_lock(&mut);
                        printf("Client %d(socket)  %s(ip address) has left\n", selectfd,clientip[selectfd], tcpbuf);
                        pthread_mutex_unlock(&mut);
                        //disdoor=1;
                    }                        
                }        
            } /* end of if FD_ISSET*/    
        } /* end of for selectfd*/        
    }



那个线程互拆锁是我后来加上去的,加上之后,程序运行还是这个错误。我不知道线程互拆锁是不是这个用法,还是我没有用对?还是对我出现的问题没有作用?先感谢各位前辈!
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
1楼  发表于: 2009-09-03 09:11

Return Value

If successful, the pthread_create() function shall return zero; otherwise, an error number shall be returned to indicate the error.

Errors

The pthread_create() function shall fail if:
EAGAIN
The system lacked the necessary resources to create another thread, or the system-imposed limit on the total number of threads in a process {PTHREAD_THREADS_MAX} would be exceeded.

这是pthread_create() manpage中的相关描述,希望对你有用
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
级别: 侠客
UID: 6846
精华: 0
发帖: 55
金钱: 425 两
威望: 215 点
综合积分: 110 分
注册时间: 2009-06-19
最后登录: 2012-03-21
2楼  发表于: 2009-09-03 09:17

 回 1楼(kasim) 的帖子

可以确定,我程序里没有线程数量超标,同时只有四个线程在运行,两个死循环的线程,一个据条件而触发创建的活动线程(就是这个里面出了问题。),还有一个就是主程序自己是一个线程(不知道我这么说是否合理)。

活动进程是一次性的,执行完了自动结束。

出现这个错误,会是什么原因呢?请帮我分析一下,谢谢!
级别: 侠客
UID: 6846
精华: 0
发帖: 55
金钱: 425 两
威望: 215 点
综合积分: 110 分
注册时间: 2009-06-19
最后登录: 2012-03-21
3楼  发表于: 2009-09-03 10:28
偶顶!
级别: 侠客
UID: 6846
精华: 0
发帖: 55
金钱: 425 两
威望: 215 点
综合积分: 110 分
注册时间: 2009-06-19
最后登录: 2012-03-21
4楼  发表于: 2009-09-03 13:29
问题基本解决了,在要创建线程的函数里最上面加上一句:  

  pthread_detach(pthread_self());     // make the pthread separated out ,and hold the pthread exit, release resourses

线程函数里最后面加上句:


pthread_exit(0);