Advertisement
Guest User

jjj

a guest
Nov 25th, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/file.h>
  5. #include <sys/times.h>
  6. #include <semaphore.h>
  7. #define SIZE 9
  8.  
  9.  
  10.  
  11. int main()
  12. {
  13.     int potoki[2];
  14.     int pipe_result;
  15.     int bajty;
  16.     char c;
  17.     pid_t pid;
  18.     int i;
  19.  
  20.     sem_t * emptyy;
  21.     sem_t * full;
  22.     sem_t * prod;
  23.     sem_t * consread;
  24.     sem_t * isreaden;
  25.     sem_t * cons;
  26.    
  27.     emptyy= sem_open( "/emptyy", O_CREAT);
  28.     sem_init( emptyy, 1, 9);
  29.  
  30.     full=sem_open("/full", O_CREAT);
  31.     sem_init( full, 1, 0 );
  32.  
  33.     prod=sem_open( "/producer", O_CREAT);
  34.     sem_init( prod, 1, 1 );
  35.  
  36.     consread=sem_open("/consread", O_CREAT);
  37.     sem_init( consread, 1, 1 );
  38.  
  39.     isreaden=sem_open( "/isreaden", O_CREAT);
  40.     sem_init( isreaden, 1, 1 );
  41.  
  42.     cons=sem_open( "/consumer", O_CREAT);
  43.     sem_init( cons, 1 , 2 );
  44.  
  45.     pipe_result=pipe(potoki);
  46.     if(pipe_result!=0)
  47.     {
  48.         exit(EXIT_FAILURE);
  49.     }
  50.     pid = fork();
  51.     if(pid==-1)
  52.     {
  53.         exit(EXIT_FAILURE);
  54.     }
  55.     pid = fork();
  56.     srand( time( NULL ) );
  57.     if(pid==-1)
  58.     {
  59.         exit(EXIT_FAILURE);
  60.     }
  61.     if(pid==0)//producent
  62.     {
  63.         for(i=0; i<50; i++)
  64.         {
  65.             printf("dupa1");
  66.             //produkuje
  67.             c=65+(rand()%24);
  68.             sem_wait( emptyy);
  69.             sem_wait( prod);
  70.             bajty = write(potoki[1], &c, 1);
  71.             sem_post( full);
  72.             sem_post(prod);
  73.  
  74.             usleep((rand()%20)*100);
  75.         }
  76.         exit(EXIT_SUCCESS);
  77.     }
  78.     else//czytelnik
  79.     {
  80.  
  81.  
  82.         for(i=0; i<50; i++)
  83.         {
  84.             printf("dupa1");
  85.             sem_wait( cons);
  86.             sem_wait( full);
  87.             sem_wait( consread);
  88.             if(sem_trywait( cons)==0 || sem_trywait( isreaden)!=0)
  89.             {
  90.                 bajty = read(potoki[0], &c, 1);
  91.                 sem_post( emptyy);
  92.                 sem_post( isreaden);
  93.             }
  94.             else
  95.             {
  96.                 bajty= pread(potoki[0], &c, 1, 0);
  97.                 sem_post( full);
  98.  
  99.             }
  100.             sem_post(cons);
  101.             usleep((rand()%20)*100);
  102.         }
  103.         exit(EXIT_SUCCESS);
  104.     }
  105.     sem_destroy(emptyy);
  106.     sem_destroy(full);
  107.     sem_destroy(prod);
  108.     sem_destroy(cons);
  109.     sem_destroy(consread);
  110.     sem_destroy(isreaden);
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement