Linux - KernelThis forum is for all discussion relating to the Linux kernel.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 ?
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 */
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.