Advertisement
EBobkunov

ex1,2 w6

Oct 16th, 2023 (edited)
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.40 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <fcntl.h>
  7.  
  8. void sigusr1_handler(int signum) {
  9.     // Read and print the contents of text.txt
  10.     char buffer[1024];
  11.     int file_descriptor = open("text.txt", O_RDONLY);
  12.     ssize_t bytes_read;
  13.     while ((bytes_read = read(file_descriptor, buffer, sizeof(buffer))) > 0) {
  14.         write(STDOUT_FILENO, buffer, bytes_read);
  15.     }
  16.     close(file_descriptor);
  17. }
  18.  
  19. void sigusr2_handler(int signum) {
  20.     // Print a termination message and exit
  21.     printf("Process terminating...\n");
  22.     exit(0);
  23. }
  24.  
  25. void sigterm_handler(int signum) {
  26.     // Print a termination message and exit
  27.     printf("Process terminating...\n");
  28.     exit(0);
  29. }
  30.  
  31. int main() {
  32.     // Create the agent.pid file and write the PID into it
  33.     FILE* pid_file = fopen("/var/run/agent.pid", "w");
  34.     if (pid_file == NULL) {
  35.         perror("Error creating agent.pid file");
  36.         exit(1);
  37.     }
  38.     fprintf(pid_file, "%d", getpid());
  39.     fclose(pid_file);
  40.  
  41.     // Register signal handlers for SIGUSR1, SIGUSR2, and SIGTERM
  42.     signal(SIGUSR1, sigusr1_handler);
  43.     signal(SIGUSR2, sigusr2_handler);
  44.     signal(SIGTERM, sigterm_handler);
  45.  
  46.     // Read and print the initial contents of text.txt
  47.     char buffer[1024];
  48.     int file_descriptor = open("text.txt", O_RDONLY);
  49.     ssize_t bytes_read;
  50.     while ((bytes_read = read(file_descriptor, buffer, sizeof(buffer))) > 0) {
  51.         write(STDOUT_FILENO, buffer, bytes_read);
  52.     }
  53.     close(file_descriptor);
  54.  
  55.     // Sleep indefinitely
  56.     while (1) {
  57.         sleep(3600);  // Sleep for a long time (1 hour)
  58.     }
  59.  
  60.     return 0;
  61. }
  62.  
  63.  
  64. #include <stdio.h>
  65. #include <stdlib.h>
  66. #include <signal.h>
  67. #include <unistd.h>
  68. #include <fcntl.h>
  69. #include <sys/types.h>
  70. #include <sys/stat.h>
  71.  
  72. int agent_pid;
  73.  
  74. void sigint_handler(int signum) {
  75.     // Send SIGTERM to the agent and exit
  76.     if (agent_pid > 0) {
  77.         kill(agent_pid, SIGTERM);
  78.     }
  79.     exit(0);
  80. }
  81.  
  82. int main() {
  83.     // Check if the agent.pid file exists
  84.     struct stat stat_buffer;
  85.     if (stat("/var/run/agent.pid", &stat_buffer) == -1) {
  86.         perror("Error: No agent found.");
  87.         exit(1);
  88.     }
  89.  
  90.     // Try to open the agent.pid file to get the agent's PID
  91.     FILE* pid_file = fopen("/var/run/agent.pid", "r");
  92.     if (pid_file == NULL) {
  93.         perror("Error reading agent PID.");
  94.         exit(1);
  95.     }
  96.     fscanf(pid_file, "%d", &agent_pid);
  97.     fclose(pid_file);
  98.  
  99.     printf("Agent found.\n");
  100.  
  101.     // Register the Ctrl+C signal handler
  102.     signal(SIGINT, sigint_handler);
  103.  
  104.     while (1) {
  105.         printf("Choose a command {\"read\", \"exit\", \"stop\", \"continue\"}: ");
  106.         char command[20];
  107.         scanf("%s", command);
  108.  
  109.         if (strcmp(command, "read") == 0) {
  110.             // Send SIGUSR1 to the agent
  111.             kill(agent_pid, SIGUSR1);
  112.         } else if (strcmp(command, "exit") == 0) {
  113.             // Send SIGUSR2 to the agent and exit
  114.             kill(agent_pid, SIGUSR2);
  115.             exit(0);
  116.         } else if (strcmp(command, "stop") == 0) {
  117.             // Send SIGSTOP to the agent
  118.             kill(agent_pid, SIGSTOP);
  119.         } else if (strcmp(command, "continue") == 0) {
  120.             // Send SIGCONT to the agent
  121.             kill(agent_pid, SIGCONT);
  122.         } else {
  123.             printf("Invalid command.\n");
  124.         }
  125.     }
  126.  
  127.     return 0;
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement