Guest User

Untitled

a guest
Jun 7th, 2016
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.08 KB | None | 0 0
  1. // common.h
  2. #define MEMORY_KEY 145
  3. #define SIZE_OF_ARRAY 10
  4.  
  5. struct wrapper
  6. {
  7.     int array[SIZE_OF_ARRAY];
  8.     sem_t empty;
  9.     sem_t pmutex;
  10.     sem_t cmutex;
  11.     sem_t full;
  12.     int n;
  13. };
  14.  
  15. // consumer.c
  16. #include <semaphore.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include "common.h"
  21. #include <sys/shm.h>
  22.  
  23. int check_prime(int a) {
  24.     return a;
  25. }
  26.  
  27. int memoryID;
  28. struct wrapper *memory;
  29.  
  30. int main() {
  31.      srand(time(NULL));
  32.     key_t sharedMemoryKey = ftok(".",MEMORY_KEY);
  33.     if(sharedMemoryKey==-1)
  34.     {
  35.         perror("ftok():");
  36.         exit(1);
  37.     }
  38.     memoryID=shmget(sharedMemoryKey,sizeof(struct wrapper),0);
  39.  
  40.     if(memoryID==-1)
  41.     {
  42.         perror("shmget(): ");
  43.         exit(1);
  44.     }
  45.  
  46.     memory = shmat(memoryID,NULL,0);
  47.     if(memory== (void*)-1)
  48.     {
  49.         perror("shmat():");
  50.         exit(1);
  51.     }
  52.  
  53.     while(1)
  54.     {
  55.         sem_wait(&memory->full);
  56.         sem_wait(&memory->cmutex);
  57.  
  58.         int n = memory->n;
  59.         int temp = (memory->array)[n];
  60.         printf("Removed item: %d\tPrime:%d\tNumer of tasks left:%d\n",
  61.             temp, check_prime(temp),n);
  62.         memory->n--;
  63.         usleep(10000);
  64.  
  65.         sem_post(&memory->cmutex);
  66.         sem_post(&memory->empty);
  67.     }
  68. }
  69.  
  70. // producer.c
  71. #include <semaphore.h>
  72. #include <stdio.h>
  73. #include <errno.h>
  74. #include <stdlib.h>
  75. #include <unistd.h>
  76. #include <sys/ipc.h>
  77. #include <sys/shm.h>
  78. #include <string.h>
  79. #include <fcntl.h>
  80. #include "common.h"
  81.  
  82. int memoryID;
  83. struct wrapper *memory;
  84. int rc;
  85.  
  86. void atexit_function() {
  87.     rc = shmctl(memoryID, IPC_RMID, NULL);
  88.     rc = shmdt(memory);
  89.     sem_destroy(&memory->pmutex);
  90.     sem_destroy(&memory->cmutex);
  91.     sem_destroy(&memory->empty);
  92.     sem_destroy(&memory->full);
  93. }
  94.  
  95. int main() {
  96.     atexit(atexit_function);
  97.     //creating key for shared memory
  98.     srand(time(NULL));
  99.     key_t sharedMemoryKey = ftok(".", MEMORY_KEY);
  100.     if (sharedMemoryKey == -1) {
  101.         perror("ftok():");
  102.         exit(1);
  103.     }
  104.  
  105.     memoryID = shmget(sharedMemoryKey, sizeof(struct wrapper), IPC_CREAT | 0600);
  106.     if (memoryID == -1) {
  107.         perror("shmget():");
  108.         exit(1);
  109.     }
  110.  
  111.     memory = shmat(memoryID, NULL, 0);
  112.     if (memory == (void *) -1) {
  113.         perror("shmat():");
  114.         exit(1);
  115.     }
  116.  
  117.     //initialization
  118.  
  119.     printf("Initializataion !\n");
  120.     memset(&memory->array, 0, sizeof(memory->array));
  121.     sem_init(&memory->pmutex, 0, 1);
  122.     sem_init(&memory->cmutex, 0, 1);
  123.     sem_init(&memory->empty, 0, SIZE_OF_ARRAY);
  124.     sem_init(&memory->full, 0, 0);
  125.     memory->n = -1;
  126.  
  127.     if (memoryID == -1) {
  128.         perror("shmget(): ");
  129.         exit(1);
  130.     }
  131.  
  132.  
  133.     for (int i = 0; i < 100; ++i)
  134.     {
  135.         int r = rand();
  136.         sem_wait(&memory->empty);
  137.         sem_wait(&memory->pmutex);
  138.         memory->n++;
  139.         (memory->array)[memory->n]=r;
  140.         printf("Adding task\t Value:%d\tNumber of tasks waiting:%d \n",r,memory->n);
  141.         usleep(10000);
  142.         sem_post(&memory->pmutex);
  143.         sem_post(&memory->full);
  144.        
  145.     }
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment