SHARE
TWEET

zad2 syg

LazySenpai Nov 29th, 2019 (edited) 140 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //------------------------------------------PISARZ-----------------------------------------------------
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <signal.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9.  
  10. int main() {
  11.     pid_t pid=fork();
  12.     if(pid == 0) {
  13.         system("gcc -o czytelnik zad1_czytelnik.c");
  14.         execl("czytelnik", NULL);
  15.     }
  16.     else {
  17.         sleep(1);
  18.         char buf[100];
  19.         int output = open("bufor", O_WRONLY | O_CREAT, 0777);
  20.         FILE *input = fopen("/etc/passwd", "r");
  21.         while(fgets(buf, 100, input)) {
  22.             write(output, buf, 100);
  23.             memset(buf,0,100);
  24.             kill(pid, SIGUSR1);
  25.             usleep(1000);
  26.             lseek(output, 0L, 0);
  27.         }
  28.         close(output);
  29.         fclose(input);
  30.         remove("bufor");
  31.         kill(pid, SIGUSR2);
  32.     }
  33.     return 0;
  34. }
  35.  
  36. //------------------------------------------CZYTELNIK-----------------------------------------------------
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <signal.h>
  40. #include <sys/stat.h>
  41. #include <fcntl.h>
  42. #include <string.h>
  43. #include <unistd.h>
  44. int line=1;
  45.  
  46. void signal_handler() {
  47.     int input=open("bufor", O_RDONLY);
  48.     char buf[100];
  49.     printf("%d ", line);
  50.     line++;
  51.     read(input, buf, 100);
  52.     printf("%s", buf);
  53.     fflush(stdout);
  54.     close(input);
  55. }
  56.  
  57. void end_work(){
  58.     exit(0);
  59. }
  60.  
  61. int main() {
  62.     signal(SIGUSR1, signal_handler);
  63.     signal(SIGUSR2, end_work);
  64.     for(;;);
  65. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top