Linux : Check if message queue is empty - c

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.

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.

Related

Message queue - No proper response

*I'm using message queues for sending and receiving messages but some parameters in the the structure msqid_ds no giving proper values why it happens?
*Time of last message send=1525240214- why this is showing junk values?
struct mesg_q
{
char msg_txt[100];
long msg_typ;
};
int main()
{
int msgid;
key_t key;
char buffer[100];
struct mesg_q msgq;
struc answerst msqid_ds info;
// key = ftok("/home/yash72/krishna/thread_test/msgq.text", 65);
msgid=msgget((key_t)1234, 0666 | IPC_CREAT);
if(msgid== -1)
{
printf("msgget failed\n");
return -1;
}
while(1)
{
printf("Text Message\n");
fgets(msgq.msg_txt,100,stdin);
if(msgsnd(msgid,&msgq,100,0)==-1)
{
printf("Send failed\n");
return -1;
}
else
{
printf("Message send\n");
}
msgctl(msgid, IPC_STAT, &info);
printf("Time of last message send=%d\n",info.msg_stime);
printf("uid=%d\n",info.msg_perm.uid);
}
}
OUTPUT:
Text Message
qwerty
Message send
Time of last message send=1525240214 // Why this is showing junk?
uid=0
Text Message
Receiver code:
Reason for junk values from this code?
struct mesg_q
{
char msg_txt[100];
long msg_typ;
};
int main()
{
int msgid;
char buffer[100];
long int rec_buff=0;
key_t key;
struct mesg_q msgq;
struct msqid_ds info;
// key = ftok("/home/yash72/krishna/thread_test/msgq.text", 65);
msgid=msgget((key_t)1234, 0666 | IPC_CREAT);
if(msgid == -1)
{
printf("Msgget failed\n");
return -1;
}
while(1)
{
if(msgrcv(msgid,&msgq,100,rec_buff,0)==-1)
{
printf("Mesg recv failed\n");
return -1;
}
else
{
printf("Mesg Recvd\n");
}
printf("Recvd mesg=%s\n",msgq.msg_txt);
msgctl(msgid, IPC_STAT, &info);
printf("Num:of bytes on queue= %d\n", info.msg_cbytes);
printf("num:of messages on queue=%d\n", info.msg_qnum);
printf("Max bytes on queue=%d\n", info.msg_qbytes);
printf("Time of last message recvd=%d\n",info.msg_rtime);
}
}
OUTPUT;
Number of bytes on queue= 0
number of messages on queue=0
Maximum bytes on queue=16384
Time of last message received=1525240214
what is the reason foe this incorrect values from struct msqid_ds ?
First of all you should know why to use message queue IPC, I assume before using message queue, you have gone through FIFO. In FIFO what is condition for the processes to communicate each other ? that both process should be alive while communicating.
To avoid above problem of FIFO you are using message queue, as in MQ you can put data at one time and can read at anytime, for that you have to specify the mtype i.e with what mtype process_1 is sending the data and with what mtype process_2 is receiving the data.
From the manual page of msgsnd
struct msgbuf {
long mtype; /* message type, must be > 0, I'm talking baout this */
char mtext[1]; /* message data */
};
so in both the process(reader & sender) specify the mtype
msgq.msg_typ = 1; /*specify this in both process */
Secondly, Time of last message send=1525240214- why this is showing junk values ? => Its not junk data, its the time in seconds from EPOCH time, use ctime() to print in human readable format.
sender.c
#include<stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include<stdint.h>
struct mesg_q {
char msg_txt[100];
long msg_typ;
};
int main(void) {
int msgid;
struct mesg_q msgq;
struct msqid_ds info;
msgid=msgget((key_t)1234, 0666 | IPC_CREAT);
if(msgid== -1) {
printf("msgget failed\n");
return -1;
}
while(1) {
printf("Text Message\n");
fgets(msgq.msg_txt,sizeof(msgq.msg_txt),stdin);
/* with what msg_type you are sending, you didn't mention ? mention it */
msgq.msg_typ = 1; /* I am sending with msg_type 1 */
if(msgsnd(msgid,&msgq,sizeof(msgq),msgq.msg_typ)==-1) {
printf("Send failed\n");
return -1;
}
else {
printf("Message send\n");
}
msgctl(msgid, MSG_STAT, &info);
printf("Time of last message send = %jd\n",(intmax_t)info.msg_stime);/* use the correct format specifier */
/* what ever time info.msg_stime printing, its not junk, its seconds from EPOCH, use ctime() to print in readable format */
printf("uid = %d\n",info.msg_perm.uid);
}
return 0;
}
receiver.c
struct mesg_q {
char msg_txt[100];
long msg_typ;
};
int main(void ){
int msgid;
long int rec_buff=0;
struct mesg_q msgq;
struct msqid_ds info;
msgid=msgget((key_t)1234, 0666 | IPC_CREAT);
if(msgid == -1) {
printf("Msgget failed\n");
return -1;
}
while(1) {
/* with what msg_typ you are reciving, you have to mention ? u didn't */
msgq.msg_typ = 1;
if(msgrcv(msgid,&msgq,sizeof(msgq),rec_buff, msgq.msg_typ)==-1) {
printf("Mesg recv failed\n");
return -1;
}
else{
printf("Mesg Recvd\n");
}
printf("Recvd mesg=%s\n",msgq.msg_txt);
msgctl(msgid, MSG_STAT, &info);
/* enable compiler warning, and use correct format specifier */
printf("Num:of bytes on queue= %u\n", (int)info.msg_cbytes);
printf("num:of messages on queue=%d\n", (int)info.msg_qnum);
printf("Max bytes on queue=%d\n", (int)info.msg_qbytes);
printf("Time of last message recvd=%jd\n",(intmax_t)info.msg_rtime);
}
return 0;
}

