Tobiahao

S01_PLIKI_08

Jan 20th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.03 KB | None | 0 0
  1. /*
  2. Napisz program, w którym dwa procesy będą zapisywały do jednego pliku, a trzy będą odczytywały. Zastosuj zajmowanie zalecane do synchronizacji pracu procesów.
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <sys/file.h>
  10. #include <fcntl.h>
  11. #include <unistd.h>
  12. #include <wait.h>
  13. #include <string.h>
  14.  
  15. #define PATHNAME "08_file"
  16. #define BUFFER_SIZE 128
  17. #define NUMBER_OF_WRITERS 2
  18. #define NUMBER_OF_READERS 3
  19. #define MSG_TO_SAVE "Wiadomosc nr "
  20.  
  21. void error_exit(const char *message)
  22. {
  23.     perror("message");
  24.     exit(EXIT_SUCCESS);
  25. }
  26.  
  27. void write_to_file(int fd, const char *msg)
  28. {
  29.     if(flock(fd, LOCK_EX) == -1)
  30.         error_exit("flock_ex");
  31.  
  32.     if(write(fd, msg, strlen(msg)) == -1)
  33.         error_exit("write");
  34.     printf("Zapisano do pliku!\n");
  35.  
  36.     if(flock(fd, LOCK_UN) == -1)
  37.         error_exit("flock_un");
  38. }
  39.  
  40. void read_from_file(int fd)
  41. {
  42.     lseek(fd, 0, SEEK_SET);
  43.     ssize_t status;
  44.     char buffer[BUFFER_SIZE];
  45.  
  46.     if(flock(fd, LOCK_SH) == -1)
  47.         error_exit("flock_sh");
  48.  
  49.     printf("Odczytano z pliku:\n");
  50.     for(;;) {
  51.         memset(buffer, '\0', BUFFER_SIZE);
  52.         status = read(fd, buffer, BUFFER_SIZE-1);
  53.         if(status == -1)
  54.             error_exit("read");
  55.         else if(status == 0)
  56.             break;
  57.         else
  58.             printf("%s", buffer);
  59.     }
  60.  
  61.     if(flock(fd, LOCK_UN) == -1)
  62.         error_exit("flock_un");
  63. }
  64.  
  65. int main(void)
  66. {
  67.     pid_t pids;
  68.     int fd;
  69.     char write_buffer[strlen(MSG_TO_SAVE)+1];
  70.  
  71.     if((fd = open(PATHNAME, O_RDWR | O_CREAT | O_TRUNC, 0755)) == -1)
  72.         error_exit("open");
  73.  
  74.     for(int i = 0; i < NUMBER_OF_WRITERS + NUMBER_OF_READERS; i++) {
  75.         pids = fork();
  76.         if(pids == -1)
  77.             error_exit("fork");
  78.         else if(pids == 0){
  79.             printf("Proces nr: %d\n", i+1);
  80.             if(i < NUMBER_OF_WRITERS) {
  81.                 sprintf(write_buffer, "%s%d\n", MSG_TO_SAVE, i);
  82.                 write_to_file(fd, write_buffer);
  83.             }
  84.             else {
  85.                 read_from_file(fd);
  86.             }
  87.             return EXIT_SUCCESS;
  88.         }
  89.     }
  90.    
  91.     for(int i = 0; i < NUMBER_OF_WRITERS + NUMBER_OF_READERS; i++)
  92.         if(wait(NULL) == -1)
  93.             error_exit("wait");
  94.  
  95.  
  96.     return EXIT_SUCCESS;
  97. }
Add Comment
Please, Sign In to add comment