title: What happens when two or more philosophers check the mutex to be 1 and at the same time down the mutex and enter test function
body: <pre><code>#define N 5 /* number of philosophers */
  #define LEFT (i + N−1) % N /* number of i’s left neighbor */
  #define RIGHT (i + 1) % N /* number of i’s right neighbor */
  #define THINKING 0 /* philosopher is thinking */
  #define HUNGRY 1 /* philosopher is trying to get for ks */
  #define EATING 2 /* philosopher is eating */
  
  typedef int semaphore; /* semaphores are a special kind of int */
  
  int state[N]; /* array to keep track of everyone’s state */
  semaphore mutex = 1; /* mutual exclusion for critical regions */
  semaphore s[N]; /* one semaphore per philosopher */
  
  void philosopher(int i) /* i: philosopher number, from 0 to N−1 */
  {
      while (TRUE) /* repeat forever */
      {
          think(); /* philosopher is thinking */
          take forks(i); /* acquire two for ks or block */
  
          eat(); /* yum-yum, spaghetti */
          put forks(i); /* put both for ks back on table */
      }
  }
  
  void take forks(int i) /* i: philosopher number, from 0 to N−1 */
  {
      down(&amp;mutex); /* enter critical region */
      state[i] = HUNGRY; /* record fact that philosopher i is hungry */
      test(i); /* tr y to acquire 2 for ks */
      up(&amp;mutex); /* exit critical region */
      down(&amp;s[i]); /* block if for ks were not acquired */
  }
  
  void put forks(i) /* i: philosopher number, from 0 to N−1 */
  {
      down(&amp;mutex); /* enter critical region */
      state[i] = THINKING; /* philosopher has finished eating */
      test(LEFT); /* see if left neighbor can now eat */
      test(RIGHT); /* see if right neighbor can now eat */
      up(&amp;mutex); /* exit critical region */
  }
  
  void test(i) /* i: philosopher number, from 0 to N−1 */
  {
      if (state[i] == HUNGRY &amp;&amp; state[LEFT] != EATING &amp;&amp; state[RIGHT] != EATING)
      {
          state[i] = EATING;
          up(&amp;s[i]);
      }
  }
  </code></pre>
  
  <p>In this code, as you may see, we have a mutex that is initially one which means that no philosopher is testing whether the forks are free or not. What happens when two or more philosophers check the mutex at the same time and happen to see that the mutex is one and both at the same time down the mutex and enter the function to test the whether forks are free or not? Can this happen or not is my question?</p>