#include #include #include #include #include #include #define MSGKEY1 75 //#define MSGKEY2 76 struct msgform { int mtype; char mtext[1000]; }; struct msgform msg1; int msgqid1; cleanup( ) { msgctl(msgqid1,IPC_RMID,0); exit(0); } void server( ) { int j; for(j=0;j<20;j++) signal(j,cleanup); msgqid1=msgget(MSGKEY1,0777|IPC_CREAT); /*创建75#消息队列*/ do { msgrcv(msgqid1,&msg1,1030,1,0); /*接收消息*/ printf("(server)received:%s\\n",msg1.mtext); sleep(1); printf("(server):input the message that will be send:\\n") ; scanf("%s",msg1.mtext); msg1.mtype=2; msgsnd(msgqid1,&msg1,1024,0); /*发送消息*/ }while(strcmp(msg1.mtext,"bye")); exit(0); } void client() { msgqid1=msgget(MSGKEY1,0777|IPC_CREAT); /*创建75#消息队列*/ do { printf("(client):input the message that will be send:\\n") ; scanf("%s",msg1.mtext); msg1.mtype=1; msgsnd(msgqid1,&msg1,1024,0); /*发送消息*/ printf("(client)send msg.mtext:%s\\n",msg1.mtext); sleep(1); msgrcv(msgqid1,&msg1,1030,2,0); /*接收消息*/ printf("(client)received:%s\\n",msg1.mtext); }while(strcmp(msg1.mtext,"bye")); exit(0); } main( ) { int pid1,pid2; while ((pid1=fork())==-1); if (!pid1) server( ); system("ipcs -q"); while ((pid2=fork( ))==-1); if (!pid2) client( ); wait(0); wait(0); system("ipcs -q"); }