Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux : Check if message queue is empty

i want to know if a queue message is empty or not . i have used msg_ctl() as follows it doesn't work :

struct msqid_ds buf;
int num_messages;

rc = msgctl(msqid, IPC_STAT, &buf);

and i've used this peek function :

int peek_message( int qid, long type )
{
    int result, length;
    if((result = msgrcv( qid, NULL, 0, type, IPC_NOWAIT)) == -1) {
        if(errno==E2BIG)
            return(1);
    }

    return(0);
}

in both cases i get the same result before and after sending a message to the queue.

the message gets to the queue successfully , i've tested that with reading what i've sent.

like image 342
Marwan Tushyeh Avatar asked Oct 09 '12 13:10

Marwan Tushyeh


People also ask

How do I see message queue in Linux?

First of all, you want to connect to a queue, or create it if it doesn't exist. The call to accomplish this is the msgget() system call: int msgget(key_t key, int msgflg); msgget() returns the message queue ID on success, or -1 on failure (and it sets errno, of course.)

How do I clear the message queue in Linux?

-q msgid removes the message queue identified by msgid. -S semkey removes the semaphore created with semkey. -s semid removes the semaphore identified by semid. The details of the removes are described in msgctl(2), shmctl(2), and semctl(2).

Is Msgrcv blocked?

The msgsnd(2) and msgrcv(2) functions can be performed as either blocking or non-blocking operations. A blocked message operation remains suspended until one of the following three conditions occurs: The call succeeds. The process receives a signal.

What is Mqueue in Linux?

The <mqueue. h> header shall define the mq_attr structure, which is used in getting and setting the attributes of a message queue. Attributes are initially set when the message queue is created. An mq_attr structure shall have at least the following fields: long mq_flags Message queue flags.


1 Answers

I wrote the sample code that does seem to work properly:

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <errno.h>

struct msgbuf {
    long mtype;       /* message type, must be > 0 */
    char mtext[1];    /* message data */
};

int main(void) {
    int msqid;
    //msqid = msgget(IPC_PRIVATE, (IPC_CREAT | IPC_EXCL | 0600));
    msqid = msgget((key_t)1235, 0600 | IPC_CREAT);

    printf("Using message queue %d\n", msqid);
    struct msqid_ds buf;

    int rc = msgctl(msqid, IPC_STAT, &buf);

    uint msg = (uint)(buf.msg_qnum);
    printf("# messages before post: %u\n", msg);

    printf("Posting message to queue...\n");
    struct msgbuf qmsg;
    qmsg.mtype = 100;
    qmsg.mtext[0] = 'T';

    int res = msgsnd(msqid, &qmsg, 1, MSG_NOERROR);

    rc = msgctl(msqid, IPC_STAT, &buf);

    msg = (uint)(buf.msg_qnum);
    printf("# messages after post: %u\n", msg);

    return 0;
}

Maybe that will be helpful to you? The number of messages on the queue does seem to increment correctly when using this code.

like image 149
ashgromnies Avatar answered Sep 22 '22 11:09

ashgromnies