Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <unistd.h>
- #define LOG_FILE "/user/home/prg.log"
- int hup_count = 0;
- void sighup_handler(int signum) {
- hup_count++;
- FILE *file = fopen(LOG_FILE, "a");
- if (file == NULL) {
- perror("Error opening file");
- exit(EXIT_FAILURE);
- }
- fprintf(file, "%d\n", hup_count);
- fclose(file);
- }
- void sigusr1_handler(int signum) {
- FILE *file = fopen(LOG_FILE, "r");
- if (file == NULL) {
- perror("Error opening file");
- exit(EXIT_FAILURE);
- }
- int value;
- while (fscanf(file, "%d", &value) != EOF) {
- printf("%d\n", value);
- }
- fclose(file);
- }
- void sigterm_handler(int signum) {
- if (remove(LOG_FILE) == -1) {
- perror("Error removing file");
- exit(EXIT_FAILURE);
- }
- exit(EXIT_SUCCESS);
- }
- int main() {
- signal(SIGHUP, sighup_handler);
- signal(SIGUSR1, sigusr1_handler);
- signal(SIGTERM, sigterm_handler);
- while (1) {
- sleep(1);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement