2024-11-18 00:09:05 +07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2024-11-18 23:37:15 +07:00
|
|
|
// Declare Variables
|
2024-11-18 00:09:05 +07:00
|
|
|
int flag = 0;
|
2024-11-18 23:37:15 +07:00
|
|
|
int lap = 0;
|
|
|
|
|
2024-11-18 00:09:05 +07:00
|
|
|
// Handling signal function
|
|
|
|
void handle_signal(int signal)
|
|
|
|
{
|
|
|
|
// If signal is SIGINT
|
2024-11-18 23:37:15 +07:00
|
|
|
if (signal == SIGINT)
|
2024-11-18 00:09:05 +07:00
|
|
|
{
|
|
|
|
// Set flag to its complement
|
|
|
|
// Print the flag value
|
|
|
|
flag = !flag;
|
2024-11-18 23:37:15 +07:00
|
|
|
lap++;
|
2024-11-18 00:09:05 +07:00
|
|
|
printf("Flag: %d\n", flag);
|
|
|
|
}
|
|
|
|
|
2024-11-18 23:37:15 +07:00
|
|
|
if (signal == SIGQUIT)
|
2024-11-18 00:09:05 +07:00
|
|
|
{
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2024-11-18 23:37:15 +07:00
|
|
|
clock_t start, end;
|
|
|
|
double cpu_time_used;
|
|
|
|
|
|
|
|
start = clock();
|
|
|
|
|
2024-11-18 00:09:05 +07:00
|
|
|
// Handling signal
|
|
|
|
signal(SIGINT, handle_signal);
|
2024-11-18 23:37:15 +07:00
|
|
|
signal(SIGQUIT, handle_signal);
|
2024-11-18 00:09:05 +07:00
|
|
|
|
|
|
|
// Let the program run until interrupt
|
2024-11-18 23:37:15 +07:00
|
|
|
while (lap < 10)
|
2024-11-18 00:09:05 +07:00
|
|
|
{
|
|
|
|
sleep(1);
|
2024-11-18 23:37:15 +07:00
|
|
|
}
|
2024-11-18 00:09:05 +07:00
|
|
|
|
2024-11-18 23:37:15 +07:00
|
|
|
end = clock();
|
|
|
|
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
|
2024-11-18 00:09:05 +07:00
|
|
|
|
2024-11-18 23:37:15 +07:00
|
|
|
printf("CPU Time: %f\n", cpu_time_used);
|
2024-11-18 00:09:05 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|