LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   send PID through message queue (https://www.linuxquestions.org/questions/programming-9/send-pid-through-message-queue-455066/)

mariogarcia 06-15-2006 10:56 AM

send PID through message queue
 
hello,
i want to send the pid of a processus through message queue... it's a program that c program that sends information to another program, that will use this pid to send back informations. but the pid sent and received are never the same, and I don't know how I could make it so?
if i have to give you my code; let me know..
thank you

tuxdev 06-15-2006 12:00 PM

AFAIK, message queues are pretty much for pthreads only. Since I think you are using fork(), I am thinking you really want to use a pipe to send data from the child to the parent.

mariogarcia 06-15-2006 02:54 PM

it's communication interprocessus.. i don't know if utilising forks or something like that it could be done... all i need to do it's to send the pid of a process to a 'process that it's always listening and sends replies to the appropriate processus in fonction of the pid,
thank you.
mario

tuxdev 06-15-2006 03:15 PM

I think I feel your usage of babelfish. It is quite confusing.

As for your problem, are doing the standard C syscall fork() to spawn the client process, or are they independently run as separate apps? If you are doing fork(), then use pipe() to create a pair of file descriptors, one for reading and the other for writing. If they are separate apps, like a web browser - web server pair, than you need to use sockets.

Hko 06-15-2006 03:21 PM

Quote:

Originally Posted by mariogarcia
but the pid sent and received are never the same

Do you mean, you send for example "1234", and on the other end of the queue you receive some different number?

Then you have something wrong in your sending and/or receiving code obviously.

Do I understand correctly that you want to send the PID so the receiver knows where to send the reply?

Then I imagine something like sending the PID as the normal 'payload' of the message, and mark the message back with the PID in the "mtype" field of your "struct msgbuf".

But depending on your intentions, I think in your case a TCP- of file system-socket would be more suitable.

Quote:

Originally Posted by tuxdev
AFAIK, message queues are pretty much for pthreads only.

No, that's not the case. IIRC message queue's were invented a considerable number of years before threads.

mariogarcia 06-15-2006 03:41 PM

hello
I do not use babelfish at all, but it's confusing for me, therefore i write it in a way not very clear; please apologize.
I may not use tcp or sockets, I have to use and ipc method, in this case I am using message queues, and i don't have time to try shared memory or other method at this stage so I must stick with it.
I want to do exactly what you said; I want to send the PID of a program c called 'the client' to a program c called 'the server', the server collects the data it has to and then sends the reply.
I hope i made myself clear now? didn't I?
may I show you the code of my 'client and 'server'? i am really stuck and can't do the rest of my code.
thank you

tuxdev 06-15-2006 04:41 PM

It looks like you need sockets to do this kind of IPC properly.

Hko: I am aware that message queues dated before pthreads, and is really what pipes and sockets are. However, I got the impression that POSIX message queues were made for use with pthreads.

mariogarcia 06-15-2006 06:36 PM

the messages are sent from a tty window to another... do you understand? i log on on the linux box, and launch the server.
then i do a alt-f2 log on, then launch a client, then launch alt-f3 log on and launch another client etc, and these consoles have to communicate with each other.
does this require sockets? sockets aren't the concatenation of ip address with port number? just wondering?
and that's not all; I also need some sort of coordination it means that i may not do something if another client is doing the same thing.
to be fully complete; this program it's the management of a golf tournament. a server keeps track of the points and performances of the players aka the clients.
the coordination part means that if a client is playing at a 'hole', no other client may access that hole.
thank you.

tuxdev 06-16-2006 02:10 PM

Just read this tutorial for some explanation of what sockets are and how to use them.
http://beej.us/guide/bgnet/

varun_shrivastava 06-19-2006 04:17 AM

hi mario,


what your client program is doing, is to send client's pid to server and to receive the message sent by server and see whether the client pid and the pid sent by server are same and you r not receiving the same pid at client side. i think this is your problem.

but first you need to send the received pid by server to the client,
your server program is not sending the received pid so how can client determine what is the pid it received from server.

i have sent a mail to you with programs attached.


bye
VARUN

mariogarcia 06-19-2006 07:25 AM

i have to send the pid of the client to the server.. so that the server knows who to reply to since there are going to be several clients connected to the same server and the server needs to know who to address a message.

the other thing i need to know is how to store the struct received, from the client the server receives a struct via msgrcv(); i need to use that struct.. how do can i use it? put it in a table? let's say a cell by client.. ie: client connected : 1 corresponds to tab[1] etc,
i'thank you for the code, i'll work on them.

tuxdev 06-19-2006 10:57 AM

I really think that you should scrap POSIX message passing here because this app looks a lot like the client/server relationship between a web server and a web browser. This kind of program should really be able to run from two separate computers. POSIX message passing can only occur within the same computer, and the server and client must be related through fork() or pthread_create(). The use of this program I see is a caddy with a PDA logging the golfers progress through the game, and the data is sent to the server to prevent collisions, as well as generate up-to-the-minute reports that can be used on TV or the internet.

varun_shrivastava 06-20-2006 10:47 AM

Quote:

Originally Posted by mariogarcia
i have to send the pid of the client to the server.. so that the server knows who to reply to since there are going to be several clients connected to the same server and the server needs to know who to address a message.

the other thing i need to know is how to store the struct received, from the client the server receives a struct via msgrcv(); i need to use that struct.. how do can i use it? put it in a table? let's say a cell by client.. ie: client connected : 1 corresponds to tab[1] etc,
i'thank you for the code, i'll work on them.


i m not able to understand what you mean

please make it clearer

mariogarcia 06-20-2006 03:08 PM

i need a way to modify the data sent through the message queue and send it back to the client that sent it

aral 08-18-2006 09:51 AM

Hko , what do you mean " mark the message back with the PID in the mtype field of struct msgbuf " ? U mean put the PID in the message field of struct msgbuf and also in the type field of msgbuf ? What 's the point of that ?


All times are GMT -5. The time now is 04:30 AM.