title: Semaphore not working properly
body: <p>I am working on an assignment where we need to use semaphores in order to make the second print of the parent process wait until child executes first. It is first time using semaphores and I certainly understood how they work, however I think I have a problem with the initialising of sem_open(). </p>
  
  <p>By following this:</p>
  
  <pre><code>sem_t *sem_open(const char *name, int oflag);
  </code></pre>
  
  <p>I have created this:</p>
  
  <pre><code>sem_t *sem = sem_open("MYSEM", O_CREAT , 2);
  </code></pre>
  
  <p>However, when executing my sem_wait are ignored.This is my whole program:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;semaphore.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/wait.h&gt;
  
  
  /* void ChildProcess(void)  ChildProcess prototype */
  /* void ParentProcess(void)  ParentProcess prototype */
  
  int main(int argc, char ** argv){
  
  int pid;
  pid = fork();
  
  sem_t *sem = sem_open("MYSEM", O_CREAT , 2);
  
  if (pid&lt;0)
  {
      printf("Cannot create a child process");
      exit(EXIT_FAILURE);
  }
  else if (pid==0)
  {
      printf("I am the child process. \n");
      printf("The child process is done. \n");
      sem_post(sem);
      exit(EXIT_SUCCESS);
  }
      else
  {
      printf("I am the parent process. \n");
      sem_wait(sem);
      printf("The parent process is done. \n");
  }
  sem_destroy(sem);
  exit (EXIT_SUCCESS);
  }
  </code></pre>
  
  <p>and what is printing is:</p>
  
  <pre><code>I am the parent process. 
  The parent process is done. 
  I am the child process. 
  The child process is done.
  </code></pre>
  
  <p>and what should print is this:</p>
  
  <pre><code>I am the parent process.
  I am the child process. 
  The child process is done.
  The parent process is done. 
  </code></pre>