# Multiplexing socket and message queue using Select()

**URL:** https://community.unix.com/t/multiplexing-socket-and-message-queue-using-select/218325
**Category:** Programming
**Created:** [October 29, 2008, 12:04am UTC](https://community.unix.com/t/multiplexing-socket-and-message-queue-using-select/218325 "2008-10-29T00:04:44Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![shaurya.rastogi](https://community.unix.com/letter_avatar/shaurya.rastogi/32/5_5575768a8748004e209b776fc1b2916d.png) [@shaurya.rastogi](https://community.unix.com/u/shaurya.rastogi)
#### Post date: [October 29, 2008, 12:04am UTC](https://community.unix.com/t/multiplexing-socket-and-message-queue-using-select/218325/1 "2008-10-29T00:04:44Z")

</div>

I have a socket and a message queue over which i am trying to multiplex input using select().

When data comes over socket the select works but when it comes over message queue the select is not detecting it .

Create\_Q gets the identifier of the messege queue.

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*CODE\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/

fd\_set readfds;  
struct msgbuf msg;  
int msgPipe;

msgPipe=Create\_Q("/tmp/Key",'a');  
printf("Message queue id: %d \n",msgPipe);

FD\_ZERO(&readfds);  
FD\_SET(msgPipe,&readfds);  
FD\_SET(hServerSocket,&readfds);

select(msgPipe+1, &readfds, NULL, NULL,NULL);

if (FD\_ISSET(hServerSocket, &readfds))  
printf("Data on socket\n");  
else  
printf("Data on Queue\n");

---

<div class="post-metadata">

### Author: ![geekosaur](https://community.unix.com/letter_avatar/geekosaur/32/5_5575768a8748004e209b776fc1b2916d.png) [@geekosaur](https://community.unix.com/u/geekosaur)
#### Post date: [October 29, 2008, 4:21am UTC](https://community.unix.com/t/multiplexing-socket-and-message-queue-using-select/218325/2 "2008-10-29T04:21:23Z")

</div>

Message queues don't work like file descriptors, and can't be used with [tt]select()[/tt].

The usual way around this is to fork a subprocess which waits on the message queue and writes the data down a pipe to the main process, which can be [tt]select()[/tt]-ed.
