Modified code and motified report

This commit is contained in:
Win 2024-11-18 23:37:15 +07:00
parent 1247e0f9fc
commit 96b6b1769b
6 changed files with 35 additions and 18 deletions

View File

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

View File

@ -1,16 +1,23 @@
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <time.h>
int main()
{
clock_t start, end;
double cpu_time_used;
// Set start to current CPU time
start = clock();
int c;
int flag = 0;
int laps = 0;
/*
THIS IS A REPLACEMENT FOR THE _KBHIT FUNCTION
*/
// These structs are for saving tty parameters
static struct termios old_term, new_term;
@ -26,9 +33,12 @@ int main()
tcsetattr(STDIN_FILENO, TCSANOW, &new_term);
// Get character input by user
while ((c = getchar()) != EOF)
while ((c = getchar()) != EOF && laps < 10)
{
// Flip the flag
flag = !flag;
laps++;
printf("\n");
printf("Flag: %d\n", flag);
}
@ -37,5 +47,10 @@ int main()
// Set tty parameters back to old tty parameters
tcsetattr(STDIN_FILENO, TCSANOW, &old_term);
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("CPU Time: %f\n", cpu_time_used);
return 0;
}

View File

@ -1 +0,0 @@
,winsdominoes,fedora,18.11.2024 11:44,file:///home/winsdominoes/.var/app/org.libreoffice.LibreOffice/config/libreoffice/4;