Message Queue for inter-process communication in linux

Source: Internet
Author: User

1. Create a message queue:

Int msgget (key_t key, int flags );
Function: If the IPC_CREAT bit is set in flags, create a new queue.
The value of the key.
When the key value is IPC_PRIVATE, it indicates that the key value is generated by the system.
If the IPC_CREAT bit is not set in flags, open the existing team corresponding to the key
Column.
The return value is the id of the message queue, which is globally valid before the system is shut down.

/* * mkq.c - Create a SysV IPC message queue */#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <stdio.h>#include <stdlib.h>int main(int argc, char *argv[]){    int qid; /* The queue identifier */    key_t key; /* The queue key */    key = 123;    /* Create the queue */    if((qid = msgget(key, IPC_CREAT | 0666)) < 0) {        perror("msgget:create");        exit(EXIT_FAILURE);    }    printf("created queue id = %d\n", qid);         /* Open the queue again */    if((qid == msgget(key, 0)) < 0) {        perror("msgget:open");        exit(EXIT_FAILURE);    }    printf("opened queue id = %d\n", qid);    exit(EXIT_SUCCESS);}

View existing Message Queue commands: ipcs-q

 

2. write messages:

Int msgsnd (int msqid, const void * prt, size_t nbytes, int flags );

/* * qsnd.c - Send a message to previously opened queue */#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#define BUFSZ 512/* Message structure */struct msg {    long msg_type;    char msg_text[BUFSZ];};int main(int argc, char *argv[]){    int qid; /* The queue identifier */    int len; /* Length of data sent */    struct msg pmsg; /* Pointer to message structure */         /* Expect the queue ID passed on the command-line */    if(argc != 2) {        puts("USAGE: qsnd <queue ID>");        exit(EXIT_FAILURE);    }    qid = atoi(argv[1]);    /* Get the message to add to the queue */    puts("Enter message to post:");    if((fgets((&pmsg)->msg_text, BUFSZ, stdin)) == NULL) {        puts("no message to post");        exit(EXIT_SUCCESS);    }         /* Associate the message with this process */    pmsg.msg_type = getpid();    /* Add the message to the queue */    len = strlen(pmsg.msg_text);    if((msgsnd(qid, &pmsg, len, 0)) < 0) {        perror("msgsnd");        exit(EXIT_FAILURE);    }    puts("message posted");    exit(EXIT_SUCCESS);}

In this case, the default qid of the Message Queue created in the previous section is 0, so run the write-side program command:./xx 0.

 

3. read messages:

Int msgrcv (int msqid, void * ptr, size_t nbytes, long type, int flags );
Function: extracts the first message queue that matches the type from the Message Queue represented by msqid.
The message is stored in the space pointed to by ptr.
When type is equal to 0, the first message in the queue is taken out.
If the value of type is greater than 0, the first message with the same type in the queue is taken out.

/* * qrd.c - Read all message from a message queue */#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <stdio.h>#include <stdlib.h>#define BUFSZ 512/* Message structure */struct msg {    long msg_type;    char msg_text[BUFSZ];};int main(int argc, char *argv[]){    int qid; /* The queue identifier */    int len; /* Length of message */    struct msg pmsg; /* A message structure */         /* Expect the queue ID passed on the command-line */    if(argc != 2) {        puts("USAGE: qrd <queue ID>");        exit(EXIT_FAILURE);    }    qid = atoi(argv[1]);    /* Retrieve and display a message from the queue */    len = msgrcv(qid, &pmsg, BUFSZ, 0, 0);    if(len > 0) {        (&pmsg)->msg_text[len] = '\0';        printf("reading queue id: %05d\n", qid);        printf("message type: %05ld\n", (&pmsg)->msg_type);printf("message length: %d bytes\n", len);         printf("message text: %s\n", (&pmsg)->msg_text);    } else {        perror("msgrcv");        exit(EXIT_FAILURE);    }         exit(EXIT_SUCCESS);}

Run the read-in command:./xx 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.