cmkl_assignments/fall-2024/sys-arch/sys-102/00040/source/interrupt.c

53 lines
778 B
C
Raw Normal View History

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