Chinaunix首页 | 论坛 | 博客
  • 博客访问: 631294
  • 博文数量: 1008
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 5175
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-31 09:44
文章分类
文章存档

2012年(1008)

我的朋友

分类:

2012-08-01 11:15:41

  1. /*
  2.  * 生产者消费者模型
  3.  * 生产者代码实现
  4.  *
  5.  */

  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <sys/types.h>
  9. #include <sys/ipc.h>
  10. #include <sys/sem.h>

  11. #define SHM_SIZE 10            //共享内存大小

  12. void pro_exit(char str[], int temp)        //程序出错,打印错误信息并退出程序
  13. {
  14.     if(temp == -1)                    
  15.     {
  16.         perror(str);
  17.         exit(0);
  18.     }
  19. }

  20. int main(void)
  21. {
  22.     int        sem_id,                //保存信号量ID            
  23.             shm_id;                //保存共享内存ID

  24.     key_t    sem_key,            //保存信号量关键值            
  25.             shm_key;            //保存共享内存关键值    
  26.     
  27.     struct sembuf lock0 = {0, -1, 0};        //互拆锁上锁
  28.     struct sembuf unlock0 = {0, 1, 0};        //互拆锁解锁

  29.     struct sembuf lock1 = {1, -1, 0};        
  30.     struct sembuf unlock1 = {1, 1, 0};        

  31.     struct sembuf lock2 = {2, -1, 0};        
  32.     struct sembuf unlock2 = {2, 1, 0};        

  33.     int *p;                //指向共享内存
  34.     int ret;
  35.     int n = 100;
  36.     
  37.     sem_key = ftok("lzy", 2);    //获取互斥锁关键值
  38.     pro_exit("sem_ftok", sem_key);    //检测程序是否出错

  39.     if((sem_id = semget(sem_key, 3, IPC_CREAT |IPC_EXCL | 0666)) < 0)        //创建三个信号量互斥锁
  40.     {
  41.         sem_id = semget(sem_key, 0, 0);        //读取已存在的互斥锁
  42.         pro_exit("semget", sem_id);
  43.     }
  44.     else
  45.     {

  46.     ret = semctl(sem_id, 0, SETVAL, 1);        //设置互斥锁的值为1
  47.     pro_exit("semctl_0", ret);

  48.     ret = semctl(sem_id, 1, SETVAL, 10);    //设置生产者允许生产最大值为30
  49.     pro_exit("semctl_1", ret);

  50.     ret = semctl(sem_id, 2, SETVAL, 0);        //设置消费者允许最小消费
  51.     pro_exit("semctl_2", ret);
  52.     }
  53.     shm_key = ftok("lzy", 1);                //获取共享内存关键值
  54.     pro_exit("shm_ftok", shm_key);            //检测程序是否出错

  55.     shm_id = shmget(shm_key, SHM_SIZE, IPC_CREAT | 0666);    //创建共享内存
  56.     pro_exit("shmget", shm_id);

  57.         p = (int *)shmat(shm_id, NULL, 0);        //映像共享内存
  58.         pro_exit("shmat", (int)*p);

  59.     while(n--)
  60.     {
  61.         semop(sem_id, &lock1, 1);            //生产者上锁
  62.         ret = semop(sem_id, &lock0, 1);            //互斥锁上锁
  63.         pro_exit("semop_lock", ret);

  64.         
  65.         (*p)++;
  66.         printf("process %d : count = %d\n", getpid(),*p);    //加一

  67.         

  68.         ret = semop(sem_id, &unlock0, 1);            //互斥锁解锁
  69.         pro_exit("semop_unlock", ret);
  70.         semop(sem_id, &unlock2, 1);                    //消费者加一

  71.         sleep(1);
  72.     }

  73. ret = shmdt(p);                            //释放共享内存
  74.         pro_exit("shmdt", ret);
  75.     ret = shmctl(shm_id, IPC_RMID, NULL);        //删除共享内存
  76.     pro_exit("shmctl", ret);

  77.     ret = semctl(sem_id, 0, IPC_RMID);            //删除信号量
  78.     pro_exit("semctl", ret);

  79.     return 0;
  80. }
  1. /*
  2.  * 生产者消费者模型
  3.  * 消费者代码实现
  4.  *
  5.  */

  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <sys/types.h>
  9. #include <sys/ipc.h>
  10. #include <sys/sem.h>

  11. #define SHM_SIZE 10            //共享内存大小

  12. void pro_exit(char str[], int temp)        //程序出错,打印错误信息并退出程序
  13. {
  14.     if(temp == -1)                    
  15.     {
  16.         perror(str);
  17.         exit(0);
  18.     }
  19. }

  20. int main(void)
  21. {
  22.     int        sem_id,                //保存信号量ID            
  23.             shm_id;                //保存共享内存ID

  24.     key_t    sem_key,            //保存信号量关键值            
  25.             shm_key;            //保存共享内存关键值    
  26.     
  27.     struct sembuf lock0 = {0, -1, 0};        //互拆锁上锁
  28.     struct sembuf unlock0 = {0, 1, 0};        //互拆锁解锁

  29.     struct sembuf lock1 = {1, -1, 0};        
  30.     struct sembuf unlock1 = {1, 1, 0};        

  31.     struct sembuf lock2 = {2, -1, 0};        
  32.     struct sembuf unlock2 = {2, 1, 0};    

  33.     int *p;                //指向共享内存
  34.     int ret;
  35.     int n = 100;
  36.     
  37.     sem_key = ftok("lzy", 2);    //获取互斥锁关键值
  38.     pro_exit("sem_ftok", sem_key);    //检测程序是否出错

  39.     if((sem_id = semget(sem_key, 3, IPC_CREAT |IPC_EXCL | 0666)) < 0)        //创建三个信号量互斥锁
  40.     {
  41.         sem_id = semget(sem_key, 0, 0);        //读取已存在的互斥锁
  42.         pro_exit("semget", sem_id);
  43.     }
  44.     else
  45.     {
  46.         ret = semctl(sem_id, 0, SETVAL, 1);        //设置互斥锁的值为1
  47.         pro_exit("semctl_0", ret);

  48.         ret = semctl(sem_id, 1, SETVAL, 10);    //设置生产者允许生产最大值为30
  49.         pro_exit("semctl_1", ret);

  50.         ret = semctl(sem_id, 2, SETVAL, 0);        //设置消费者允许最小消费
  51.         pro_exit("semctl_2", ret);
  52.     }
  53.     shm_key = ftok("lzy", 1);                //获取共享内存关键值
  54.     pro_exit("shm_ftok", shm_key);            //检测程序是否出错

  55.     shm_id = shmget(shm_key, SHM_SIZE, IPC_CREAT | 0666);    //创建共享内存
  56.     pro_exit("shmget", shm_id);

  57.         p = (int *)shmat(shm_id, NULL, 0);        //映像共享内存
  58.         pro_exit("shmat", (int)*p);
  59.     while(n--)
  60.     {
  61.         semop(sem_id, &lock2, 1);            //消费者上锁
  62.         ret = semop(sem_id, &lock0, 1);            //互斥锁上锁
  63.         pro_exit("semop_lock", ret);        

  64.         (*p)--;
  65.         printf("ser %d : count = %d\n", getpid(),*p);    //
  66.         

  67.         ret = semop(sem_id, &unlock0, 1);            //互斥锁解锁
  68.         pro_exit("semop_unlock", ret);
  69.         semop(sem_id, &unlock1, 1);                    //生产者加一

  70.         sleep(1);
  71.     }

  72.     ret = shmdt(p);                            //释放共享内存
  73.         pro_exit("shmdt", ret);

  74.     ret = shmctl(shm_id, IPC_RMID, NULL);        //删除共享内存
  75.     pro_exit("shmctl", ret);

  76.     ret = semctl(sem_id, 0, IPC_RMID);            //删除信号量
  77.     pro_exit("semctl", ret);

  78.     return 0;
  79. }
阅读(281) | 评论(0) | 转发(0) |
0

上一篇:内存映射实现快速排序

下一篇:exec

给主人留下些什么吧!~~