3

I want to start the calculator application from my code, interrupt it with sigint-2 shows that it has been interrupted, start it again, and then quit it with sigquit-9, the idea is to interrupt it within the C code so it is not necessary to press Ctrl+C or Ctrl+</kbd>

Write a C program that accepts the signals SIGINT and SIGQUIT via a signalfd file descriptor. The program terminates after accepting a SIGQUIT signal.

What is the syntax in C language to start a process, then interrupt it, then end it?

radrow
  • 6,419
  • 4
  • 26
  • 53
FlipFlopSquid
  • 99
  • 1
  • 4
  • 11

1 Answers1

1

I think this may be what you are looking for

//
//  main.c
//  Project 4
//
//  Found help with understanding and coding at
// http://www.thegeekstuff.com/2012/03/catch-signals-sample-c-code/
//

#include<stdio.h>
#include<signal.h>
#include<unistd.h>
//signal handling function that will except ctrl-\ and ctrl-c
void sig_handler(int signo)
{
    //looks for ctrl-c which has a value of 2
    if (signo == SIGINT)
        printf("\nreceived SIGINT\n");
    //looks for ctrl-\ which has a value of 9
    else if (signo == SIGQUIT)
        printf("\nreceived SIGQUIT\n");
}

int main(void)
{
    //these if statement catch errors
    if (signal(SIGINT, sig_handler) == SIG_ERR)
        printf("\ncan't catch SIGINT\n");
    if (signal(SIGQUIT, sig_handler) == SIG_ERR)
        printf("\ncan't catch SIGQUIT\n");
    //Runs the program infinitely so we can continue to input signals
    while(1)
        sleep(1);
    return 0;
}
FlipFlopSquid
  • 99
  • 1
  • 4
  • 11