Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kill signal example

Tags:

c

unix

kill

signals

I'm trying this example that I took from: http://www.cs.cf.ac.uk/Dave/C/node24.html:

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

void sighup(); /* routines child will call upon sigtrap */
void sigint();
void sigquit();

main() { 
 int pid;

 /* get child process */

 if ((pid = fork()) < 0) {
    perror("fork");
    exit(1);
 }

if (pid == 0) { /* child */
   printf("\nI am the new child!\n\n");
       signal(SIGHUP,sighup); /* set function calls */
       signal(SIGINT,sigint);
       signal(SIGQUIT, sigquit);
       printf("\nChild going to loop...\n\n");
      for(;;); /* loop for ever */
 }
else /* parent */
 {  /* pid hold id of child */
   printf("\nPARENT: sending SIGHUP\n\n");
   kill(pid,SIGHUP);
   sleep(3); /* pause for 3 secs */
   printf("\nPARENT: sending SIGINT\n\n");
   kill(pid,SIGINT);
   sleep(3); /* pause for 3 secs */
   printf("\nPARENT: sending SIGQUIT\n\n");
   kill(pid,SIGQUIT);
   sleep(3);
 }
}

void sighup() {
   signal(SIGHUP,sighup); /* reset signal */
   printf("CHILD: I have received a SIGHUP\n");
}

void sigint() {
    signal(SIGINT,sigint); /* reset signal */
    printf("CHILD: I have received a SIGINT\n");
}

void sigquit() {
  printf("My DADDY has Killed me!!!\n");
  exit(0);
}

But I do not see any output from the child process.

Is it the expected behaviour? If so; why?

Thank you very much!

like image 601
Christian Wagner Avatar asked Jun 09 '11 01:06

Christian Wagner


People also ask

What are kill signals?

Kill signals allow interaction between different processes. Concretely signals are event notifications sent to processes mostly to interrupt, terminate, kill or suspend processes (That's why we use the term “kill”).

Which signal is sent by kill?

Description. kill ends a process by sending it a signal. The default signal is SIGTERM. kill is a built-in shell command.

What signal is kill 9?

“ kill -9” command sends a kill signal to terminate any process immediately when attached with a PID or a processname. It is a forceful way to kill/terminate a or set of processes. “ kill -9 <pid> / <processname>” sends SIGKILL (9) — Kill signal. This signal cannot be handled (caught), ignored or blocked.


1 Answers

Your code has a major race condition. You do not ensure that the child has finished calling signal before the parent sends the signals. You either need to use some kind of synchronization primitive to make the parent wait for the child to install the handlers, or you need to install the signal handlers before forking so the child inherits them.

Here's the easiest way I know to synchronize processes like this:

  1. Before forking, call pipe(p) to create a pipe.
  2. fork().
  3. In the parent, close(p[1]); (the writing end) and read(p[0], &dummy, 1);
  4. In the child, close(p[0]); and close(p[1]); after installing the signal handlers.
  5. When the parent returns from read, you can be sure the child has setup its signal handlers. You can also close(p[0]); in the parent at this point.

Edit 2: Perhaps a better and easier approach:

  1. Before forking, call sigprocmask to block all signals and save the old signal mask.
  2. In the parent, call sigprocmask again right after forking to restore the original signal mask.
  3. In the child, call sigprocmask right after installing the signal handlers to restore the original signal mask.
like image 75
R.. GitHub STOP HELPING ICE Avatar answered Sep 28 '22 14:09

R.. GitHub STOP HELPING ICE