title: Get the process name of pid
body: <p>Related:<br>
  <a href="https://stackoverflow.com/questions/15545341/process-name-from-its-pid-in-linux">Process name from its pid in linux</a><br>
  <a href="https://stackoverflow.com/questions/4974140/get-pid-of-the-process-which-has-triggered-some-signal">Get pid of the process which has triggered some signal</a></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  #include &lt;signal.h&gt;
  
  const char* get_process_name_by_pid(const int pid)
  {
      char* name = (char*)calloc(1024,sizeof(char));
      if(name){
          sprintf(name, "/proc/%d/cmdline",pid);
          FILE* f = fopen(name,"r");
          if(f){
              size_t size;
              size = fread(name, sizeof(char), 1024, f);
              if(size&gt;0){
                  if('\n'==name[size-1])
                      name[size-1]='\0';
              }
              fclose(f);
          }
      }
      return name;
  }
  
  
  
  static void my_handler(int signum, siginfo_t *siginfo, void *context) {
      printf("Got signal '%d' from process '%d' of user '%d' (%s)\n",
          signum, siginfo-&gt;si_pid, siginfo-&gt;si_uid, get_process_name_by_pid(siginfo-&gt;si_uid));
  }
  
  int main(void) {
      struct sigaction act;
      memset(&amp;act, '\0', sizeof(act));
      act.sa_sigaction = &amp;my_handler;
      act.sa_flags = SA_SIGINFO;
      sigaction(SIGUSR1, &amp;act, NULL);
      printf("Hi, my pid is %d\ntalk to me with 'kill -SIGUSR1 %d'\n", getpid(), getpid());
      while(1)
          sleep(1000);
      return 0;
  }
  </code></pre>
  
  <p>I am trying to catch who sent the signal to a program.  </p>
  
  <p>This successfully shows the pid of the signal sender but I would also like to know the process name. </p>
  
  <p>I've tried with the function get_process_name_by_pid() but it doesn seem to work<br>
  QUESTION: What can i do to see the process name?</p>
  
  <p>Im running oh RHEL6.6</p>