"Process Programming"--msg Message Queuing communication between processes
Message Queuing is like a linked list, FIFO, like a line of rice, first come first buy!
The key value is used to get the message queue descriptor, and I feel like a positional identifier!
function one key_t ftok (char *pathname,char proj)
Returns a key value------> Be sure to get this key before creating a queue!
Proj: project name, not zero
Open, create
function two int msgget (key_t key,int MSGFLG)//See No, here is the key value required.
Key: Value obtained by Ftok
MSGFLG: Logo
Return value: The message queue descriptor corresponding to the key value key.
Ipc_creat creating a new Message queue
IPC_EXCL if a message queue to be created already exists, an error is reversed.
Ipc_nowait read-write Message Queuing is not blocked until it is satisfied
Create condition
1, in the process of creation, does not correspond to the key value, and MSGFLG is ipc_creat--------> that is, there is no generated key value before creation, is the first time!
2, the key parameter is ipc_private.
Send Message
function three int msgsnd (int msqid, struct msgbuf*msgp, int msgsz, int msgflg)
Msqid with an open message queue ID
MSGP structure for storing messages
MSGSZ Message Data length
MSGFLG Send Message flag, meaningful MSFFLG flag for ipc_nowait,
Indicates whether to send a message if the message queue does not have enough space msgsnd wait
Message format
struct msgbuf{
Long mtype; message type
char Mtext; First address of message data
};
Accept Message
function Four int MSGRCV (int msqid,struct msgbuf *msgp,int msgsz,long msgtyp,int msgflg)
Messages are deleted after reading
/********** implementation of Message Queuing *******************/
The program implementation creates a message queue, then sends the data in the struct to the message queue, and then formats the structure
Data, and finally through the message queue message sent to the data structure body, to print out!!
/**********************************************************/
#include <sys/types.h>
#include <sys/msg.h>
#include <unistd.h>
struct MSG_BUF//defines a struct used to store the contents of a message queue
{
int mtype; Type, usually sent and received the same
Char data[255]; Queue Contents
};
int main ()
{
key_t key;
int msgid;
int ret;
struct MSG_BUF msgbuf; Beginner data structure should be understood, define structure variables
Key=ftok ("/tmp/2", ' a '); Create a Message Queue key value, the first parameter is the path, the second is the project name is not zero
printf ("Key =[%x]\n", key);
Msgid=msgget (key,ipc_creat|0666); /* Create a message queue via the file corresponding to the key value created */
if (msgid==-1)//Return one, create failed
{
printf ("Create error\n");
return-1;
}
Msgbuf.mtype = Getpid (); Type set process number, receive and send must be the same;
strcpy (msgbuf.data, "test haha"); ,//copy contents to, Structure database
Ret=msgsnd (Msgid,&msgbuf,sizeof (msgbuf.data), ipc_nowait); Send struct data to message to Team//column, parameter 1 message queue address
if (ret==-1)//Parameter 2 struct address 3 is data length, waiting for blocking
{
printf ("Send Message err\n");
return-1;
}
memset (&msgbuf,0,sizeof (MSGBUF)); Empty the struct, and then in use, when the struct has no data, the following is the return of the contents of the message queue to the struct.
RET=MSGRCV (Msgid,&msgbuf,sizeof (Msgbuf.data), Getpid (), ipc_nowait); receive the contents of the message queue MsgId in the struct
if (ret==-1)
{
printf ("recv message err\n");
return-1;
}
printf ("Recv msg =[%s]\n", msgbuf.data); Finally, the information printed out, back to a circle, haha ha!
}
Do you understand--------------------------------------------------------->>>>>>>>>cheer up!?
"Process Programming"--msg Message Queuing communication between processes