当前位置:网站首页>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

边栏推荐
- Huawei interview summary during the epidemic
- Jupyter remote server configuration and server startup
- Cesium for unreal quick start - simple scenario configuration
- UE4 source code reading_ Bone model and animation system_ Animation node
- Go resolve ID card
- P1596 [USACO10OCT]Lake Counting S
- [set theory] order relation (the relation between elements of partial order set | comparable | strictly less than | covering | Haas diagram)
- Data analysis exercises
- Vscode, idea, VIM development tool shortcut keys
- Kunlunbase meetup is waiting for you!
猜你喜欢

Jupyter remote server configuration and server startup

Easy touch plug-in
![[set theory] order relation (the relation between elements of partial order set | comparable | strictly less than | covering | Haas diagram)](/img/df/a034032e203e7935dafaf8a71cb6c8.jpg)
[set theory] order relation (the relation between elements of partial order set | comparable | strictly less than | covering | Haas diagram)

matlab神經網絡所有傳遞函數(激活函數)公式詳解

Unity editor expansion - scrolling list

Advanced OSG collision detection

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

UE4 source code reading_ Bone model and animation system_ Animation compression

GIS实战应用案例100篇(七十八)-多规合一数据库设计及数据入库

VIM learning notes from introduction to silk skating
随机推荐
Golang string segmentation, substitution and interception
UE4 source code reading_ Mobile synchronization
C#课程设计之员工信息管理系统
Dealing with duplicate data in Excel with xlwings
Osgearth target selection
Introduction to Base64 coding
Use of ue5 QRcode plug-in
Un système de gestion de centre commercial pour la conception de cours de technologie d'application de base de données
【K&R】中文第二版 个人题解 Chapter1
Conversion between string and int types in golang
matlab神經網絡所有傳遞函數(激活函數)公式詳解
Golang time format sorting
Map的实现类的顺序性
Unity Editor Extension - Outline
Why can void * be a general pointer
LinkList
十六进制编码简介
Conversion between golang JSON format and structure
Go resolve ID card
P1596 [USACO10OCT]Lake Counting S