LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 862|回复: 0

多個生產者及多個消費者..問題??

[复制链接]
发表于 2007-5-26 14:58:52 | 显示全部楼层 |阅读模式
請問這樣能否平衡兩者所需??

信號量及共享記憶體 初始化

  1. #include <sys/types.h>
  2. #include <sys/ipc.h>
  3. #include <sys/shm.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7. #include <sys/sem.h>

  8. #define KEY (1492)

  9. void sem_lock(int sem_set_id)
  10. {
  11.     /* structure for semaphore operations.   */
  12.     struct sembuf sem_op;

  13.     /* wait on the semaphore, unless it's value is non-negative. */
  14.     sem_op.sem_num = 0;
  15.     sem_op.sem_op = -1;
  16.     sem_op.sem_flg = 0;
  17.     semop(sem_set_id, &sem_op, 1);
  18. }

  19. void sem_unlock(int sem_set_id)
  20. {
  21.     /* structure for semaphore operations.   */
  22.     struct sembuf sem_op;

  23.     /* signal the semaphore - increase its value by one. */
  24.     sem_op.sem_num = 0;
  25.     sem_op.sem_op = 1;   /* <-- Comment 3 */
  26.     sem_op.sem_flg = 0;
  27.     semop(sem_set_id, &sem_op, 1);
  28. }

  29. void shared_memory()
  30. {
  31.   int shmid;
  32.   int *shm;
  33.   key_t key = IPC_PRIVATE;
  34.   int flag = SHM_R | SHM_W;


  35.    key = 1234;


  36.   shmid = shmget(key, 1000, flag);
  37.   printf("shared memory for key %d: %d\n", key, shmid);
  38.   if (shmid < 0) {
  39.     perror("shmget");
  40.     printf("Try to create this segment\n");
  41.     shmid = shmget(key, 1000, flag | IPC_CREAT);
  42.     if (shmid < 0) {
  43.       perror("shmget | IPC_CREAT");
  44.     }
  45.   }

  46.   shm = (int *)shmat(shmid, /*addr*/0, /*flag*/0);
  47.   if (shm == (int *)-1) {
  48.     perror("shmat");
  49.     exit(1);
  50.   }
  51.   [color=red]*shm = 5;[/color]
  52.   printf("shared memory content: %d\n", *shm);

  53. }

  54. void sema_init()
  55. {
  56.    int id;
  57.    union semun {
  58.            int val;
  59.            struct semid_ds *buf;
  60.            ushort * array;
  61.            } argument;

  62.    argument.val = 0;

  63.    id = semget(KEY, 1, 0666 | IPC_CREAT);

  64.    if(id < 0)
  65.    {
  66.       fprintf(stderr, "Unable to obtain semaphore.\n");
  67.       exit(0);
  68.    }

  69.    if( semctl(id, 0, SETVAL, argument) < 0)
  70.    {
  71.       fprintf( stderr, "Cannot set semaphore value.\n");
  72.    }
  73.    else
  74.    {
  75.       fprintf(stderr, "Semaphore %d initialized.\n", KEY);
  76.    }
  77. }
  78. int main(){
  79.         sema_init();
  80.         shared_memory();
  81.         return 0;
  82. }

复制代码


生產者

  1. #include <sys/types.h>
  2. #include <sys/ipc.h>
  3. #include <sys/shm.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7. #include <sys/sem.h>

  8. #define KEY (1492)

  9. void sem_lock(int sem_set_id)
  10. {
  11.     /* structure for semaphore operations.   */
  12.     struct sembuf sem_op;

  13.     /* wait on the semaphore, unless it's value is non-negative. */
  14.     sem_op.sem_num = 0;
  15.     sem_op.sem_op = -1;
  16.     sem_op.sem_flg = 0;
  17.     semop(sem_set_id, &sem_op, 1);
  18. }

  19. void sem_unlock(int sem_set_id)
  20. {
  21.     /* structure for semaphore operations.   */
  22.     struct sembuf sem_op;

  23.     /* signal the semaphore - increase its value by one. */
  24.     sem_op.sem_num = 0;
  25.     sem_op.sem_op = 1;   /* <-- Comment 3 */
  26.     sem_op.sem_flg = 0;
  27.     semop(sem_set_id, &sem_op, 1);
  28. }

  29. int main()
  30. {
  31.   int shmid;
  32.   int *shm;
  33.   key_t key;

  34.   key = 1234;

  35.   printf("Trying shared memory %d\n", key);
  36.   shmid = shmget(key, 1000, SHM_R | SHM_W);
  37.   if (shmid < 0) {
  38.     perror("shmget");
  39.     shmid = key;
  40.   }
  41.   else {
  42.     printf("shared memory: %d\n", shmid);
  43.   }
  44.   shm = (int *)shmat(shmid, /*addr*/0, /*flag*/0);
  45.   if (shm == (int *)-1) {
  46.     perror("shmat");
  47.     exit(1);
  48.   }
  49.   printf("shared memory: %p\n", *shm);
  50.   if (shm != 0) {
  51.     printf("shared memory content: %d\n", *shm);
  52.   }

  53.         int sem_set_id;
  54.         sem_set_id = semget(KEY, 1, 0666);

  55.         sem_unlock(sem_set_id);

  56.         int pause_time;
  57.         int i;
  58.         for(i=0; i<10; i++){
  59.                 sem_lock(sem_set_id);
  60.                 pause_time = rand() % 3;
  61.             sleep(pause_time);
  62.                 if(*shm < 5){
  63.                         *shm = *shm + 1;
  64.                         printf("producer item: %d\n", *shm);
  65.                 }else {printf("Full: %d\n", *shm);}
  66.                 //printf("C");
  67.                 fflush(stdout);
  68.                 sem_unlock(sem_set_id);
  69.                 if(*shm < 5){
  70.                         *shm = *shm + 1;
  71.                         printf("producer item: %d\n", *shm);
  72.                 }else {printf("Full: %d\n", *shm);}
  73.                 //printf("C");
  74.                 fflush(stdout);
  75.                 pause_time = rand() % 2;
  76.             sleep(pause_time);

  77.         }

  78.   return 0;
  79. } /* main */

复制代码


消費者

  1. #include <sys/types.h>
  2. #include <sys/ipc.h>
  3. #include <sys/shm.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7. #include <sys/sem.h>

  8. #define KEY (1492)

  9. void sem_lock(int sem_set_id)
  10. {
  11.     /* structure for semaphore operations.   */
  12.     struct sembuf sem_op;

  13.     /* wait on the semaphore, unless it's value is non-negative. */
  14.     sem_op.sem_num = 0;
  15.     sem_op.sem_op = -1;
  16.     sem_op.sem_flg = 0;
  17.     semop(sem_set_id, &sem_op, 1);
  18. }

  19. void sem_unlock(int sem_set_id)
  20. {
  21.     /* structure for semaphore operations.   */
  22.     struct sembuf sem_op;

  23.     /* signal the semaphore - increase its value by one. */
  24.     sem_op.sem_num = 0;
  25.     sem_op.sem_op = 1;   /* <-- Comment 3 */
  26.     sem_op.sem_flg = 0;
  27.     semop(sem_set_id, &sem_op, 1);
  28. }

  29. int main()
  30. {
  31.   int shmid;
  32.   int *shm;
  33.   key_t key;

  34.   key = 1234;

  35.   printf("Trying shared memory %d\n", key);
  36.   shmid = shmget(key, 1000, SHM_R | SHM_W);
  37.   if (shmid < 0) {
  38.     perror("shmget");
  39.     shmid = key;
  40.   }
  41.   else {
  42.     printf("shared memory: %d\n", shmid);
  43.   }
  44.   shm = (int *)shmat(shmid, /*addr*/0, /*flag*/0);
  45.   if (shm == (int *)-1) {
  46.     perror("shmat");
  47.     exit(1);
  48.   }
  49.   printf("shared memory: %p\n", *shm);
  50.   if (shm != 0) {
  51.     printf("shared memory content: %d\n", *shm);
  52.   }

  53.         int sem_set_id;
  54.         sem_set_id = semget(KEY, 1, 0666);

  55.         sem_unlock(sem_set_id);

  56.         int pause_time;
  57.         int i;
  58.         sleep(2);
  59.         for(i=0; i<10; i++){

  60.                 sem_lock(sem_set_id);
  61.                 pause_time = rand() % 3;
  62.             sleep(pause_time);
  63.                 if(*shm > 0){
  64.                         *shm = *shm - 1;
  65.                         printf("Consuemr item: %d\n", *shm);
  66.                 }else {printf("Out of Stock: %d\n", *shm);}
  67.                 //printf("D");
  68.                 fflush(stdout);
  69.                 sem_unlock(sem_set_id);
  70.                 if(*shm > 0){
  71.                         *shm = *shm - 1;
  72.                         printf("Consuemr item: %d\n", *shm);
  73.                 }else {printf("Out of Stock: %d\n", *shm);}
  74.                 //printf("D");
  75.                 fflush(stdout);
  76.                 pause_time = rand() % 2;
  77.             sleep(pause_time);
  78.         }

  79.   return 0;
  80. } /* main */

复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表