Chinaunix首页 | 论坛 | 博客
  • 博客访问: 42920
  • 博文数量: 19
  • 博客积分: 1560
  • 博客等级: 上尉
  • 技术积分: 165
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-24 09:57
文章分类
文章存档

2011年(3)

2009年(10)

2008年(6)

我的朋友

分类: LINUX

2011-08-18 15:02:39

  1. //share_ex.h
  2. #include <stdio.h>
  3. #include <signal.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/ipc.h>
  8. #include <sys/shm.h>
  9. #include <sys/sem.h>
  10. /* 共享内存的关键字 */
  11. #define SHMKEY1 (key_t)0x10
  12. #define SHMKEY2 (key_t)0x15
  13. /* 信号量的关键字 */
  14. #define SEMKEY (key_t)0x20
  15. /* 读写缓冲区的大小 */
  16. #define SIZ 5*BUFSIZ
  17. struct databuf{
  18.     int d_nread;
  19.     char d_buf[SIZ];
  20. };

  21. void fatal(char *mes);
  22. int getsem();
  23. void getseg(struct databuf** p1,struct databuf** p2);
  24. void removeshm(void);
  25. void reader(int semid,struct databuf *buf1,struct databuf *buf2);
  26. void writer(int semid,struct databuf *buf1,struct databuf *buf2);
  1. //share_ex.c
  2. #include "share_ex.h"
  3. #define IFLAGS (IPC_CREAT | IPC_EXCL)
  4. #define ERR ((struct databuf*) -1)
  5. static int shmid1,shmid2,semid;
  6. void fatal(char *mes)
  7. {
  8.     perror(mes);
  9.     exit(1);
  10. }
  11. void getseg(struct databuf** p1,struct databuf** p2)
  12. {
  13.     /* 创建共享内存 */
  14.     if ((shmid1=shmget(SHMKEY1,sizeof(struct databuf),0600|IFLAGS)) < 0)
  15.              fatal("shmget");
  16.     if ((shmid2=shmget(SHMKEY2,sizeof(struct databuf),0600|IFLAGS)) < 0)
  17.              fatal("shmget");
  18.      /* 建立与共享内存的连接 */
  19.     if ((*p1=(struct databuf*)(shmat(shmid1,0,0)))==ERR)
  20.              fatal("shmat");
  21.     if ((*p2=(struct databuf*)(shmat(shmid1,0,0)))==ERR)
  22.              fatal("shmat");
  23. }

  24. int getsem()
  25. {
  26.     /* 建立信号量对象 */
  27.     if ((semid=semget(SEMKEY,2,0600|IFLAGS))<0)
  28.         fatal("semget");
  29.     /* 初始化信号量对象 */
  30.     if (semctl(semid,0,SETVAL,0)<0)
  31.         fatal("semctl");
  32.     if (semctl(semid,1,SETVAL,0)<0)
  33.         fatal("semctl");
  34.     return (semid);
  35. }

  36. void removeshm(void)
  37. {
  38.     if(shmctl(shmid1,IPC_RMID,NULL)<0)
  39.         fatal("shmctl");
  40.     if(shmctl(shmid2,IPC_RMID,NULL)<0)
  41.         fatal("shmctl");
  42.     if(semctl(semid,2,IPC_RMID,NULL)<0)
  43.         fatal("semctl");
  44. }
  45. //reader.c
  46. #include "share_ex.h"
  47. struct sembuf p1={0,-1, 0 },
  48.               p2={1,-1, 0 },
  49.               v1={0, 1, 0 },
  50.               v2={1, 1, 0 };
  51. void reader(int semid,struct databuf *buf1,struct databuf *buf2)
  52. { 
  53.     for(;;) {
  54.         /* 读入buf1 缓冲区 */
  55.         buf1->d_nread=read(0,buf1->d_buf,SIZ);
  56.         /* 同步 */
  57.         //fprintf(stderr, "%s:%d -------------\n", __FUNCTION__, __LINE__);
  58.         semop(semid,&v1,1);
  59.         semop(semid,&p2,1);
  60.         //fprintf(stderr, "%s:%d -------------\n", __FUNCTION__, __LINE__);
  61.         /* 防止writer 进程休眠 */
  62.         if (buf1->d_nread<=0)
  63.             return;
  64.         buf2->d_nread=read(0,buf2->d_buf,SIZ);
  65.         semop(semid,&v1,1);
  66.         semop(semid,&p2,1);
  67.         if(buf2->d_nread<=0)
  68.             return;
  69.     }
  70. }
    1. //writer.c
    2. #include "share_ex.h"
    3. extern struct sembuf p1,p2;
    4. extern struct sembuf v1,v2;

    5. void writer(int semid,struct databuf *buf1,struct databuf *buf2)
    6. {
    7.     for(;;) {
    8.         semop(semid,&p1,1);
    9.         semop(semid,&v2,1);
    10.         if (buf1->d_nread<=0)
    11.             return;
    12.         //fprintf(stderr, "%s:%d -------------\n", __FUNCTION__, __LINE__);            
    13.         write(1,buf1->d_buf,buf1->d_nread);
    14.         semop(semid,&p1,1);
    15.         semop(semid,&v2,1);
    16.         //fprintf(stderr, "%s:%d -------------\n", __FUNCTION__, __LINE__);
    17.         if (buf2->d_nread<=0)
    18.             return;
    19.         write(1,buf2->d_buf,buf2->d_nread);
    20.     }
    21. }
    22. //shmcopy.c
    23. #include "share_ex.h"
    24. int main(void)
    25. {
    26.     int semid,pid;
    27.     struct databuf *buf1,*buf2;
    28.     /* 初始化信号量对象 */
    29.     semid=getsem();
    30.     /* 创建并连接共享内存 */
    31.     getseg(&buf1,&buf2);
    32.     switch(pid=fork()) {
    33.     case -1:
    34.         fatal("fork");
    35.     case 0:
    36.         writer(semid,buf1,buf2);
    37.         removeshm();
    38.         break;
    39.     default:
    40.         reader(semid,buf1,buf2);
    41.         break;
    42.     }
    43.     return 0;
    44. }




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