The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
any way to close socket pa.chidhambaram UNIX and Linux Applications 3 02-01-2008 07:12 AM
Close Socket at HP-UX Renato Gregio HP-UX 2 08-14-2007 07:04 AM
socket close hangs and CPU go to 100% arico HP-UX 0 08-22-2006 09:35 AM
Suse LINUX hassan2 Linux 1 06-24-2004 06:38 AM
C Prog to close a socket in established state teledelux High Level Programming 1 10-03-2001 08:42 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 02-05-2008
Registered User
 

Join Date: Dec 2007
Location: Virginia, USA.
Posts: 250
socket close() -w- pthreads linux 2.6.18.2-34 (suse) SMP

Interesting issue. There was some discussion on the LKML last year regarding the potential problems in concurrent applications reusing file descriptors in various scenarios. The main issue is that the reuse of a file descriptor and reception of data in a threaded application can be confused pretty easily.

Alan Cox suggested using shutdown() before close() to deal with one of the most glaring reuse problems. This doesn't seem to work 100% either.

If I have code like this:
Code:
extern void *handleclient(void *arg) {
int rd = -1;
long mcount = 0;
VHNDL in;
char buf[BSZ];
                         pthread_detach(pthread_self());       
                         pthread_mutex_lock(&socketstable);  
                         memcpy(&in,arg,sizeof(in));
                         pthread_mutex_unlock(&socketstable);
                         while ( (rd = read(in.c_sock,buf,BSZ)) > 0) {
                                 printf("TID %d: Read message %d from peer at %s = %s\n",pthread_self(),mcount++,inet_ntoa(in.peer.sin_addr),buf);
                                 bzero(buf,BSZ);
                         }
                         /*pthread_mutex_lock(&socketstable);*/
                         shutdown(in.c_sock,SHUT_RDWR);
                         close(in.c_sock);
                        /*pthread_mutex_unlock(&socketstable);*/
                         pthread_exit(NULL);
}
The reuse of file descriptors still causes EBADF in some cases. The parent process is simple in an accept() loop creating handler threads till MAXTHREADS then recycling. As you see the handler does nothing but read till error and then exits.
I'm still getting EBADF on some closes and as a result having incrementing numbers of close-wait connects.

Any ideas?
Reply With Quote
Forum Sponsor
  #2  
Old 02-06-2008
Registered User
 

Join Date: Dec 2007
Location: Virginia, USA.
Posts: 250
Never mind. Logic error. Was inadvertently reusing a thread.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:26 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0