5

What happens when a process reads an IPC message with msgrcv?

Why I can't read a message with the same mtype more than once?

Code for the structure being used:

struct msgbuff{
    long mtype;
    char mtext[150];   
};
JoshDM
  • 4,939
  • 7
  • 43
  • 72
DNA
  • 103
  • 1
  • 1
  • 7

1 Answers1

2

A message can only be read once. This is how the msgrcv manpage describes its function:

The msgrcv() system call removes a message from the queue specified by msqid and places it in the buffer pointed to by msgp.

There is no way to peek at the queue. If you need that you could pop an item from the queue with msgrcv() and then add it again using msgsnd(). There is a risk that will fail (queue full, out of memory, etc.) so it is not foolproof.

Wichert Akkerman
  • 4,918
  • 2
  • 23
  • 30
  • Mhm thank you Wichert Akkerman and Satish. Can I send a second message with the same mtype ? – DNA Jan 14 '13 at 16:15
  • 1
    Certainly, there are no restrictions on how often a type is used. The type is only used to allow you to select which messages you want to receive. – Wichert Akkerman Jan 14 '13 at 16:16