Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Linux / C Handling SIGALRM PLEASE LOOK AT MY CODE AT THE BOTTOM, USE THAT TEMPLA

ID: 3850433 • Letter: L

Question

Linux / C Handling SIGALRM

PLEASE LOOK AT MY CODE AT THE BOTTOM, USE THAT TEMPLATE. I NEED HELP FINISHING IT

#include <signal.h>

volatile sig_atomic_t print_flag = false;

void handler_alarm(int sig) {
print_flag = true;
}

int main() {
signal(SIGALARM,handler);
alarm(1);

// infinite for loop:
// if statement to check print flag
// print
// change print flag to false
// call alarm function

exit(EXIT_SUCCESS)
}

Write a function for handling SIGALRM: According to this link: https://www.secure coding.cert.org/confluence/display/c/SIG31-C.+Do+ not+access shared objects in signal+handlers ccessing or mod can leave data in an inconsistent state. The two exceptions (C Standard, 5.1.2.3, paragraph 5) to this rule are the ability to read from and write to lock-free atomic objects and variables of type "volatile sig atomic-t". Accessing any other type of object from a signal handler is undefined behavior. Therefore, globally define and initialize a flag with type volatile sig atomic-t use a system call to handle signal SIGALIM with the function that you have already defined. use a system call to set an alarm clock for delivery of SIGALRM every one second. use the defined flag and the alarm clock to print the message: Step 1/10 step 2/10 step 2/10

Explanation / Answer

#include <signal.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>

volatile sig_atomic_t print_flag = false;

void handle_alarm( int sig ) {
print_flag = true;
if(print_flag)
printf("In signal handler: print flag is true ");
}

int main() {
signal( SIGALRM, handle_alarm ); // Install handler first,
alarm( 1 ); // call alarm before infinite loop

for (;;) {
if ( print_flag ) {
printf( "print_flag is true " );
print_flag = false;
//again call alarm in loop
alarm( 1 );
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote