title: How can a process kill itself?
body: <pre><code>#include&lt;stdlib.h&gt;
  #include&lt;unistd.h&gt;
  #include&lt;signal.h&gt;
  
  int main(){
  
      pid_t pid = fork();
  
      if(pid==0){
              system("watch ls");
      }
      else{
              sleep(5);
              killpg(getpid(),SIGTERM);  //to kill the complete process tree.
      }
      return 0;
  }
  </code></pre>
  
  <p>Terminal:</p>
  
  <pre><code>anirudh@anirudh-Aspire-5920:~/Desktop/testing$ gcc test.c
  anirudh@anirudh-Aspire-5920:~/Desktop/testing$ ./a.out
  Terminated
  </code></pre>
  
  <p><em>for the first 5 secs the output of the "watch ls" is shown and then it terminates because I send a SIGTERM.</em></p>
  
  <p><strong>Question:</strong> How can a process kills itself ? I have done kill(getpid(),SIGTERM);</p>
  
  <p><strong>My hypothesis:</strong>
  so during the kill() call the process switches to kernel mode. The kill call sends the SIGTERM to the process and copies it in the process's process table. when the process comes back to user mode it sees the signal in its table and it terminates itself (HOW ? I REALLY DO NOT KNOW )
  <em>(I think I am going wrong (may be a blunder) somewhere in my hypothesis ... so Please enlighten me)</em></p>
  
  <p>This code is actually a stub which I am using to test my other modules of the Project.
  Its doing the job for me and I am happy with it but there lies a question in my mind how actually a process kills itself.  I want to know the step by step hypothesis.</p>
  
  <p>Thanks in advance</p>
  
  <p>Anirudh Tomer</p>