Sponsored Content
Special Forums IP Networking Can we write a multiple thread to receive from a single socket file descriptor Post 302174401 by sumitpandya on Tuesday 11th of March 2008 03:47:34 AM
Old 03-11-2008
Use AIO or Thread Pool

Multiple threads can definately listen to single socket. All threads can be blocked on "select" or "poll" system-call. Now depending on number of CPU your that many number of threads will come out of select/poll system call then if its TCP you mandatory call "accept" system-call. Here only 1 thread will succeed and remaining threads will fails which again go back to select/poll system-call. Now if instead of TCP you using UDP then same above syncronization will happen at "read" system-call and remaining threads has to go back at select/poll. Pseudo code will look like

while(1) {
select(sockfd);
rs = accept(sockfd);
if(rs == -1) continue;
thread_function();
}

But from past-experience. I'd recommend you to re-design of your software with producer and worker concept. One thread will read data on socket then it will select a thread from pool and assign work to that.
From our statistics I'd say 1GHz CPU can efficiently hangle 25 complex threads only!!! And for multithreads instead of deploying higher capacity of CPU you should go for higher multi-core procerrors only!!!
 

10 More Discussions You Might Find Interesting

1. Programming

Get the file descriptor of a socket file. C vs Python.

Hi, I want to validate that a file is a socket file on Linux. I know I can do this using the S_ISSOCK macro, but I am not sure how to get the file descriptor for the socket file. For example, I know that /tmp/mapping-foo is a socket file. In Python I can do something like this: >>> import... (2 Replies)
Discussion started by: goon12
2 Replies

2. Programming

Finding used socket receive-buffer size

I have set the receive buffer size of socket to max. setsockopt(sd,SOL_SOCKET, SO_RCVBUF,&max,optval); Am reading data from the socket in a loop(say max 100 bytes per recv) while(1) { int rlen=recv(sd,(void *)buf, 100 , 0); //err handle and processing } Assume my process is slow... (2 Replies)
Discussion started by: johnbach
2 Replies

3. Solaris

Killing a thread having a listener socket

Hi All I have a separate thread ListenerThread having a socket listening to info broadcasted by some remote server. This is created in my main thread. Because of requirements, once the separate thread is started I need to avoid any blocking function on the main thread. Once it comes to the... (2 Replies)
Discussion started by: manustone
2 Replies

4. Solaris

Polling on socket descriptor does not return pollhup

I have a query related to the functioning of poll() system call on solaris and linux platforms. When the client is abnormally terminated, it is observed that on Linux the socket is immediately closed and the server gets ECONNREFUSED. But in case of Solaris it is observed that the socket is not... (0 Replies)
Discussion started by: Amarjeet_7
0 Replies

5. Programming

how can I send and receive data in client server socket programing

char name; printf ("Welcome to the server \n"); printf ("Enter user name: \n"); scanf ("%c", &name); how can client send name to server:what should be the code? int send ( int sid , const char ∗buffer Ptr , int len , int f l a g ) how can client receive ack from... (1 Reply)
Discussion started by: saiful_911
1 Replies

6. Programming

write on Non Blocking Socket

How to know whether socket is ready for write. select(maxfds, (fd_set *)NULL, &writefds, NULL, &timeout); By default socket is set for write without checking whether it would block or not? If so how do I know my FD is ready for writing. (3 Replies)
Discussion started by: satish@123
3 Replies

7. Programming

creating multiple threads using single thread id

Hi all, Can I create multiple threads using single thread_id like pthread_t thread_id; pthread_create(&thread_id, NULL, &print_xs, NULL); pthread_create(&thread_id, NULL, &print_ys, NULL); pthread_create(&thread_id, NULL, &print_zs, NULL); pthread_join(thread_id, NULL); what... (2 Replies)
Discussion started by: zing_foru
2 Replies

8. IP Networking

thread blocked while deleting a socket.

Hello to all, I have below function, but the thread will be bloked at the line delete pListenSocket; // Close and delete If I put some cpu timing switching function prior to it, then no blocking pop up. printf("xyz\n"); // or sleep(100); delete pListenSocket; // Close and... (1 Reply)
Discussion started by: shinypro
1 Replies

9. Solaris

18-Mar-2012 14:25:03.209 general: error: socket: file descriptor exceeds limit (4096/4096)

I have BIND 9.8.1-P1 cache only DNS server running in Solaris 10. I have upgraded the same from 9.6.1 to 9.8.1-P1. Now i am facing "file descriptor exceeds limit (4096/4096)" error frequently on the server. Please help me on this issue! (1 Reply)
Discussion started by: sandeep.tk
1 Replies

10. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies
SELRECORD(9)						   BSD Kernel Developer's Manual					      SELRECORD(9)

NAME
seldrain, selrecord, selwakeup -- record and wakeup select requests SYNOPSIS
#include <sys/param.h> #include <sys/selinfo.h> void seldrain(struct selinfo *sip); void selrecord(struct thread *td, struct selinfo *sip); void selwakeup(struct selinfo *sip); DESCRIPTION
seldrain(), selrecord() and selwakeup() are the three central functions used by select(2), poll(2) and the objects that are being selected on. They handle the task of recording which threads are waiting on which objects and the waking of the proper threads when an event of interest occurs on an object. selrecord() records that the calling thread is interested in events related to a given object. If another thread is already waiting on the object a collision will be flagged in sip which will be later dealt with by selwakeup(). selrecord() acquires and releases sellock. selwakeup() is called by the underlying object handling code in order to notify any waiting threads that an event of interest has occurred. If a collision has occurred, selwakeup() will increment nselcoll, and broadcast on the global cv in order to wake all waiting threads so that they can handle it. If the thread waiting on the object is not currently sleeping or the wait channel is not selwait, selwakeup() will clear the TDF_SELECT flag which should be noted by select(2) and poll(2) when they wake up. seldrain() will flush the waiters queue on a specified object before its destruction. The object handling code must ensure that *sip cannot be used once seldrain() has been called. The contents of *sip must be zeroed, such as by softc initialization, before any call to selrecord() or selwakeup(), otherwise a panic may occur. selwakeup() acquires and releases sellock and may acquire and release sched_lock. seldrain() could usually be just a wrapper for selwakeup(), but consumers should not generally rely on this feature. SEE ALSO
poll(2), select(2) AUTHORS
This manual page was written by Chad David <davidc@FreeBSD.org> and Alfred Perlstein <alfred@FreeBSD.org>. BSD
August 25, 2011 BSD
All times are GMT -4. The time now is 08:57 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy