#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
volatile sig_atomic_t user_time = 0;
/* Handles Ctrl+C (SIGINT) */
void handle_sigint(int signal) {
printf("\nSIGINT received. Pausing for 5 seconds...\n");
sleep(5);
printf("Resuming execution.\n");
}
/* Logic executed by the child process */
void run_child_process(void) {
printf("Child process running. PID: %d\n", getpid());
sleep(2);
printf("Child process finished.\n");
}
int main(void) {
pid_t child_pid;
struct sigaction sa;
sa.sa_handler = handle_sigint;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if (sigaction(SIGINT, &sa, NULL) == -1) {
perror("Failed to set SIGINT handler");
return 1;
}
child_pid = fork();
if (child_pid < 0) {
perror("Failed to fork");
return 1;
}
if (child_pid == 0) {
run_child_process();
} else {
if (wait(NULL) == -1) {
perror("Failed to wait for child process");
return 1;
}
printf("Child process has terminated. Parent process exiting.\n");
}
return 0;
}