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

50 lines
812 B
C
Raw Normal View History

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <time.h>
clock_t t;
t = clock();
int flag = 0;
// Handling signal function
void handle_signal(int signal)
{
// If signal is SIGINT
if (signal == SIGQUIT)
{
// Set flag to its complement
// Print the flag value
flag = !flag;
printf("Flag: %d\n", flag);
}
if (signal == SIGINT)
{
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds
printf("CPU Time: %f\n", t);
_exit(0);
}
}
int main()
{
// Handling signal
signal(SIGQUIT, handle_signal);
signal(SIGINT, handle_signal);
// Let the program run until interrupt
while (1)
{
sleep(1);
}
//t = clock() - t;
//double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds
printf("CPU Time: %f\n", t);
return 0;
}