Tobiahao

S01_PLIKI_04

Jan 20th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.16 KB | None | 0 0
  1. /*
  2. Zademonstruj działanie funkcji flock() na przykładzie, w którym kilka procesów współbieżnych będzie się ubiegało o możliwość zapisu lub odczytu z pliku.
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <sys/file.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. #include <wait.h>
  13. #include <time.h>
  14. #include <stdbool.h>
  15. #include <string.h>
  16.  
  17. #define FILENAME "04_file"
  18. #define NUMBER_OF_PROCESSES 5
  19.  
  20. const char *write_text = "Wiadomosc do zapisania\n";
  21.  
  22. void writer_function(int fd)
  23. {
  24.     if(flock(fd, LOCK_EX) == -1) {
  25.         perror("flock_ex");
  26.         exit(EXIT_FAILURE);
  27.     }
  28.  
  29.     if(write(fd, write_text, strlen(write_text)) == -1) {
  30.         perror("write");
  31.         exit(EXIT_FAILURE);
  32.     }
  33.     printf("    Zapisano do pliku!\n");
  34.  
  35.     if(flock(fd, LOCK_UN) == -1) {
  36.         perror("flock_un");
  37.         exit(EXIT_FAILURE);
  38.     }
  39. }
  40.  
  41. void reader_function(int fd)
  42. {
  43.     char read_buffer[16];
  44.     ssize_t read_status;
  45.  
  46.     if(flock(fd, LOCK_SH) == -1) {
  47.         perror("flock_sh");
  48.         exit(EXIT_FAILURE);
  49.     }
  50.  
  51.     printf("    Odczytano z pliku!\n");
  52.  
  53.     for(;;) {
  54.         read_status = read(fd, read_buffer, 16);
  55.         if(read_status == -1) {
  56.             perror("read");
  57.             exit(EXIT_FAILURE);
  58.         }
  59.         else if(read_status == 0){
  60.             break;
  61.         }
  62.         else
  63.             printf("    %s", read_buffer);
  64.     }
  65.  
  66.     if(flock(fd, LOCK_UN) == -1) {
  67.         perror("flock_un");
  68.         exit(EXIT_FAILURE);
  69.     }
  70. }
  71.  
  72. int main(void)
  73. {
  74.     int fd;
  75.     pid_t pid;
  76.     bool reader_writer;
  77.  
  78.     srand(time(NULL));
  79.  
  80.     if((fd = open(FILENAME, O_RDWR | O_TRUNC, 0600)) == -1) {
  81.         perror("open");
  82.         return EXIT_FAILURE;
  83.     }
  84.  
  85.     for(int i = 0; i < NUMBER_OF_PROCESSES; i++) {
  86.         reader_writer = rand() % 2;
  87.        
  88.         pid = fork();
  89.         if(pid == -1) {
  90.             perror("fork");
  91.             return EXIT_FAILURE;
  92.         }
  93.         else if(pid == 0) {
  94.             if(reader_writer){
  95.                 printf("Proces nr %d:\n", i);
  96.                 writer_function(fd);
  97.             }
  98.             else{
  99.                 printf("Proces nr %d:\n", i);
  100.                 reader_function(fd);
  101.             }
  102.             return EXIT_SUCCESS;
  103.         }
  104.     }
  105.  
  106.     for(int i = 0; i < NUMBER_OF_PROCESSES; i++) {
  107.         if(wait(NULL) == -1) {
  108.             perror("wait");
  109.             return EXIT_FAILURE;
  110.         }
  111.     }
  112.  
  113.     if(close(fd) == -1) {
  114.         perror("close");
  115.         return EXIT_FAILURE;
  116.     }
  117.  
  118.     return EXIT_SUCCESS;
  119. }
Add Comment
Please, Sign In to add comment