Message Queue for multi-thread synchronization in Linux

Source: Internet
Author: User

A message queue is a linked list of messages stored in the kernel with the message queue identifier.

Msgget is used to create a new queue or open an existing queue. Msgsnd adds a new message to the Message Queue. Each message includes a long type and a message cache. msgrcv is used to retrieve messages from the queue. It is intelligent to retrieve messages, not necessarily FIFO.

① Msgget: Create a New queue or open an existing queue

# Include

Int msgget (key_t key, int flag );

// Message Queue ID is returned for success;-1 is returned for Error

② Msgsnd: Send a message

# Include

Int msgsnd (int msgid, const void * ptr, size_t nbytes, int flag)

// 0 is returned for success and-1 is returned for Error

A: flag can be specified as IPC_NOWAIT. If the message queue is full, msgsnd returns EABAIN immediately after an error occurs;

If IPC_NOWAIT is not specified, msgsnd is blocked until the message queue has space.

③ Msgrcv: read the message:

Ssize_t msgrcv (int msgid, void * ptr, size_t nbytes, long type, int flag );

A. type = 0; returns the first message in the message queue.

B. type> 0 return the first message of tpye type in the message queue.

C. type <0 returns the data of type <= | type | in the message queue. If there are several types of messages, messages with the smallest type value are obtained.

To create a message queue, follow these steps:

# Define MSG_FILE "."

Struct msgtype {

Long mtype;

Char buffer [BUFFER + 1];

};

If (key = ftok (MSG_FILE, 'A') =-1)

{

Fprintf (stderr, "Creat Key Error: % s \ n", strerror (errno ));

Exit (1 );

}

If (msgid = msgget (key, IPC_CREAT | 0666/* PERM */) =-1)

{

Fprintf (stderr, "Creat Message Error: % s \ n", strerror (errno ));

Exit (1 );

}

Msg. mtype = 1;

Strncpy (msg. buffer, argv [1], BUFFER );

Msgsnd (msgid, & msg, sizeof (struct msgtype), 0 );

Msgrcv (msgid, & msg, sizeof (struct msgtype), 1, 0 );

Sample Code:

# Include

# Include

# Include

# Include

# Include

# Include

# Include

# Include

# Include

# Define MSG_FILE "."

# Define BUFFER 255.

# Define PERM S_IRUSR | S_IWUSR

# Define IPCKEY 0x111

Struct msgtype {

Long mtype;

Char buffer [BUFFER + 1];

};

Void * thr_test (void * arg ){

Struct msgtype msg;

Int msgid;

Msgid = * (int *) arg );

Printf ("msqid = % d IPC_NOWAIT = % d \ n", msgid, IPC_NOWAIT );

Time_t tt = time (0) + 8;

// While (time (0) <= tt)

//{

Msgrcv (msgid, & msg, sizeof (struct msgtype), 1, 0 );

Fprintf (stderr, "Server Receive: % s \ n", msg. buffer );

Msg. mtype = 2;

Msgsnd (msgid, & msg, sizeof (struct msgtype), 0 );

//}

Pthread_exit (void *) 2 );

}

Int main (int argc, char ** argv)

{

Struct msgtype msg;

Key_t key;

Int msgid;

Pthread_t tid;

If (argc! = 2)

{

Fprintf (stderr, "Usage: % s string \ n", argv [0]);

Exit (1 );

}

/*

Char path [256];

Sprintf (path, "% s/", (char *) getenv ("HOME "));

Printf ("path is % s \ n", path );

Msgid = ftok (path, IPCKEY );

*/

If (key = ftok (MSG_FILE, 'A') =-1)

{

Fprintf (stderr, "Creat Key Error: % s \ n", strerror (errno ));

Exit (1 );

}

If (msgid = msgget (key, IPC_CREAT | 0666/* PERM */) =-1)

{

Fprintf (stderr, "Creat Message Error: % s \ n", strerror (errno ));

Exit (1 );

}

Pthread_create (& tid, NULL, thr_test, & msgid );

Fprintf (stderr, "msid is: % d \ n", msgid );

Msg. mtype = 1;

Strncpy (msg. buffer, argv [1], BUFFER );

Msgsnd (msgid, & msg, sizeof (struct msgtype), 0 );

Exit (0 );

}
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.