信息量机制代码

来源:互联网 发布:淘宝双十一数据 编辑:程序博客网 时间:2024/05/21 14:47
# include <stdio.h>
# include <sys/types.h>
# include <sys/msg.h>
# include <sys/ipc.h>
# define MSGKEY 75


struct msgform
{
  long mtype;
  char mtrex[1030];
 }msg;
int msgqid, i;
void CLIENT()
{
   int i;
   msgqid=msgget(MSGKEY,0777)\  printf("%d",msgqid);
   for(i=3;i>=1;i--)
   {  msg.mtype=i;
         printf("clien)sent\n");
         scanf("%s",msg.mtrex);
         msgsnd(msgqid, &msg,1024,0);
     } 
    exit(0);
    }
 void SERVER()
 {
      msgqid=msgget(MSGKEY,077|IPC_CREAT);
   do{
        msgrcv(msgqid,&msg,1030,0,0);
        printf("(server)received\n %s\n",msg.mtrex);
        } while(msg.mtype!=1);
        msgctl(msgqid,IPC_RMID,0);
        exit(0);
 }
void main()
{
   while((i=fork())==-1);
   if(!i) SERVER();
 while((i=fork())==-1);
   if(!i) CLIENT();
   wait(0);
   wait(0);
 }
0 0
原创粉丝点击