消息队列
#include <sys/types.h>#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MSGKEY 75
struct msgform
{
long mtype;
char mtext;
};
int main()
{
struct msgform msg;
int msgqid,pid,*pint;
msgqid =msgget (MSGKEY,0777);
pid= getpid ();
pint =(int*)msg.mtext;
*pint=pid;
msg.mtype=1;//定义消息类型
msgsnd(msgqid,&msg,sizeof(int),0);//发送消息
msgrcv(msgqid,&msg,512,pid,0);//接受服务者server消息
printf("client: receive from pid %d\n",*pint);
}
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MSGKEY 75
struct msgform
{
long mtype;
char mtext;
}msg;
int msgqid;
int main()
{
int i,pid,*pint;
extern cleanup();
for(i=0;i<20;i++){//软中断处理
signal (i,cleanup);
}
//建立与顾客进程相同的消息队列
msgqid=msgget(MSGKEY,0777|IPC_CREAT);
for(;;)
{
msgrcv(msgqid,&msg, 512,1,0);
pint=(int*)msg.mtext;
pid =*pint;
printf ("server:receive from pid %d\n",pid);//接受客户client消息
msg.mtype= pid;
*pint= getpid();
msgsnd(msgqid,&msg,sizeof(int),0);
}
}
cleanup()
{
msgctl(msgqid,IPC_RMID,0);
exit (1);
}
请问为什么在我运行server接收不到消息?
页:
[1]