This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Process hang(100% CPU Usage) when concurrent calling select(),cygwin1.5.5-1 WinXP/Win2000


hi,all
    I have reported this problem a week ago.I belive that the test_select.c(see below) is 
correct.The usage of select() is classical.
    This testing program initialize a number of threads, each thread create a socket and 
send UDP packet to UDP server,then wait response by calling select().If select() return 1,
call recvfrom() read the response,else if select() timeout goto next loop...
    If the udp worker's count greater than 18,some thread's select() hang and process
take 100% cpu:
$uname -a
CYGWIN_NT-5.1 zipxing 1.5.5(0.94/3/2) 2003-09-20 16:31 i686 unknown
$gcc -o tsel test_select.c
$./tsel 15 127.0.0.1 7     --create 15 threads communicate with UDP echo service,no problem
$./tsel 20 127.0.0.1 7     --create 20 threads communicate with UDP echo service,100% CPU! 

    I make test_select.c on solaris9 and linux,it seems all ok.
    Specially, the testing program work fine use cygwin1.dll 1.3.22!

Thanks.     

=========================================================================================
test_select.c
=========================================================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

//Select timeout...
int timeout = 5;
//Thread count...
int tcount  = 20;
struct sockaddr_in servaddr;

//Thread function...
void *
udpworker(void *ptr) {
    int  sockfd = 0;
    int  tid;
    char buf[256];
    tid = (int)ptr;
        
    if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
        printf("cannot open socket for udp packet!\n");
        exit(1);
    }
        
    while(1){
        struct timeval    tv;
        fd_set            rfds;
        
        //Send request to UDP server...
        sendto(sockfd, "echo", 4, 0, 
                (struct sockaddr *)&servaddr, sizeof(servaddr));
        
        //Wait UDP server response...
        FD_ZERO(&rfds);
        FD_SET(sockfd, &rfds);
        tv.tv_sec  =  timeout;
        tv.tv_usec = 0;
        if(select(sockfd + 1, &rfds, NULL, NULL, &tv) == 1) {
            recvfrom(sockfd, buf, sizeof(buf), 0, NULL, NULL);
            printf("thread.%d recive a response...%s\n", tid, buf);
        } else {
            printf("thread.%d recvfrom timeout?...\n", tid);
        }
        
        sleep(10);        
    }
}

//Initial udp client thread pool...
void 
thread_pool_init() {
    int i;
    pthread_t thrid;
        
    for(i=0; i<tcount; i++) 
        pthread_create(&thrid, NULL, udpworker, (void *)i);
    printf("%d threads created.\n", tcount);
}

//Main entry...
int 
main(int argc, char *argv[])
{
    if(argc != 4) {
        printf("Usage: tsel <thread_count> <udp_server_ip> <udp_server_port>\n");
        exit(0);
    }
    tcount = atoi(argv[1]);
    servaddr.sin_family = AF_INET;
    servaddr.sin_port   = htons(atoi(argv[3]));
    inet_aton(argv[2], &servaddr.sin_addr);
    
    thread_pool_init();

    while(1)
        sleep(1000);
}
=========================================================================================




--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]