Linux Message Queue practice (3) and linux Message Queue practice

Source: Internet
Author: User

Linux Message Queue practice (3) and linux Message Queue practice
Integrated Use of Apis

// The parent process sends a message. The child process receives the message struct msgBuf {long mtype;/* message type, must be> 0 */char mtext [104]; /* message data */}; const int MSGNUMBER = 10; int main () {// get a key key_t msgKey = ftok ("/tmp/mySeedFile ", 'F'); // get a message queue int msgid = msgget (msgKey, IPC_CREAT | 0666); if (msgid =-1) {err_exit ("msgget error");} struct msgBuf myBuffer; pid_t pid = fork (); if (pid =-1) {err_exit ("fork error ");} // parent process, send data if (pid> 0) {myBuffer. mtype = getpid (); // send a message to the Message Queue. If the queue is full, the process is always blocked for (int I = 0; I <MSGNUMBER; ++ I) {sprintf (myBuffer. mtext, "Hello, My Number is % d", I); msgsnd (msgid, & myBuffer, strlen (myBuffer. mtext), 0);} // wait until the sub-process ends wait (NULL);} else if (pid = 0) // sub-process {// sleep for 1 second, wait until the parent process sends an end sleep (1); memset (& myBuffer, 0, sizeof (myBuffer); // continuously retrieve data from the beginning of the queue for (int I = 0; I <MSGNUMBER; ++ I) {int recvBytes = 0; if (recvBytes = msgrcv (msgid, & myBuffer, sizeof (myBuffer. mtext), getppid (), IPC_NOWAIT) =-1) {err_exit ("msgrcv error ");} else {cout <"recvBytes =" <recvBytes <endl; cout <"myBuffer. mtype = "<myBuffer. mtype <endl; cout <"\ t" <myBuffer. mtext <endl ;}}cout <"strlen (myBuffer. mtext) = "<strlen (myBuffer. mtext) <endl;} return 0 ;}

Message Queue project development case

Message Queue implementation



/** A simplified implementation-> program description: 1. client-sent data format: Type: client pid content is the content entered by the keyboard 2. the receiving format of the client is pid content sent by the server. 3. the data type received by the server is the client pid content, which is the message content of each client. 4. the data type sent by the server is client id, and the content of the client message is */

//server.cpp#include "commen.h"void echo_server(int msgid){    struct msgBuf myMsgBuf;    while (true)    {        memset(&myMsgBuf,0,sizeof(myMsgBuf));        int recvBytes = msgrcv(msgid,&myMsgBuf,MAXMSGSIZE,0,0);        if (recvBytes == -1)        {            err_exit("msgrcv error");        }        fputs(myMsgBuf.mtext,stdout);        if (msgsnd(msgid,&myMsgBuf,strlen(myMsgBuf.mtext),0) < 0)        {            err_exit("msgsnd error");        }    }}int main(){    key_t key = ftok(FILESEED,'f');    int msgid = msgget(key,0666|IPC_CREAT);    if (msgid == -1)    {        err_exit("msgget error");    }    echo_server(msgid);    return 0;}

//client.cpp#include "commen.h"void echo_client(int msgid){    struct msgBuf myMsgBuf,recvMsgBuf;    myMsgBuf.mtype = getpid();    while (fgets(myMsgBuf.mtext,MAXMSGSIZE,stdin) != NULL)    {        if (msgsnd(msgid,&myMsgBuf,strlen(myMsgBuf.mtext),0) == -1)        {            err_exit("msgsnd error");        }        memset(&recvMsgBuf,0,sizeof(recvMsgBuf));        if (msgrcv(msgid,&recvMsgBuf,sizeof(recvMsgBuf),getpid(),0) == -1)        {            err_exit("msgrcv error");        }        fputs(recvMsgBuf.mtext,stdout);        memset(&myMsgBuf+4,0,sizeof(myMsgBuf)-4);    }}int main(){    key_t key = ftok(FILESEED,'f');    int msgid = msgget(key,0666);    if (msgid == -1)    {        err_exit("msgget error");    }    echo_client(msgid);    return 0;}

//commen.h#ifndef COMMEN_H_INCLUDED#define COMMEN_H_INCLUDED#include <string>#include <string.h>#include <errno.h>#include <stdlib.h>#include <stdio.h>const char FILESEED[] = "/tmp/mySeedFile";const int MAXMSGSIZE = 1024;struct msgBuf{    long mtype;             //Message Type: Client PID    char mtext[MAXMSGSIZE]; //message data};void err_exit(std::string str){    perror(str.c_str());    exit(EXIT_FAILURE);}#endif // COMMEN_H_INCLUDED

Broaden your horizons



Appendix-Makefile

CC = g++ CPPFLAGS = -Wall -gBIN = client server SOURCES = $(BIN.=.cpp).PHONY: clean all all: $(BIN)$(BIN): $(SOURCES)clean:    -rm -rf $(BIN) bin/ obj/ core


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.