title: static variables in multiple processes (Signals)
body: <p>I have 2 processes running test.c. There is a signal handler in test.c which executes an execlp. In test.c, I have a static variable which needs to be only initialized once, and incremented each time before the execlp call. When either process reaches 99, they exit.</p>
  
  <p>Unfortunately, right now, it's not getting incremented, my guess is because there are 2 processes that each have a copy of the static variable. Here is test.c:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;unistd.h&gt;
  
  static int i = 0;
  
  static int foo(int j)
  {
      printf("In the foo...\n");
      j++;
      printf("%d\n", j);
      return j;
  }
  
  
  void main(int argc, char *argv[])
  {
      int pid, pid2, k;
      int *h = malloc(sizeof(int));
      int g = 0;
      h = &amp;g;
  
      static char s[15];
  
  
      pid = fork();
      if (pid &gt; 0)
      {
          sleep(1);
      }
  
      if (pid == 0)
      {
          k = foo(*h);
  
          sprintf(s, "%d", k);
          if (k &gt;= 99)
          {
              printf("k=99\n");
              exit(0);        
          }
          execlp("./a.out", "forktest", s, NULL);
      }
  
      pid2 = fork();
  
      if (pid2 == 0)
      {
  
          k = foo(*h);
  
          sprintf(s, "%d", k);
          if (k &gt;= 99)
          {
              printf("k=99\n");
              exit(0);        
          }
          execlp("./a.out", "forktest", s, NULL);
  
      }
  
      wait(pid2);
      wait(pid);
  }
  </code></pre>
  
  <p>Can anyone please explain why there is an infinite loop? Why isn't the static variable get incremented?</p>
  
  <p>Thank you.</p>