0

As the title says I'm trying to create a C++ program in linux (Ubuntu) which communicates using the msgget function. This program creates multiple processes using the fork() function and I want each process to be able to communicate with each other via message queues.

The problem I have is that I don't know how to pass messages using only one msgget so I don't have to create one with different key for every process. Is there a way where I can for example do a msgsnd with and specific ID to the message queue so only another process with adds this ID in msgrcv can receive only this message and leave the rest in the message queue?

The only way I've manage to communicate between the process is making a different msgget for each process using a different key, but I'd prefer if there was a way where I can do the same with only one msgget.

user4581301
  • 33,082
  • 7
  • 33
  • 54
Diego Esquivel
  • 182
  • 1
  • 12
  • SysV IPC, where msgget comes from, is a somewhat antiquated API that has several design issues. It's fine to play with it for learning or educational purposes, but no serious application should be written with those APIs, which have some basic, fundamental problems. Processes that wish to communicate with each will find it easier to use more traditional means, like named pipes or local sockets. As far as doing what you want to do here, see the description of the `msgtyp` field in the manual pages. – Sam Varshavchik Sep 10 '20 at 20:15
  • Yeah I know some about pipes but right now I am in the process of learning to program with using multiple processes and right now just in the process of using message queues to communicate and monitors with conditional variables with semaphores to control the program. But right now I'm trying to find a way to use one msgget for every process so I don't need to create a bunch of those. I've seen about msgtyp but when I try to implement it it still takes every message that is in the message queue even if they were send with a different msgtyp – Diego Esquivel Sep 10 '20 at 20:19
  • Using `msgtype` should work as documented. If you are having difficulties using it correctly, you'll have to figure out why. Without a [mre], as explained in the [help], it's unlikely that anyone here will be able to tell you anything more. – Sam Varshavchik Sep 10 '20 at 20:35
  • Well what I am trying is I send a msgtype of 2020 and then I receive with 1010 as I understand it, is that the 1010 one isn't supposed to take the 2020 one out of the queue but it does. It doesn't receive the message it had but nonetheless it shouldn't empty the queue if the msgtype is different from the ones in the queue. – Diego Esquivel Sep 10 '20 at 20:58
  • That's not how `msgrcv` is documented to work. Like I said, without a [mre] that anyone can cut/paste ***exactly as shown***, and receive the same results, no further advice is possible. – Sam Varshavchik Sep 10 '20 at 22:00

0 Answers0