Advertisement
J3st3rs_j0k3

pr7

Dec 16th, 2023
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4. #include <unistd.h>
  5.  
  6. void sighup_handler(int signum) {
  7.     FILE *file = fopen("/user/home/prg.log", "a+");
  8.     if (file == NULL) {
  9.         file = fopen("/user/home/prg.log", "w");
  10.         fprintf(file, "1\n");
  11.     } else {
  12.         int num;
  13.         fscanf(file, "%d", &num);
  14.         fclose(file);
  15.         file = fopen("/user/home/prg.log", "w");
  16.         fprintf(file, "%d\n", num + 1);
  17.     }
  18.     fclose(file);
  19. }
  20.  
  21. void sigusr1_handler(int signum) {
  22.     FILE *file = fopen("/user/home/prg.log", "r");
  23.     if (file != NULL) {
  24.         int num;
  25.         fscanf(file, "%d", &num);
  26.         printf("Current content of prg.log: %d\n", num);
  27.         fclose(file);
  28.     } else {
  29.         printf("File prg.log does not exist\n");
  30.     }
  31. }
  32.  
  33. void sigterm_handler(int signum) {
  34.     remove("/user/home/prg.log");
  35.     exit(0);
  36. }
  37.  
  38. int main() {
  39.     signal(SIGHUP, sighup_handler);
  40.     signal(SIGUSR1, sigusr1_handler);
  41.     signal(SIGTERM, sigterm_handler);
  42.  
  43.     while (1) {
  44.         sleep(1);
  45.     }
  46.  
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement