# Message queue is not blocked in msgsnd

**URL:** <https://community.unix.com/t/message-queue-is-not-blocked-in-msgsnd/295856>\
**Category:** Programming\
**Created:** [September 22, 2011, 2:04pm UTC](https://community.unix.com/t/message-queue-is-not-blocked-in-msgsnd/295856 "2011-09-22T14:04:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexse](https://community.unix.com/letter_avatar/alexse/32/5_5575768a8748004e209b776fc1b2916d.png) [@alexse](https://community.unix.com/u/alexse)\
**Post date:** [September 22, 2011, 2:04pm UTC](https://community.unix.com/t/message-queue-is-not-blocked-in-msgsnd/295856/1 "2011-09-22T14:04:58Z")

</div>

Hi, I am trying to send/receive data by message queue and expecting it to be blocked on send/read for other side (at least this is my understooding ) , I am connecting message between perl\<-\>C , perl is working as expected , but in C msgsnd and msgrcv are not waiting (blocked) untill second side will read/write .  
Here is a code I am using :

QueueId = msgget(QueueKey, 0777 | IPC\_CREAT | IPC\_EXCL );  
if ((status=msgsnd(QueueId, &MsgBuf, s.length()+1, 0)) == -1) { /\* +1 for '\0' \*/  
fprintf(stderr,"Failing to send a message: \"%s\"...\n",s.c\_str()),  
msgctl(QueueId, IPC\_RMID, NULL);  
exit(-1);  
};  
msgrcv(QueueId, &MsgBuf, sizeof(MsgBuf.mtext), 0, 0);

Appreciate any help

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [September 22, 2011, 2:11pm UTC](https://community.unix.com/t/message-queue-is-not-blocked-in-msgsnd/295856/2 "2011-09-22T14:11:52Z")

</div>

Check the return value of everything and whenever it's not what you expected, perror("something happened");. What's the value of QueueID?

---

<div class="post-metadata">

**Author:** ![alexse](https://community.unix.com/letter_avatar/alexse/32/5_5575768a8748004e209b776fc1b2916d.png) [@alexse](https://community.unix.com/u/alexse)\
**Post date:** [September 22, 2011, 2:18pm UTC](https://community.unix.com/t/message-queue-is-not-blocked-in-msgsnd/295856/3 "2011-09-22T14:18:58Z")

</div>

> [@corona688](#):
>
> Check the return value of everything and whenever it's not what you expected, perror("something happened");. What's the value of QueueID?

QueueID=1409024  
The return statuses are : MSGSND:0 MSGRCV: 16  
I am expecting this process stalled on msgsnd ... Correct ? But it doesn't ...

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [September 22, 2011, 2:22pm UTC](https://community.unix.com/t/message-queue-is-not-blocked-in-msgsnd/295856/4 "2011-09-22T14:22:27Z")

</div>

Well, it's receiving a message. Check the contents, see what it received.

---

<div class="post-metadata">

**Author:** ![alexse](https://community.unix.com/letter_avatar/alexse/32/5_5575768a8748004e209b776fc1b2916d.png) [@alexse](https://community.unix.com/u/alexse)\
**Post date:** [September 22, 2011, 2:27pm UTC](https://community.unix.com/t/message-queue-is-not-blocked-in-msgsnd/295856/5 "2011-09-22T14:27:29Z")

</div>

> [@corona688](#):
>
> Well, it's receiving a message. Check the contents, see what it received.

Let me to describe a problem more precised :  
I am expectin to open the message in other process in perl ,it should get a data from C-\>msgsnd and reply back to C-msgrcv.  
I am expecting from C-\>msgsnd to be blocked untill perl will make msgrcv  
and C-\>msgrcv to be blocked untill perl side will execute Perl-\>msgsnd .  
However I don't run Perl at all and C-\>msgsnd should be stacked forever ...  
However it execute msgsnd and msgread with previously send data without waiting ...

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [September 22, 2011, 2:38pm UTC](https://community.unix.com/t/message-queue-is-not-blocked-in-msgsnd/295856/6 "2011-09-22T14:38:31Z")

</div>

Interesting, now I see why msgrcv can selectively ignore message types.

Have them send and receive different message types, blocking their own.

```nohighlight
msg->mtype=1; // Send a type-1 message.
msgsnd(msqid, msg, msg_size, 0);
msgrcv(msqid, msg, msg_size, 1, MSG_EXCEPT); // Ignore type-1 messages

```

---

<div class="post-metadata">

**Author:** ![alexse](https://community.unix.com/letter_avatar/alexse/32/5_5575768a8748004e209b776fc1b2916d.png) [@alexse](https://community.unix.com/u/alexse)\
**Post date:** [September 22, 2011, 2:44pm UTC](https://community.unix.com/t/message-queue-is-not-blocked-in-msgsnd/295856/7 "2011-09-22T14:44:17Z")

</div>

> [@corona688](#):
>
> Interesting, now I see why msgrcv can selectively ignore message types.
> 
> Have them send and receive different message types, blocking their own.
> 
> ```plaintext
> msg->mtype=1; // Send a type-1 message.
> msgsnd(msqid, msg, msg_size, 0);
> msgrcv(msqid, msg, msg_size, 1, MSG_EXCEPT); // Ignore type-1 messages
> 
> ```

Waw it is working , THANKS A LOT  
(I spend half day on this stupid thing ):rolleyes:
