- UID
- 1029342
- 性别
- 男
|
而且要加两个头文件:#include #include
/*请求方程序msgreq.c*/#include
#include
#include
#include
#include
#include
static struct msgbuf1
{
long mtype;
char mtext[100];
} sndbuf, rcvbuf, *msgp ;
extern int errno;
int main(int argc, char **argv)
{
int rtrn, msqid ;
char name[10];
double balance;
if (argc!=2)
{
fprintf(stderr,"msgreq [01-99]"); exit(-1);
}
if ((msqid = msgget(0x888, IPC_CREAT|0660)) == -1 )
{
fprintf(stderr, "msgget 888 failed !");
//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("姓名=%s",name);
printf("余额=%lf",balance);
}
/*服务方程序msgcenter.c*/
#include
#include
#include
#include
#include
#include
static struct msgbuf1
{
long mtype;
char mtext[100];
} sndbuf, rcvbuf , *msgp;
extern int errno;
int main()
{
int rtrn, msqid ;
char strbuf[100];
if ( (msqid = msgget(0x888, IPC_CREAT|0600)) == -1 )
{
fprintf(stderr, "msgget 888 failed !"); 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",rcvbuf.mtext);
msgp->mtype=atoi(strbuf);
printf("输入帐号=%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);
}
}
} |
|