title: Semaphore lock is not working properly in Problem
body: <p>I have assingment problem which states :</p>
  <p>Servers can be designed to limit the number of open
  connections. For example, a server may wish to have only N socket connections at any point
  in time. As soon as N connections are made, the server will not accept another incoming
  connection until an existing connection is released. Write a program using semaphores to
  synchronize server activity to limit the number of concurrent connections.</p>
  <p>To solve it ,, I have created 2 semaphore :</p>
  <ol>
  <li>a semaphore that is initialised to N.</li>
  <li>a sempaphore to provide mutual exclusion.</li>
  </ol>
  <p>I have to create in such a way  that if all N connection is made, client must wait till release is made.</p>
  <p>I have tried to code it, but code is not working properly :</p>
  <pre><code> #include&lt;stdio.h&gt;
   #include&lt;stdlib.h&gt;
   #include&lt;unistd.h&gt;
   #include&lt;pthread.h&gt;
   #include&lt;semaphore.h&gt;
   #include&lt;stdlib.h&gt; 
   #include&lt;string.h&gt;
  
  sem_t allow;
  sem_t mutex;
  
  void *acquire(void * arg)
  {
   int tid = *(int *)arg;
   tid+=1;
   sem_wait(&amp;mutex);
  
   sem_wait(&amp;allow);                       
  
  
  printf(&quot;\n Client %d is connected to the server....&quot;,tid);
  sem_post(&amp;mutex);
  sleep(rand()%10);
  release(tid);
  
  }
  void  release(int num)
  {
    printf(&quot;\n Client %d releases the Server ....&quot;,num);
    sem_post(&amp;allow);                       
  }   
  void main(int argc,char ** argv)
  {
  int n;
  int i;
  printf(&quot;\n Enter the number of client :&quot;);
  scanf(&quot;%d&quot;, &amp;n);
  int maxi=3;         // maximum no. of connection available
  sem_init(&amp;allow,0,maxi);  // semaphore that is initialised to maxi. 
                             //no. of connection
  sem_init(&amp;mutex,0,1);  // for mutual exclusion
  
  pthread_t thread[n];
  
  for(i=0;i&lt;n;i++) {
      pthread_create(&amp;thread[i],NULL,acquire,&amp;(i));   // Clients are 
                                                    // acquiring ..
  }
  
  for(i=0;i&lt;n;i++)
      pthread_join(thread[i],NULL);
  
  sem_destroy(&amp;allow);
  sem_destroy(&amp;mutex);
  </code></pre>
  <p>}</p>
  <p>Its giving different order of execution like , even before acquiring connection (client &quot;x&quot;) its releasing the connection like ..</p>
  <p>Output :</p>
  <blockquote>
  <p>Enter the number of client :6</p>
  <p>Client 3 is connected to the server....</p>
  <p>Client 3 is connected to the server....</p>
  <p>Client 4 is connected to the server....</p>
  <p>Client 3 releases the Server ....</p>
  <p>Client 6 is connected to the server....</p>
  <p>Client 3 releases the Server ....</p>
  <p>Client 6 is connected to the server....</p>
  <p>Client 4 releases the Server ....</p>
  <p>Client 1 is connected to the server....</p>
  <p>Client 6 releases the Server ....</p>
  <p>Client 6 releases the Server ....</p>
  <p>Client 1 releases the Server ....</p>
  </blockquote>
  <p>Please help me to correct the code !!</p>
  <p>And Sorry for bad title !!!</p>