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

Write a C program in Linux to implement the following functionalities Declare a

ID: 3780421 • Letter: W

Question

Write a C program in Linux to implement the following functionalities

Declare a global variable usr_interrupt=1

  Write a signal handler function

void reset_signalvaluezero ()

When a SIGALRM signal arrives this function will be called in which variable will be set

sig_atomic_t usr_interrupt = 0;

Write a signal handler function

void Increment_signalvalue ()

When a SIGINT signal arrives this function will be called in which variable will be incremented the usr_interrupt by 1.

Write a main function

int main (void)

Handle two Signals SIGALRM and SIGINT.

Set the alarm for 10 sec

Parent process will sit in tight loop by keeping check on usr_interrupt variable once this variable will be 5 parent process will print some ENDING message and will exit.

Explanation / Answer

#include<stdio.h>
#include<signal.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

sig_atomic_t usr_interrupt = 1;

void reset_signalvaluezero(int signo)
{
if (signo == SIGALRM)
    usr_interrupt = 0;
}

void Increment_signalvalue(int signo)
{
if (signo == SIGINT)
    usr_interrupt++;
}

int main(void)
{
if (signal(SIGINT, Increment_signalvalue) == SIG_ERR)
      printf(" can't catch SIGINT ");
if (signal(SIGALRM, reset_signalvaluezero) == SIG_ERR)
      printf(" can't catch SIGALRM ");
  

alarm(10);
  
  
while(1)
{
    if (usr_interrupt >= 5)
    {
        printf ("Exiting...");
        return 0;
    }
}
  
return 0;
}

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