regarding socket & mssage queue

hello ,
I have to write an application in which I had to implement both Socket Comminication and IPC- message queues.
and that process should run in Infinite loop as well I had to continously check and send data through both type of communications...
What should I use to implement it...
I had implemented message queue and established socket communiction......but not getting how to polll and how to continously check data and send data ...
Neither my process should block nor terminate..

So which mechanism or system calls should I use to implement it??????

I would implement a select/poll loop in the main program to multiplex input.

At the start of the program I would use pipe() and fork() to create a child process on the write end of a pipe. This child process would simply do "msgrcv()" and write the resulting message to the write end of the pipe.

The select fd_set for reading would include any sockets and the read end of the pipe, hence allowing us to read from both any filedescriptor and the pipe which is getting fed by the child process.

When it comes to shutdown you use a kill() and waitpid() to clear up the child.

As I told you that I am using select for socket .....
so there will be no need for using different select for socket and message queues.......

No you didn't, you made no mention of using select.

So what are you asking for?

ok.............
I am sorry... I forget to mention it earler..........
I want to ask you that ...........now I will have two read_fds --->>>one from socket another from pipe end .
So whenever there will be data.....through read_fds, select will multiplex between the two.......

You just need the single select statement as you have now, but by spawning the child process to do the reading and converting it into a pipe message you can now effectively select on both tcp sockets and the pipe for the message queue.

I suggest you may want to add a length based messaging protocol so that you can preserve the message boundaries from the message queue. Eg you write on the pipe each dequeued message with a four byte prefix indicating the length of the message. Then when you read from the pipe, you can read four bytes, then know the total length of the single message and then read that.

I am getting one problem i.e my child is terminating after writting once into the pipe ...as porter guided me ..........
moreover I am not able to use select with read pipe descriptor....

guide me some more steps.......

Put debugging code in with fprintf(stderr,....) and perror() so you can see what errors are occuring.

Are you getting EPIPE or SIGPIPE? (Install a signal handler for SIGPIPE in the child process).

Are you reading the read end and writing the write end of the pipe?

yeah that part i s correctly working..........i.e reading and writing to the pipe......

As I told you earlier I had to create a task which has to check whether there is any message in messag e queue to read.....So I created a child process and from child process I am first reading from message queue and then writing it to the pipe....
and then I am using select through parent process to check for a active file descriptor....
I am getting data correctly....but the problem is that I had to terminate my child after it does its task..........
So no further read and writes..........
and if I will not terminate my child then the code.......after read and write operation is executed twice once by parent and one by child.......
So should I had to create child every time I had to read from message queue.......???????????

Certainly not.

How are you marking the length of a message with writing to the pipe? Are you writing the length first, then the bytes, and when it comes to reading you read the length first, then that exact number of bytes?

NO.....currently I am not considering length........because first I am testing for fixed size string.........
how should I make my child not to interfear with parents data.........
because I will not terminate my child then the code.......after read and write operation is executed twice once by parent and one by child.......

how should i use my child process......???? so that it also continously read data from message queue and write to pipe....

Yes. With either fixed length messages or some other form of message length marking.

I am sorry sir........I am unable to understand what you want to say....??????
please tell me in detail.

is it safe to use child process with loops inside it???????

Sure, why not? As long as you are using fork() and not vfork().

The parent should be terminating the child with a signal.

If I had to recieve data through message queue in child process...then its okay....my code is working fine........

what should I do to make make my child to collect data through message queue continously.........
length of data to be recieved and send is fixed.......because my application is like that .....it sends or recieves fixed length data, so no need to check length ........

plz tell me in steps what steps should I follow????

sir, I am modifing my fist line of my previous post.............

If I had to recieve data through message queue in child process only once....then its okay....my code is working fine........

put a while loop round it

while (1)
{
    read msg queue
    write pipe
}

now facing another problem............
Now I am able to send and recieve messages on message queue...........
but if my process goes idle for some tome 10-20 minutes then its blocking and after such duration I am not able to recieve messages which are in message queue bufffer........
what to do sir??????

Is the main process still sitting in select? Ie, waiting for input on the pipe?

Use a debugger and attach to the process to see where it is.

You could also do strace of the process..

strace -ppid