stuk06

Untitled

Jun 3rd, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <fcntl.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5.  
  6. int main(void)
  7. {
  8.     /* Proces 1 - pobiera dane z STDIN (deskryptor 0) i zapisuje w pliku */
  9.     if(fork())
  10.     {
  11.         int  des;
  12.         int n;
  13.         char buf[20];
  14.         char buf1[20];
  15.  
  16.         des = open( "/tmp/file.txt", O_CREAT | O_RDWR | O_APPEND,  0666 );
  17.  
  18.         while(((n = read(0, buf, sizeof(buf))) > 0))
  19.         {                      
  20.             //printf("Wczytano: %d\n",n);
  21.             write( des, buf, strlen( buf ) );
  22.             sprintf(buf1,"%x",*buf);
  23.             //write(1,buf1,sizeof(buf1));
  24.             printf("Otrzymałem: %s\n",buf);
  25.             write(des,buf,sizeof(buf));
  26.             bzero(buf,sizeof(buf1));
  27.             close(des);
  28.         }
  29.         /* Proces 2 - odczytuje z pliku i przekazuje dane za pomocą FIFO do procesu 3 */
  30.         if(fork())
  31.         {
  32.             while(((n = read(des, buf, sizeof(buf))) > 0))
  33.             {                      
  34.                  bzero(buf,sizeof(buf));
  35.                  des=open("/tmp/file.txt",O_RDONLY);
  36.                  read(des,buf,sizeof(buf1));
  37.                  //sprintf(buf1,"%x",*buf);
  38.                  printf("Otrzymałem: %s\n",buf);
  39.                  //write(2,buf1,3);
  40.                  close(des);
  41.             }
  42.             /* Proces 3 - wypisuje otrzymane dane na STDOUT (deskryptor 1) */
  43.             if(fork())
  44.             {
  45.                 ;
  46.             }
  47.         }
  48.         close(des);
  49.     }
  50.  
  51.     sleep(100);
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment