How to Send Integer with Message Queue with Posix API in Linux

How to send integer with message queue with POSIX API in linux?

Do not read the char* in this case as the only allowed datatype.

Many *ix API use char as a generic buffer pointer.

View the interface therefore as taking a pointer to buffer and the size of the buffer.

That buffer can be anything you like, from a single int, to a struct, seralized string representation of your class, or just about anything else in memory.

int i;
mq_send(mq, (char *) &i, sizeof(i), 0);

Should work (not tested)

Good Luck

Can't send integer from one process to another using POSIX in C

From the POSIX documentation for msgsnd:

The application shall ensure that the argument msgp points to a user-defined buffer that contains first a field of type long specifying the type of the message, and then a data portion that holds the data bytes of the message. The structure below is an example of what this user-defined buffer might look like:

struct mymsg {
long mtype; /* Message type. */
char mtext[1]; /* Message text. */
}

You're not following that, so your code can't work. Add an long field at the start of your structure, and set it to 2 since that's what your receiver is expecting as a message type.

Also move that structure definition to a header file so that you can share it between your two pieces of code rather than duplicate it.

 if(msgqid=msgget(MsgKey, 0600 | IPC_CREAT)<0)

Add parens around the assignment (in both pieces of code):

 if((msgqid=msgget(MsgKey, 0600 | IPC_CREAT)) < 0)

< and the other comparison operators have higher precedence than the assignment operator, so your code is incorrect without the parens - it assigns the result of the comparison to msgqid.

And finally, main returns an int, never void.

Sending int with mq_send

The problem was that I never did mq_unlink.

Linux programming. How to send array with message queue?

Your code invokes undefined behavior.

Since you want to send integer-by-integer, you should use sizeof(message.a[i]), what is size of one element, instead of sizeof(message.a), which is size of the whole array a.

msgsnd(msgid, (int *) &message.a[i], sizeof(message.a), 0);
^^^^^^^^^

In short it means you are passing some garbage behind the array.

Using Linux POSIX IPC message queue

If I understand the scenario correctly, then it sounds like you will need multiple message queues. Trying to use a single message queue for two-way communication between multiple processes would turn into a complex situation. Without the ability to peek at a message, it would even be difficult for the server process to send a specific message to a specific client.

  • Have one message queue that is a generic one used by a client to establish a "private" message queue. A client that wants to open a communication channel with the server could send a message to the server on this queue. Perhaps have the client send the name (e.g., using process id maybe) of the queue for the server to open.
  • The server can then open a new message queue specifically for that client for two-way communication (or depending on the usage, it might make sense to open two queues, one for each direction between the client and server).

How to peek into a Linux (POSIX) message queue without removing an item?

Peeking is probably a bad idea for a message queue because, like sehe noted, the danger of race conditions. Just assume you have peeked a message; since you cannot lock the queue, you will be unable to reliably retrieve the same message you have peeked. If you have two processes receiving mutually exclusive messages from the same queue, you should be thinking about separating these messages into two queues, for clarity of design and race condition stability.

Answer made short: A peek is very unlikely because it would need explicit locking semantics to carry it out stably.

How POSIX Message Queue names used in makefile

Your makefile makes no sense to me.
-o specifies the name of the compiled output file.
If your source file is called iserv.c and you want your executable to be named serverqueue, compile

gcc -o serverqueue iserv.c -lrt

In your makefile you forgot the .c extension of the source file.

I don't understand the reason why you included infile.txt.

EDIT according to your comment:

In homework , it is wanted that server will be started as follows: iserv . servq is the name of a message queueto be created by the server and it is created as server queue in the code if I am true besides that inputfile is the name of a text file that will contain a large number of positive integers.

No, the statement iserv <servq> <inputfile> has nothing to do with the compilation. This is how the program should be used once the program is compiled. The name of the program is iserv, thus you have to compile your code like this

gcc -o iserv iserv.c -lrt

To start your program type

./iserv <servq> <inputfile>

where <servq> is your serverqueue and <inputfile> would be infile.txt.

How to handle signals in Linux Message Queues (POSIX or SysV) with different sizes?

Sorry, complete rewrite, I confused the msgrcv with reading from pipes and sockets, where each packet can be read in part or as a whole.

You need to have a receiving buffer that is large enough for the largest message of all that you may receive.

The actual size is returned as the return value from msgrcv, so something like this:

 struct sig2 msg;
size_t n = msgrcv(queue_id, &msg, sizeof(msg.data), -1, 0);
if (n == -1)
{
... error - inspect errno to understand what went wrong ...
}

Note also that the default size is 16384 bytes, so a 20000 byte array may not work unless you reconfigure the default message size.



Related Topics



Leave a reply



Submit