当前位置:网站首页>Message queue for interprocess communication
Message queue for interprocess communication
2022-07-03 08:31:00 【Spring and autumn sword armor Li Chungang】
establish server.c and client.c, The client is responsible for entering two operands and an operator , The server is responsible for receiving and calculating the two operands according to the operator , Finally, the operation result is returned to the client for output . Such as client input 2+4, The server will send the result 6 Return to the client .
Their thinking :
The purpose of this topic is to investigate how to use the related functions of message queue. The client and server can be understood as the sender and receiver respectively, so we only need to create a message queue , Every time the client passes into the message queue 3 It's worth , They are calculated numbers and operation symbols , The server only needs to process and convert the received value , I am dealing with this problem is , There is not much research on the incoming information of message queue , Instead, I think the main problem is how to convert the received message into operands and symbols .
tips: The code is rough , But the basic functions can be realized . If you have more ideas , You can write in the comments section . Accept your correction and criticism .
Code implementation
client
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<time.h>
#include<sys/types.h>
struct msgbuf
{
long mtype;
char mtext[500];
};
int main()
{
int msqid;
int msqid1;
int i;
struct msgbuf buf;
int flag;
int sendlength;
msqid = msgget((key_t)1111 , IPC_CREAT|0666);
if(msqid<0)
printf("ERROR");
buf.mtype = 1;
sendlength = sizeof(struct msgbuf) - sizeof(long);
sprintf(buf.mtext,"Hello World");
msgsnd(msqid,&buf,sendlength,0);
msgrcv(msqid,&buf,sendlength,1,0);
printf("%s",buf.mtext);
for(i = 0 ; i<3;i++)
{
scanf("%s",buf.mtext);
flag = msgsnd(msqid,&buf,sendlength,0);
if(i == 3)
break;
}
return 0;
}The server
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<time.h>
#include<sys/types.h>
struct msgbuf
{
long mtype;
char mtext[500];
};
int main()
{
int msqid;
int msqid1;
struct msgbuf buf;
int flag;
int number1,number2,sum,i;
char *a;
int recvlength;
msqid = msgget(1111 , 0);
if(msqid<0)
printf("ERROR");
buf.mtype = 1;
recvlength = sizeof(struct msgbuf) - sizeof(long);
for(i=0;i<3;i++)
{
flag = msgrcv(msqid,&buf,recvlength,1,0);
if(i==0)
{
number1 = atoi(buf.mtext);
printf("%d\n",number1);
}
else if(i==1)
{
number2 = atoi(buf.mtext);
printf("%d\n",number2);
}
else if(i == 2)
{
a = buf.mtext;
printf("%s\n",a);
}
}
if(strcmp(a, "*") == 0)
printf("%d\n",i = number1*number2);
else if(strcmp(a, "/") == 0)
printf("%d\n",i=number1/number2);
else if(strcmp(a, "+") == 0)
printf("%d\n",i=number1+number2);
else if(strcmp(a, "-") == 0)
printf("%d\n",i=number1-number2);
else
printf("ERROR!!!\n");
sprintf(buf.mtext,"%d",i);
msgsnd(msqid,&buf,recvlength,0);
printf("OVER\n");
return 0;
}Run a screenshot

边栏推荐
- [set theory] order relation (hastu example | divisive relation hastu | inclusive relation hastu | refinement relation hastu)
- 數據庫應用技術課程設計之商城管理系統
- Unity learning notes
- 梯度下降法求解BP神经网络的简单Demo
- C#课程设计之员工信息管理系统
- [cloud native] introduction and use of feign of microservices
- Osgearth starry background
- Golang的range
- 数据分析练习题
- Classes and objects
猜你喜欢

C#课程设计之员工信息管理系统

了解小程序的笔记 2022/7/3

OpenGL learning notes

C language - Introduction - essence Edition - take you into programming (I)

Unity editor expansion - scrolling list

Three characteristics

Easy touch plug-in

Base64 and base64url

Creation of osgearth earth files to the earth ------ osgearth rendering engine series (1)

UE4 source code reading_ Bone model and animation system_ Animation compression
随机推荐
Creation of osgearth earth files to the earth ------ osgearth rendering engine series (1)
MySQL 8
Osgearth topographic shading map drawing
What is BFC?
Introduction to Base64 coding
Simply start with the essence and principle of SOM neural network
Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
VIM learning notes from introduction to silk skating
[audio and video] ijkplayer error code
简易入手《SOM神经网络》的本质与原理
Osgearth north arrow display
Basic operation and process control
Easy touch plug-in
Notes on understanding applets 2022/7/3
php-fpm软件的安装+openresty高速缓存搭建
UE4 source code reading_ Bone model and animation system_ Animation process
数据分析练习题
十六进制编码简介
Youyou1 of xlua knapsack system
Unity Editor Extension - Outline