title: share memory lock in linux (c lang)
body: <p>I have problem with locking shared memory.  This the code:</p>
  
  <pre><code>if(shmctl(id,SHM_LOCK,&amp;sgmlock)==FAILED)
  exit_failed("shmctl failed");
  printf("%d got the lock\n", getpid()) ;
  
  if(arr[CHLD3_ID_SET]==1){
    arr[CHLD3_ID_SET]++;
    puts("2a");
  }
  else if(arr[CHLD3_ID_SET]==2)
  {
    puts("2b");
  
    arr[CHLD3_ID]=EMPTY;
    arr[CHLD3_ID_SET]=EMPTY;
  }
  
  printf("%d got  unlocked\n", getpid()) ;
  
  if(shmctl(id,SHM_UNLOCK,&amp;sgmlock)==FAILED)
  exit_failed("shmctl failed");
  </code></pre>
  
  <p>So the first how get in increase the arr[CHLD3_ID_SET]
  and the second reset the cell in the array.</p>
  
  <p>the 2 process are fork of the father(and use the same shared memory)
  if the program is not stack in this section there is no problem</p>
  
  <p>this is the output for the code:</p>
  
  <pre><code>10938 got the lock
  10939 got the lock
  2a
  10938 got unlocked
  3
  2b
  10939 got unlocked
  3
  </code></pre>
  
  <p>(3 is print out of the lock section.)</p>
  
  <p>so we can see that one lock,and the secend can get into the lock section.
  what i doing wrong?</p>
  
  <p>thank you!!</p>