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