message queues and multi-process

Hi,

Am supposed to use message queues to send and receive messages between the processes. when i was working on that i realised that the message qid and the message queue related data should be maintained in a shared memory so that it can be accessed by all the processes. Could anybody refer some link which helps me out in this...

P.S. i have gone through Richard Stevens IPC volume 2 but i couldn't exactly find such a scenario..Stevens more on memory mapped I/O..

Thanks,

Am using Posix message queues

You don't need to do that. You generate a msqid from a key, which you can even hardcode in your programs or you can keep the key in a file that your programs can access.

I agree with blowtorch.

You don't need both message queues and shared memory.

What is the application?

Hi blowtorch n Neo
Thank you for the reply...

typedef struct cond
{
/* framework mutex structure for condition variable */
mutex_rs * mutexp ;
pthread_cond_t cond;
} cond_var;

typedef struct mutex_r
{
pthread_mutex_t mutex;
} mutex_rs;

typedef struct msg_q
{
int max_num_of_msgs;
int msg_size;
char name[MSGQ_NAME_SIZE];
cond_var rcv_cond_var; /*condition variable */
cond_var snd_cond_var; /send condition variable/
mutex_rs rcv_mutex; ; /Mutex to block for receive/
mutex_rs snd_mutex; /Mutex to block for send/
int msgq_curr_msgs;
}msg_q

P.S. name[MSGQ_NAME_SIZE]; field will be useful to while mq_unlik();

cond_var rcv_cond_var; /*condition variable */
cond_var snd_cond_var; /send condition variable/
mutex_rs rcv_mutex; ; /Mutex to block for receive/
mutex_rs snd_mutex; /Mutex to block for send/

Condition variables am planning to use to wait for specific time interval on read n write for certain conditions like message queue empty ( for read ) or message queue full (for write).... mutex am using to lock and unlock while accessing the condition variables.

so according to blowtorch if i have to maintain the common data for the queue in the file then i have to write the msg_q details in file and whichever process has to access the message queue has to first open the file and read the details and can act on further.....
sounds good...

my doubt but why not shared memory???? is it complicated compared to files??

Thanks

Let me ask you this question. How will your programs know the shmid to attach? You will share this information among the programs in some manner anyway right? Instead, why don't you share the msqid in the same way?

This will make the SHM redundant...

Hi blowtorch,

Thanks a lot..
I agree with you... when it is only message queue and message queue details we can manage with file...
here in this scenario as i have given the structures of mutex and condition variables too.... and i donno exactly what details of pthread_mutex_t should be stored in file while we are locking and unlocking the mutex, the same applies for condition variable...
could you pls.. clarify this to me..

FYI
typedef union
{
struct
{
int __lock;
unsigned int __count;
int __owner;
/* KIND must stay at this position in the structure to maintain
binary compatibility. */
int __kind;
unsigned int __nusers;
int __spins;
} __data;
char __size[__SIZEOF_PTHREAD_MUTEX_T];
long int __align;
} pthread_mutex_t;

thanks,

Aaargh, no, stop.

pthread_mutex_t and pthread_cond_t are private structures, dont start messing around with their internals.

Also, the pthread types are *not* designed to be (a) persistent (b) shared between processes.

Hi porter,

If pthread_mutex or pthread_cond cannot be used across process then what is the use of the following.......

pthread_mutexattr_setpshared(&mutatr, PTHREAD_PROCESS_SHARED)

pthread_condattr_setpshared -
PTHREAD_PROCESS_SHARED (interprocess)
or PTHREAD_PROCESS_PRIVATE (intraprocess)

Thanks

Okay, yes, you can if you have support for it.

hi guys,

I was complicating by using mutex which is not needed at all..
as message queue read and write are atomic...

Thanks