Tobiahao

S01_PLIKI_10

Jan 20th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.15 KB | None | 0 0
  1. /*
  2. Napisz dwa programy: pierwszy będzie generował losowe liczby i zapisywał je porcjami do pliku, drugi działając współbieżnie z pierwszym będzie odczytywał je i szukał wartości maksymalnej. Do blokowania fragmentów pliku należy użyć funkcji fcntl().
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11. #include <wait.h>
  12. #include <time.h>
  13.  
  14. #define PATHNAME "10_file"
  15. #define HOW_MANY 6
  16. #define HOW_MUCH 5
  17. #define DELAY 500000
  18.  
  19. void write_function(int fd)
  20. {
  21.     struct flock file_lock;
  22.     int numbers[HOW_MUCH];
  23.  
  24.     for(int i = 0; i < HOW_MANY; i++) {
  25.         file_lock.l_type = F_WRLCK;
  26.         file_lock.l_whence = SEEK_CUR;
  27.         file_lock.l_start = 0;
  28.         file_lock.l_len = sizeof(int) * HOW_MUCH;
  29.    
  30.         for(int j = 0; j < HOW_MUCH; j++)
  31.             numbers[j] = (rand() % 100) + 1;
  32.        
  33.         if(fcntl(fd, F_SETLKW, &file_lock) == -1) {
  34.             perror("fcntl");
  35.             exit(EXIT_FAILURE);
  36.         }
  37.        
  38.         if(write(fd, numbers, sizeof(int)*HOW_MUCH) == -1) {
  39.             perror("write");
  40.             exit(EXIT_FAILURE);
  41.         }
  42.        
  43.         file_lock.l_type = F_UNLCK;
  44.         if(fcntl(fd, F_SETLK, &file_lock) == -1) {
  45.             perror("fcntl");
  46.             exit(EXIT_FAILURE);
  47.         }
  48.    
  49.         lseek(fd, 0, SEEK_SET);
  50.         printf("Proces 1: Zapisano %d liczb\n", HOW_MUCH);
  51.         usleep(DELAY);
  52.     }
  53. }
  54.  
  55. void read_function(int fd)
  56. {
  57.     int numbers[HOW_MUCH];
  58.  
  59.     lseek(fd, 0, SEEK_SET);
  60.  
  61.     for(int i = 0; i < HOW_MANY; i++) {
  62.         if(read(fd, numbers, sizeof(int)*HOW_MUCH) == -1) {
  63.             perror("read");
  64.             exit(EXIT_FAILURE);
  65.         }
  66.        
  67.         printf("Proces 2: Odczytano liczby: ");
  68.         for(int j = 0; j < HOW_MUCH; j++)
  69.             printf("%d ", numbers[j]);
  70.         printf("\n");
  71.        
  72.         lseek(fd, 0, SEEK_SET);
  73.         usleep(DELAY);
  74.     }
  75. }
  76.  
  77. int main(void)
  78. {
  79.     pid_t pid;
  80.     int fd;
  81.  
  82.     srand(time(NULL));
  83.  
  84.     if((fd = open(PATHNAME, O_RDWR | O_CREAT | O_TRUNC, 0700)) == -1) {
  85.         perror("open");
  86.         return EXIT_FAILURE;
  87.     }
  88.  
  89.     pid = fork();
  90.     if(pid == -1) {
  91.         perror("fork");
  92.         return EXIT_FAILURE;
  93.     }
  94.     else if(pid == 0) {
  95.         read_function(fd);
  96.     }
  97.     else {
  98.         write_function(fd);
  99.     }
  100.  
  101.     if(close(fd) == -1) {
  102.         perror("close");
  103.         return EXIT_FAILURE;
  104.     }
  105.  
  106.     return EXIT_SUCCESS;
  107. }
Add Comment
Please, Sign In to add comment