Can't delete old messages in message queue C

I have created a msq to let two processes communicate to one another. The problem is that I'm having some issues since after a couple of time that I run my code some of the old messages of the previous executions show up. This bothers me because I need to perform controls on the current data and cant do it because of this. I tried irpcm -q msqid but it does not do anything except shutting down my program the next time I run it. I even tried hardcoding some keys thinking that it would help but nothing. Also tried to msgctl(msqid, IPC_RMID, 0) after I finished using the queue but nothing. Hope you can help me out and thanks in advance.
Here is my code:
sender.c
#define MAXSIZE 1024
struct msgbuf
{
long mtype;
char mtext[MAXSIZE];
};
void die(char *s)
{
perror(s);
exit(1);
}
int msqid1;
int msgflg = IPC_CREAT | 0666;
key_t keymq1;
struct msgbuf sbuf;
size_t buflen;
keymq1 = 668;
sbuf.mtype = 1;
if ((msqid1 = msgget(keymq1, msgflg )) < 0)
die("msgget");
sbuf.mtype = 1;
strcpy(sbuf.mtext,"my message");
buflen = strlen(sbuf.mtext) + 1 ;
if (msgsnd(msqid1, &sbuf, buflen, IPC_NOWAIT) < 0)
{
printf ("%d, %ld, %s, %zu\n", msqid1, sbuf.mtype, sbuf.mtext, buflen);
die("msgsnd");
}
else {
printf("Message sent\n");
}
receiver.c
#define MAXSIZE 1024
struct msgbuf
{
long mtype;
char mtext[MAXSIZE];
};
void die(char *s)
{
perror(s);
exit(1);
}
int msqid;
key_t keymq1;
struct msgbuf rcvbuffer;
keymq1 = 668;
if ((msqid = msgget(keymq1, 0666)) < 0)
die("msgget()");
if (msgrcv(msqid, &rcvbuffer, MAXSIZE, 1, 0) < 0)
die("msgrcv");
printf("Message received: %s\n", rcvbuffer.mtext);
To delete message queue with message queue Id use
ipcrm -q id
or delete the message queue using key value as
ipcrm -Q key_num
From man page "In order to delete such objects, you must be superuser, or the cre‐
ator or owner of the object."
Finally you can delete the message queue using IPC_RMID flag in msgctl() call as
main()
{
int total_mq,i,msqid;
struct msqid_ds ds; //dataStructure holding complete info for indexed message que
struct msginfo msginfo; //general buff copying data from MSG_INFO, has info of how many message que present right now
/* Obtain size of kernel 'entries' array */
total_mq = msgctl(0, MSG_INFO, (struct msqid_ds *) &msginfo); //copy kernel MSGQ_INFO to local buff
//returns count of active message Q
if (total_mq < 0)
{
perror("msgctl");
return 0;
}
printf("no of active message queue(KEY) : %d\n", total_mq+1);
/* Retrieve meaasge Queue id's */
for (i = 0; i <= total_mq; i++)
{
msqid = msgctl(i, MSG_STAT, &ds); //from each index using MSG_STAT -> ds, return msgqid
if (msqid <0 )
{
perror("msgctl");
return 0;
}
/* using msgqid remove the message queue */
if ( msgctl(msqid,IPC_RMID,0) < 0 )
{
perror("msgctl");
return 0;
}
}
printf("all message queues removed\n");
return 0;
}
before running above code, create some message queues and then delete those.

Is this how message queues are supposed to work?

I am trying to understand message queues. In the examples I saw, the msg strunct would have only one more attribute except of the first one (the type) which must be long. So, it would be something like struct msg{long mtype; char text[100]};.
I tried to add a new int attribute, x to see if I recieve both the text and the number and it worked.
Is this how message queues are supposed to work? Can I have as many attributes as I want in my struct?
And, also, is it ok to call the msgrcv and msgsnd functions with the length parameter set to sizeof(send) - sizeof(send.x) because I know that the sizeof a struct isn't always the same as the sum of the sizeof of each attribute?
Thank you.
int main(){
struct msg{
long mtype;
char text[100];
int x;
};
int key = ftok(".", 10);
int qid = msgget(key, 0666|IPC_CREAT);
int pid = fork();
if(pid == 0){
struct msg send;
send.mtype = 1;
strcpy(send.text, "hello");
send.x = 99;
if(msgsnd(qid, (void*)&send, sizeof(send) - sizeof(send.x), 0)<0){
printf("Error child: ");
}
}
else{
struct msg recieve;
if(msgrcv(qid, (void*)&recieve, sizeof(recieve) - sizeof(recieve.x), 1, 0)<0){
perror("Error parent: ");
};
printf("text: %s\nnumber: %d", recieve.text, recieve.x);
}
return 0;
}
From the man page, in:
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
msgp is defined as:
The msgp argument is a pointer to a caller-defined structure of the
following general form:
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[1]; /* message data */
};
Bold is mine
The major point here being the struct is caller-defined. So as long as the input struct (sent by msgsnd) and output struct (received by msgrcv) are the same, the data following mtype can be anything you want (as long as you specify the size correctly). For your case, you really only need:
msgsnd(qid, (void*)&send, sizeof(send) - sizeof(send.mtype), 0)
and
msgrcv(qid, (void*)&recieve, sizeof(recieve) - sizeof(send.mtype), 1, 0)
The char[] is just a placeholder, you can have whatever you want in the structure after the required long mtype field. The size on the msgsnd() call does NOT include mtype.
You almost had it correct.
Here is a working version:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int main(void){
struct msg {
long mtype;
char text[100];
int x;
};
size_t sz = sizeof(struct msg) - sizeof(long); <=== /* SIZE */
int key = ftok(".", 10);
int qid = msgget(key, 0666|IPC_CREAT);
int pid = fork();
if (pid == 0){
struct msg send;
send.mtype = 1;
strcpy(send.text, "hello");
send.x = 99;
if (msgsnd(qid, (void*)&send, sz, 0)<0){
perror("Error child: ");
}
} else {
struct msg recieve;
if(msgrcv(qid, (void*)&recieve, sz, 1, 0)<0){
perror("Error parent: ");
};
printf("text: %s\nnumber: %d\n", recieve.text, recieve.x);
}
return 0;
}
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
Since the msgp parameter is declared as const void*, you can use whatever data type you want. There is nothing that says it has to be a struct with just a long and a char[]. This means you can just do sizeof(send). You do not need to adjust for the extra struct member that you are sending. In fact, doing so will cause problems because the entire struct will not be handled. The only thing that matters is that msgrcv() uses the same struct as the previous msgsnd(). See this example.

System V Message Queues - getting message that already exists

I've been working on a project and one of the tasks that I have to do is passing the string received from another process through a pipe to yet another process but this time I have to use a message queue.
I've managed to learn how msgqueue works and made a simple working program but, the thing is, it works when receiving a string from stdin through fgets.
My question is:
Can I pass a string that is already saved in other variable (for example
char s[20] = "message test"; ) to the msgqueues mtext?
My simple program looks like that:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <errno.h>
struct msgbuf {
long mtype;
char string[20];
};
struct msgbuf mbuf;
int open_queue( key_t keyval ) {
int qid;
if((qid = msgget( keyval, IPC_CREAT | 0660 )) == -1)
return(-1);
return(qid);
}
int send_message( int qid){
int result, size;
size = sizeof mbuf.string;
if((result = msgsnd( qid, &mbuf, size, 0)) == -1)
return(-1);
return(result);
}
int remove_queue( int qid ){
if( msgctl( qid, IPC_RMID, 0) == -1)
return(-1);
return(0);
}
int read_message( int qid, long type){
int result, size;
size = sizeof mbuf.string;
if((result = msgrcv( qid, &mbuf, size, type, 0)) == -1)
return(-1);
return(result);
}
int main(void){
int qid;
key_t msgkey;
msgkey = ftok(".", 'm');
if(( qid = open_queue( msgkey)) == -1) {
perror("openErr");
exit(1);
}
mbuf.mtype = 1;
fgets(mbuf.string, sizeof mbuf.string, stdin);
if((send_message( qid)) == -1) {
perror("sendErr");
exit(1);
}
mbuf.mtype = 1;
if((read_message(qid, mbuf.mtype))== -1){
perror("recERR");
exit(1);
}
printf("Queue: %s\n", mbuf.string);
remove_queue(qid);
return 0;
}
Your code uses fgets() to fill the buffer mbuf.string with input read from stdin. You can instead use something like strcpy(mbuf.string, "message test") where you can pass in a variable or use a hard coded string.
I recommend using the POSIX message queue API as the System V API is deprecated.

Why I dont see a msg from message queue?

I would like to understand how message queues in Unix work. I wrote a simple code which sends a short message to queue and then I can read that message. But my code shows :
And I dont know why - and I cant see a message I send to queue. Heres my code:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
struct mymsgbuf {
long mtype;
char mtext[1024];
}msg;
int send_message(int qid, struct mymsgbuf *buffer )
{
int result = -1, length = 0;
length = sizeof(struct mymsgbuf) - sizeof(long);
if((result = msgsnd(qid, buffer, length, 0)) == -1)
return -1;
return result;
}
int read_message(int qid, long type, struct mymsgbuf *buffer)
{
int result, length;
length = sizeof(struct mymsgbuf) - sizeof(long);
if((result = msgrcv(qid, buffer, length, type, 0)) == -1)
return -1;
printf("Type: %ld Text: %s\n", buffer->mtype, buffer->mtext);
return result;
}
int main(int argc, char **argv)
{
int buffsize = 1024;
int qid = msgget(ftok(".", 0), IPC_CREAT | O_EXCL);
if (qid == -1)
{
perror("msgget");
exit(1);
}
msg.mtype = 1;
strcpy(msg.mtext, "my simple msg");
if((send_message(qid, &msg)) == -1)
{
perror("msgsnd");
exit(1);
}
if((read_message(qid, 1, &msg) == -1))
{
perror("msgrcv");
exit(1);
}
return 0;
}
When I changed a line with msgget for this line:
int qid = msgget(ftok(".", 0), IPC_CREAT | O_EXCL | 0600);
it shows:
From the documentation for msgget:
The low-order 9 bits of msg_perm.mode shall be set equal to the low-order 9 bits of msgflg.
You need to add some permissions to your queue, at least read and write. Do something like:
int qid = msgget(ftok(".", 0), IPC_CREAT | O_EXCL | 0600);

Resources