LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 08-10-2006, 05:22 AM   #1
sayarsoft
LQ Newbie
 
Registered: Aug 2006
Posts: 6

Rep: Reputation: 0
Thumbs down pthreads and recv


Hi everbody,

I am developing a multithreaded app. Think that one of my threads is connected to a device via tcp/ip and blocked at recv() call. Since i am using blocking socket.Another thread pings the destination of first thread to check whether connection is alive. If the connection is not alive, i am sending signal with pthread_kill. Although signal is handled, the blocked thread doesn't return from recv() call.

I also tried closing the socket instead of sending signal. Again nothing happened. Lastly I tried "select" giving timeout.Again unsuccessfull.

As a result, here is the question :

Using nptl, if one of thread is blocked at a function call, how can we unblock that ?

TIA
 
Old 08-10-2006, 03:57 PM   #2
Denes
Member
 
Registered: Mar 2004
Distribution: CentOS 4.3/4.5
Posts: 72

Rep: Reputation: 15
Interesting, pthread_kill with SIGIO should unblock the recv call on that thread assuming you pass the correct thread ID. A close call in a different thread on the same handle should also work. Additionally, a select loop with something like the code below should also work. I have tried select code like this with a socket set to O_NONBLOCK on RH9 and RHEL4 U2 with no problems. What distribution/version are you using? What happens if you use the select code with O_NONBLOCK?

while (!gQuitReceiving) {

/* Copy global read set */
ReadSet = gReadSet;

/* Default timeout for select call is one second
This must be reset each loop */
Timeout.tv_sec = 1;
Timeout.tv_usec = 0;

/* Block on read set (this returns -1 on error but
just check individual FileDescriptors for error) */
if ((SelectCount = select(gMaxSocket, &ReadSet, NULL, NULL,
&Timeout)) == -1) {
perror("Error select");
continue;
}
/* Check if something is available */
if (!SelectCount) {
continue;
}
/* Loop through file descriptors, start from 0 to
ignore values not set */
for (FileDescriptor = 0;
FileDescriptor < gMaxSocket;
FileDescriptor++) {
/* Something came in on one of the
descriptors we are checking out */
if (FD_ISSET(FileDescriptor, &ReadSet)) {
/* call recv here */
 
Old 08-11-2006, 04:06 AM   #3
sayarsoft
LQ Newbie
 
Registered: Aug 2006
Posts: 6

Original Poster
Rep: Reputation: 0
Hi Denes,

I tried 3 different approach. Two of them is still unsuccesful:

1) Sending SIGIO with pthread_kill. (I checked thread ID.It is right.Does socket needs special flags ? Can be related to pthread library ?)
2) Closing socket from another thread.

Third one,using select, was also unsuccesful with my implementation. But when I looked at your sample code, I noticed that you initalize readset in every loop. I was not doing that. With that initialization in each loop, at last it worked. Magic is "ReadSet = gReadSet" line.

I am using Suse with 2.6.4-52-default kernel. NPTL version is 0.61 (getconf GNU_LIBPTHREAD_VERSION).

Thanks
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
recv() function of SOCKET ashucool83 Programming 1 10-08-2005 07:38 PM
Can't recv() Ephracis Programming 2 01-04-2005 02:49 PM
function recv() in socket husniteja Programming 1 08-18-2004 09:06 AM
How do I add a timeout for the recv() cmd? dravya Programming 6 07-14-2004 09:28 AM
recv() buffer linuxanswer Programming 1 03-22-2004 12:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 09:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration