Linux message queue programming example

  
 

Foreword:

A message queue is a linked list of messages. You can think of a message as a record with a specific format and a specific priority. A process that has write access to the message queue can add new messages to it according to certain rules; a process that has read permission to the message queue can read the message from the message queue.

Function:

1. Create a new message queue or get an existing message queue

Prototype:
int msgget(key_t key, int msgflg);

Parameter:

key: can be considered as a The port number can also be generated by the function ftok.

msgflg: IPC_CREAT value, if there is no such queue, create one and return a new identifier; if it already exists, return the original identifier.

IPC_EXCL value, -1 if there is no such queue, or 0 if it already exists.

2.Read/write messages to the queue

Prototype:

msgrcv fetch messages from the queue:
ssize_t msgrcv(int msqid, void *msgp, size_t Msgsz, long msgtyp, int msgflg);

msgsnd puts the data in the message queue:
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);

Parameter:

msqid: ID of the message queue

msgp: Pointer to the message buffer. This location is used to temporarily store the messages sent and received. It is a user-definable general structure, in the following form: Br>struct msgstru{ long mtype; //greater than 0 char mtext[512];};


msgsz: the size of the message.

msgtyp: The form of the message read from the message queue. If the value is zero, it means that all messages in the message queue will be read.

msgflg: Used to indicate the actions that the core program should take if there is no data in the queue. If msgflg and the constant IPC_NOWAIT are used together, if the message queue is full during msgsnd() execution, msgsnd() will not block, but will return -1 immediately. If msgrcv() is executed, the message queue is empty. When you do not wait, immediately return -1 and set the error code to ENOMSG. When msgflg is 0, msgsnd() and msgrcv() take the blocking wait mode when the queue is full or empty.

3.Set Message Queue Properties

Prototype:
int msgctl ( int msgqid, int cmd, struct msqid_ds *buf );

Parameter: msgctl system call identified by msgqid The message queue performs the cmd operation. The system defines three cmd operations: IPC_STAT, IPC_SET, IPC_RMIDIPC_STAT: This command is used to obtain the msqid_ds data structure corresponding to the message queue and save it to the address space specified by buf. IPC_SET : This command is used to set the properties of the message queue. The attributes to be set are stored in buf.

IPC_RMID : Remove the message queue identified by msqid from the kernel.

Instance:

Message sender: send.c
/*send.c*/#include <stdio.h>#include <sys/types.h># Include <sys/ipc.h>#include <sys/msg.h>#include <errno.h>#define MSGKEY 1024struct msgstru{long msgtype;char msgtext[2048];};main(){struct msgstru Msgs;int msg_type;char str[256];int ret_value;int msqid;msqid=msgget(MSGKEY,IPC_EXCL); /*Check if the message queue exists */if(msqid < 0){msqid = msgget(MSGKEY,IPC_CREAT

Copyright © Windows knowledge All Rights Reserved