SHARE
TWEET

zad1 syg

LazySenpai Nov 29th, 2019 (edited) 122 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. int line=1;
  9.  
  10. void signal_handler() {
  11.     int input=open("bufor", O_RDONLY);
  12.     char buf[100];
  13.     printf("%d ", line);
  14.     line++;
  15.     read(input, buf, 100);
  16.     printf("%s", buf);
  17.     fflush(stdout);
  18.     close(input);
  19. }
  20.  
  21. void end_work(){
  22.     exit(0);
  23. }
  24.  
  25. int main() {
  26.     pid_t pid=fork();
  27.     if(pid == 0) {
  28.         signal(SIGUSR1, signal_handler);
  29.         for(;;);
  30.     }
  31.     else {
  32.         sleep(1);
  33.         char buf[100];
  34.         int output = open("bufor", O_WRONLY | O_CREAT, 0777);
  35.         FILE *input = fopen("/etc/profile", "r");
  36.         while(fgets(buf, 100, input)) {
  37.             write(output, buf, 100);
  38.             memset(buf,0,100);
  39.             kill(pid, SIGUSR1);
  40.             usleep(1000);
  41.             lseek(output, 0L, 0);
  42.         }
  43.         close(output);
  44.         fclose(input);
  45.         remove("bufor");
  46.         kill(pid, SIGUSR2);
  47.     }
  48.     return 0;
  49. }
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