kaburen

Untitled

Jan 30th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <limits.h>
  5. #include <errno.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10.  
  11.  
  12. #define FIFO_NAME "/tmp/moje_fifo5"
  13.  
  14. #define BUFFER_SIZE PIPE_BUF
  15. int main(int argc , char *argv[])
  16. {
  17.   int res;
  18.   int pipe_fd;
  19.   int open_mode=O_RDONLY;
  20.   char bufor[BUFFER_SIZE+1];
  21.   int zczytane=0;
  22.   char buf[20];
  23.   int a,b;
  24.   short int status = 0;
  25.  
  26.   memset(bufor,'\0',sizeof(bufor));
  27.  
  28.   if(access(FIFO_NAME, F_OK)==-1)
  29.   {
  30.  
  31.     res=mkfifo(FIFO_NAME,0666);
  32.  
  33.     if(res ==0) printf("Kolejka FIFO utworzona\n");
  34.     else
  35.     {
  36.       printf("B??d %d - %s tworzenia kolejki %s\n",errno,strerror(errno),FIFO_NAME);
  37.       exit(1);
  38.     }
  39.   }
  40.   printf("Proces %d otwiera FIFO \n",getpid());
  41.  
  42.  
  43.   pipe_fd=open(FIFO_NAME,open_mode);
  44.   printf("proces %d warto?c %d\n", getpid(),pipe_fd);
  45.  
  46.   sprintf(buf,"ls -l /proc/%d/fd",getpid());
  47.   system(buf);
  48.    
  49.   if(pipe_fd!=-1)
  50.   {
  51.     do{
  52.       res=read(pipe_fd,bufor,BUFFER_SIZE);
  53.      
  54.       zczytane+=res;
  55.       if(status == 0){
  56.     a = atoi(bufor);
  57.     status = 1;
  58.     printf("zawartosc : %s\n",bufor);
  59.       }
  60.       else{
  61.     b = atoi(bufor);
  62.     status = 0;
  63.     printf("zawartosc : %s\n",bufor);
  64.     printf("Suma = %d \n", a+b);
  65.       }
  66.      
  67.       write(pipe_fd, bufor,1024);
  68.      
  69.     }while(res>0);
  70.    
  71.     close(pipe_fd);
  72.   }
  73.   else
  74.     exit(1);
  75.    
  76.   printf("proces %d zko?czony zczytano %d\n", getpid(),zczytane);
  77.  
  78.  
  79. return 0;
  80. }
  81.  
  82. =====================================================================================
  83. #include <stdio.h>
  84. #include <unistd.h>
  85. #include <sys/types.h>
  86. #include <limits.h>
  87. #include <errno.h>
  88. #include <sys/stat.h>
  89. #include <fcntl.h>
  90. #include <string.h>
  91.  
  92.  
  93. #define FIFO_NAME "/tmp/moje_fifo5"
  94. #define BUFFER_SIZE PIPE_BUF
  95. #define TEN_MEG 1024*1024
  96.  
  97. int main(int argc , char *argv[])
  98. {
  99.   int res;
  100.   int pipe_fd;
  101.   int open_mode=O_WRONLY;
  102.   char bufor[BUFFER_SIZE+1];
  103.   int bytes_send=0;
  104.  
  105.   memset(bufor,'\0',sizeof(bufor));
  106.  
  107.  // strcpy(bufor,"ala ma");
  108.  
  109.   printf("%s\n",bufor);
  110.  
  111.  
  112.   if(access(FIFO_NAME, F_OK)==-1)
  113.   {
  114.  
  115.     res=mkfifo(FIFO_NAME,0666);
  116.  
  117.     if(res ==0) printf("Kolejka FIFO utworzona\n");
  118.     else
  119.     {
  120.       printf("Blad %d - %s tworzenia kolejki %s\n",errno,strerror(errno),FIFO_NAME);
  121.     }
  122.   }
  123.   printf("Proces %d otwiera FIFO\n",getpid());
  124.  
  125.  
  126.   pipe_fd=open(FIFO_NAME,open_mode);
  127.   printf("proces %d wartosc %d\n", getpid(),pipe_fd);
  128.  
  129.  
  130.   if(pipe_fd!=-1)
  131.   {
  132.    while(bytes_send<TEN_MEG)
  133.     {
  134.       printf("Podaj a:\n");
  135.       fgets(bufor,BUFFER_SIZE, stdin);
  136.      
  137.       res=write(pipe_fd,bufor,BUFFER_SIZE);
  138.        printf("nadano %d  - bufor :%s\n",res, bufor);
  139.        
  140.       printf("Podaj b:\n");
  141.       fgets(bufor,BUFFER_SIZE, stdin);;
  142.      
  143.      
  144.       printf("bufor:%s\n",bufor);
  145.      
  146.       res=write(pipe_fd,bufor,BUFFER_SIZE);
  147.        printf("nadano %d  - bufor :%s\n",res, bufor);
  148.      
  149.       if(res==-1)
  150.       {
  151.     fprintf(stderr,"Blad zapisu do kolejki FIFO %d %s",errno, strerror(errno));
  152.       }
  153.       bytes_send+=res;
  154.      
  155.     }
  156.     close(pipe_fd);
  157.   }
  158.   else
  159.     ;
  160.    
  161.  
  162.  
  163.  
  164.   printf("proces %d zkonczony nadano %d\n", getpid(),bytes_send);
  165.  
  166.  
  167. return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment