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

View File

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