Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 14th, 2012  |  syntax: C  |  size: 4.26 KB  |  views: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include<stdio.h>
  2. #include<sys/types.h>
  3. #include<sys/ipc.h>
  4. #include<sys/shm.h>
  5. #include "sem.h"
  6. #include<stdlib.h>
  7.  
  8. #define N 3
  9. #define BUFSIZE 1
  10. #define PERMS 0666 //0666 - To grant read and write permissions
  11.  
  12. int *buffer, *num_order;
  13. int nextp=0,nextc=0;
  14. int emptyShelf, orderOnShelf, pizzaOnShelf, mutex;
  15.  
  16. void waiter_as_producer()
  17. {
  18.     nextp = 0, (*num_order) = 0;
  19.     int order, *add;
  20.     char cont;
  21.     printf("\nShould we take order today (y or n): ");
  22.     scanf("\n%c", &cont);
  23.     if (cont != 'y') return;
  24.        
  25.     while(cont != 'n' && nextp < N)
  26.     {
  27.        
  28.         (*num_order)++;
  29.         printf("Enter order number: ");
  30.         scanf("\n%d", (buffer + nextp));
  31.         nextp++;
  32.         printf("More customers (y or n): ");
  33.         scanf("\n%c", &cont);
  34.         if (cont == 'n')
  35.         {
  36.             printf("\nStop serving customers right now. Passing orders to cooker:");
  37.             break;
  38.         }
  39.     }
  40.     printf("\nThere are total of %d order(s)\n", (*num_order));
  41.  
  42. }
  43.  
  44. void waiter_as_consumer()
  45. {
  46.     int max, order, i = 0;
  47.     max = *(num_order);
  48.    
  49.     if (max == 0)
  50.     {
  51.         printf("\nNo order made on shelf yet.");
  52.         return;
  53.     }
  54.     printf("\nHey, thanks cooker.");
  55.     printf("\nOkay. Let me deliver pizza to all customers right now.");
  56.     for(; i < max; i++)
  57.     {
  58.         order = *(buffer + i);
  59.         printf("\nPizza order #%d is ready.", order);
  60.     }
  61.     printf("\nI am done!");
  62. }
  63.  
  64. void cooker_as_consumer()
  65. {
  66.     int order, max;
  67.     nextc = 0;
  68.     max = *(num_order);
  69.  
  70.     if (max == 0)
  71.     {
  72.        printf("\nNo pizza order.");
  73.        return;
  74.     }
  75.     for(; nextc < max; nextc++)
  76.     {
  77.         order = *(buffer + nextc);
  78.         printf("\nRoger, waiter. I am processing order #%d", order);
  79.     }
  80.     printf("\nI finished cooking.");
  81. }
  82.  
  83. int main()
  84. {
  85.      int shmid, shmid_num;
  86.      int pid;
  87.      if((shmid=shmget(1000,BUFSIZE,IPC_CREAT | PERMS)) < 0)
  88.      {
  89.       printf("\n unable to create shared memory");
  90.       return;
  91.      }
  92.        
  93.      if((shmid_num=shmget(1001, BUFSIZE, IPC_CREAT | PERMS)) < 0)
  94.      {
  95.         printf("\n unable to create shared memory for num_order");
  96.         return;    
  97.      }
  98.  
  99.      if((buffer=(int*)shmat(shmid,(char*)0,0)) == (int*)-1)
  100.      {
  101.       printf("\n Shared memory allocation error\n");
  102.       exit(1);
  103.      }
  104.  
  105.      if((num_order=(int*)shmat(shmid_num, (char*)0,0)) == (int*)-1)
  106.      {
  107.         printf("\n Shared memory allocation for num_order encountered error\n");
  108.         exit(1);    
  109.      }
  110.  
  111.      if((mutex=semget(IPC_PRIVATE,1,PERMS | IPC_CREAT)) == -1)
  112.      {
  113.        printf("\n can't create mutex semaphore");
  114.        exit(1);
  115.      }
  116.  
  117.      if((emptyShelf=semget(IPC_PRIVATE,1,PERMS | IPC_CREAT)) == -1)
  118.      {
  119.       printf("\n can't create emptyShelf semaphore");
  120.       exit(1);
  121.      }
  122.  
  123.      if((orderOnShelf=semget(IPC_PRIVATE,1,PERMS | IPC_CREAT)) == -1)
  124.      {
  125.       printf("\ncan't create orderOnShelf semaphore");
  126.       exit(1);
  127.      }
  128.  
  129.      if((pizzaOnShelf=semget(IPC_PRIVATE, 1, PERMS | IPC_CREAT)) == -1)
  130.      {
  131.       printf("\ncan't create pizzaOnShelf semaphore");
  132.       exit(1);
  133.      }
  134.  
  135.      // Create the semaphore    
  136.      sem_create(mutex,1);
  137.      sem_create(emptyShelf,N);
  138.      sem_create(orderOnShelf,0);
  139.      sem_create(pizzaOnShelf,0);
  140.  
  141.  
  142.     //forking a child
  143.      if((pid=fork()) < 0)
  144.      {
  145.       printf("\n Error in process creation");
  146.       exit(1);
  147.      }
  148.  
  149.     //Producer process
  150.      if(pid > 0)
  151.      {
  152.         while(1)
  153.         {
  154.           P(emptyShelf); // waiter as P finds no items on shelf;
  155.           P(mutex); // has permission to use the shelf
  156.           waiter_as_producer();
  157.           V(mutex); // cooker now can use the shelf
  158.           V(orderOnShelf); // cooker now can pickup orders
  159.          
  160.           wait();
  161.           P(pizzaOnShelf);
  162.           P(mutex);
  163.           waiter_as_consumer();
  164.           V(mutex);
  165.           V(emptyShelf);
  166.         }
  167.      }
  168.         if(pid == 0)
  169.         {
  170.          while(1)
  171.         {
  172.          P(orderOnShelf); // make sure there is an order on shelf
  173.          P(mutex); //permission to work
  174.          cooker_as_consumer(); // take order and put pizza on shelf
  175.          V(mutex); //release permission
  176.          V(pizzaOnShelf); // pizza is now on shelf
  177.          wait();
  178.          wait();
  179.         }
  180.       }
  181. }
clone this paste RAW Paste Data