Thread: Trying to make a signal handler to ignore signals

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    4

    Trying to make a signal handler to ignore signals

    i wrote a code to handle when CTRL C presses and to ignore CTRL Z signal
    i am trying to run a donothing.c file from within my code but it does not run
    donothing.c literally is just an infinite loop printing nothing

    Trying that to see if after exec it does go back to default handler from parent

    here is my code

    Code:
    #include <signal.h>
    #include <stdio.h>
    #include <unistd.h>
    
    
    
    
    void sigHandler(int sig)
    {
    signal(SIGINT, sigHandler);
    printf("CTRL C does not work\n");
    }
    int main(int argc, char *argv[]){ 
    
    
    
    
    int pid; 
    pid = fork();
    
    
    
    
    
    
    if(pid == 0){
    char* args[2] ;
    args[0] = "./donothing";
    args[1] = 0;
    execvp(args[0], args);
    //execl("donothing", NULL);
    
    
    }else if(pid > 0){
    
    
    signal(SIGINT, sigHandler);
    kill(pid, SIGINT);
    }
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,541
    Nothing happens because you kill the child immediately.

    Oh, and your indentation needs work.
    Code:
    #include <signal.h>
    #include <stdio.h>
    #include <unistd.h>
    
    void sigHandler(int sig)
    {
      signal(SIGINT, sigHandler);
      // don't use printf here
      // see http://man7.org/linux/man-pages/man7/signal-safety.7.html
      printf("CTRL C does not work\n");
    }
    
    int main(int argc, char *argv[])
    {
      int pid;
    
      pid = fork();
      if (pid == 0) {
        char *args[2];
        args[0] = "./donothing";
        args[1] = 0;
        execvp(args[0], args);
      } else if (pid > 0) {
        signal(SIGINT, sigHandler);  // useless
        kill(pid, SIGINT);  // immediate infanticide
      }
    
      return 0;
    }
    Your signal hander only has scope in this process, but you never invoke it in this process.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. popen: safe to call in signal handler?
    By Elkvis in forum Linux Programming
    Replies: 10
    Last Post: 05-03-2012, 12:31 PM
  2. signal handler does not print
    By barramundi9 in forum C Programming
    Replies: 4
    Last Post: 07-27-2011, 11:20 PM
  3. Signal handler for assert()
    By jeffcobb in forum Linux Programming
    Replies: 17
    Last Post: 06-10-2010, 08:36 PM
  4. using a signal handler to kill processes
    By dinjas in forum C Programming
    Replies: 2
    Last Post: 03-16-2005, 12:58 PM
  5. signal handler
    By falconetti in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 07:54 PM

Tags for this Thread