📄 messagequen.txt
字号:
/*请求方程序msgreq.c*/
#include
#include
#include
#include
#include
static struct msgbuf1{
long mtype;
char mtext[100];
} sndbuf, rcvbuf, *msgp ;
extern int errno;
main(int argc, char **argv)
{ int rtrn, msqid ;
char name[10];
double balance;
if (argc!=2){ fprintf(stderr,
"msgreq [01-99]\n"); exit(-1); }
if ( (msqid = msgget(0x888, IPC_CREAT|0660)) == -1 ){
fprintf(stderr, "msgget 888 failed !\n"); exit(-1);
}
msgp=&sndbuf;
sprintf(sndbuf.mtext,"%2.2s",argv[1]);
printf("输入4位帐号:");
scanf("%s",&sndbuf.mtext[2]);
sndbuf.mtext[6]=0;
msgp->mtype=666;
rtrn=msgsnd(msqid,msgp, strlen(sndbuf.mtext), 0);
if (rtrn==-1){
perror("msgsnd"); exit(-1);
}
msgp=&rcvbuf;
fprintf(stderr,"等待后台数据处理进程的回答....");
rtrn=msgrcv(msqid,msgp, 100, atoi(argv[1]), 0);
if(rtrn==-1){ perror("msgrcv"); exit(-1); }
sscanf(rcvbuf.mtext,"%[^|]|%lf",name,&balance);
printf("\n姓名=%s\n",name);
printf("余额=%lf\n",balance);
}
/*服务方程序msgcenter.c*/
#include
#include
#include
#include
#include
static struct msgbuf1{
long mtype;
char mtext[100];
} sndbuf, rcvbuf , *msgp;
extern int errno;
main()
{ int rtrn, msgqid ;
char strbuf[100];
if ( (msqid = msgget(0x888, IPC_CREAT|0600)) == -1 ){
fprintf(stderr, "msgget 888 failed !\n"); exit(-1);
}
while(1) {
msgp=&rcvbuf;
fprintf(stderr,"等待前台进程的请求....");
rtrn=msgrcv(msqid, msgp, 100, 666 ,MSG_NOERROR);
if(rtrn==-1){ perror("msgrcv");exit(-1); }
msgp=&sndbuf;
sprintf(strbuf,"%2.2s\0",rcvbuf.mtext);
msgp->mtype=atoi(strbuf);
printf("\n输入帐号=%4.4s的帐户姓名:",&rcvbuf.mtext[2]);
scanf("%s",sndbuf.mtext);
strcat(sndbuf.mtext,"|");
printf("输入该帐户余额:");
scanf("%s",strbuf);
strcat(sndbuf.mtext,strbuf);
rtrn=msgsnd(msqid,msgp, strlen(sndbuf.mtext), 0);
if (rtrn==-1){ perror("msgsnd"); exit(-1); }
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -