Chinaunix首页 | 论坛 | 博客
  • 博客访问: 128025
  • 博文数量: 30
  • 博客积分: 972
  • 博客等级: 中士
  • 技术积分: 332
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-08 10:04
文章分类

全部博文(30)

文章存档

2012年(30)

分类: LINUX

2012-06-21 09:48:07


 

点击(此处)折叠或打开

  1. /*********************Bob************************************/
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/msg.h>
  8. #include <sys/ipc.h>
  9. typedef struct msg
  10. {
  11.  int type;
  12.  char text[100];
  13.  char name[20];
  14. }MSG;
  15. int main(int argc, char *argv[])
  16. {
  17.  pid_t pid;
  18.  char buf[50]="";
  19.  key_t key;
  20.  MSG temp;
  21.  int msg_id;
  22.  key=ftok(".",2);
  23.  msg_id=msgget(key,IPC_CREAT|0666);
  24.  if(msg_id < 0)
  25.  {
  26.  perror("msgget");
  27.  exit(-1);
  28.  }
  29.  if((pid=fork()) < 0)
  30.   {
  31.    perror("fork");
  32.    exit(-1);
  33.   }
  34.  if(pid == 0)
  35.   {
  36.    while(1)
  37.    {
  38.    printf("Bob:");
  39.    fflush(stdout);
  40.    fgets(buf,50,stdin);
  41.    temp.type=(int)*buf;
  42.    strcpy(temp.text,buf+2);
  43.    strcpy(temp.name,"Bob");
  44.    msgsnd(msg_id,&temp,sizeof(temp)-4,0);
  45.    }
  46.   }
  47.  if(pid > 0)
  48.   {
  49.    while(1)
  50.    {
  51.     msgrcv(msg_id,&temp,sizeof(temp)-4,'B',0);
  52.     printf("\r%s:%s",temp.name,temp.text);
  53.     fflush(stdout);
  54.     printf("Bob:");
  55.     fflush(stdout);
  56.    }
  57.   }
  58.  msgctl(msg_id,IPC_RMID,NULL);
  59.  return 0;
  60. }


  61. /**********Lucy.c***********/

  62. #include <stdio.h>
  63. #include <stdlib.h>
  64. #include <unistd.h>
  65. #include <string.h>
  66. #include <sys/types.h>
  67. #include <sys/msg.h>
  68. #include <sys/ipc.h>
  69. typedef struct msg
  70. {
  71.  int type;
  72.  char text[100];
  73.  char name[20];
  74. }MSG;
  75. int main(int argc, char *argv[])
  76. {
  77.  pid_t pid;
  78.  char buf[50]="";
  79.  key_t key;
  80.  MSG temp;
  81.  int msg_id;
  82.  key=ftok(".",2);
  83.  msg_id=msgget(key,IPC_CREAT|0666);
  84.  if(msg_id < 0)
  85.  {
  86.  perror("msgget");
  87.  exit(-1);
  88.  }
  89.  if((pid=fork()) < 0)
  90.   {
  91.    perror("fork");
  92.    exit(-1);
  93.   }
  94.  if(pid == 0)
  95.   {
  96.    while(1)
  97.    {
  98.    printf("Lucy:");
  99.    fflush(stdout);
  100.    fgets(buf,50,stdin);
  101.    temp.type=(int)buf[0];
  102.    strcpy(temp.text,buf+2);
  103.    strcpy(temp.name,"Lucy");
  104.    msgsnd(msg_id,&temp,sizeof(temp)-4,0);
  105.    }
  106.   }
  107.  if(pid > 0)
  108.   {
  109.    while(1)
  110.    {
  111.     msgrcv(msg_id,&temp,sizeof(temp)-4,'L',0);
  112.     printf("\r%s:%s",temp.name,temp.text);
  113.     fflush(stdout);
  114.     printf("Lucy:");
  115.     fflush(stdout);
  116.    }
  117.   }
  118.  msgctl(msg_id,IPC_RMID,NULL);
  119.  return 0;
  120. }


  121. /*****************Peter.c******************/
  122. #include <stdio.h>
  123. #include <stdlib.h>
  124. #include <unistd.h>
  125. #include <string.h>
  126. #include <sys/types.h>
  127. #include <sys/msg.h>
  128. #include <sys/ipc.h>
  129. typedef struct msg
  130. {
  131.  int type;
  132.  char text[100];
  133.  char name[20];
  134. }MSG;
  135. int main(int argc, char *argv[])
  136. {
  137.  pid_t pid;
  138.  char buf[50]="";
  139.  key_t key;
  140.  MSG temp;
  141.  int msg_id;
  142.  key=ftok(".",2);
  143.  msg_id=msgget(key,IPC_CREAT|0666);
  144.  if(msg_id < 0)
  145.  {
  146.  perror("msgget");
  147.  exit(-1);
  148.  }
  149.  if((pid=fork()) < 0)
  150.   {
  151.    perror("fork");
  152.    exit(-1);
  153.   }
  154.  if(pid == 0)
  155.   {
  156.    while(1)
  157.    {
  158.    printf("Peter:");
  159.    fflush(stdout);
  160.    fgets(buf,50,stdin);
  161.    temp.type=(int)*buf;
  162.    strcpy(temp.text,buf+2);
  163.    strcpy(temp.name,"Peter");
  164.    msgsnd(msg_id,&temp,sizeof(temp)-4,0);
  165.    }
  166.   }
  167.  if(pid > 0)
  168.   {
  169.    while(1)
  170.    {
  171.     msgrcv(msg_id,&temp,sizeof(temp)-4,'P',0);
  172.     printf("\r%s:%s",temp.name,temp.text);
  173.     fflush(stdout);
  174.     printf("Peter:");
  175.     fflush(stdout);
  176.    }
  177.   }
  178.  msgctl(msg_id,IPC_RMID,NULL);
  179.  return 0;
  180. }


 

阅读(1590) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~