title: Semget and fork(): no such file or directory
body: <p>I have a problem with accessing semaphore in child process. I can't get already created semaphore through semget() when in child process.</p>
  
  <p>Here is make_semaphores() function;</p>
  
  <pre><code>int make_semaphores(key_t key)
  { 
  int semid;
  semid = semget(key, 3, IPC_CREAT|/*IPC_EXCL|*/0666);
  if(semid == -1)
  {
      perror("Creating an array of semaphores");
      exit(1);
  }
  if(semctl(semid,0,SETVAL, (int)MAX)==-1)
  {
      perror("Initializing 'empty' semaphore");
      exit(1);
  }
  if(semctl(semid,1,SETVAL, (int)0)==-1)
  {
      perror("Initializing 'full' semaphore");
      exit(1);
  }
  if(semctl(semid,2,SETVAL, (int)1)==-1)
  {
      perror("Initializing mutex");
      exit(1);
  }
  return semid;
  }
  </code></pre>
  
  <p>Here is allocate_memory function:</p>
  
  <pre><code>int* allocate_memory(int *buf, key_t key)
  {
  int shmid;
  shmid=shmget(key,(MAX+1)*sizeof(int),IPC_CREAT|/*IPC_EXCL|*/0666);
  if(shmid==-1)
  {
      perror("Creating shared memory segment");
      exit(1);
  }
  buf=(int*)shmat(shmid,NULL,0);
  if(buf==NULL)
  {
      perror("Including shared memory segment");
      exit(1);
  }
  buf[0]=0;
  return buf;
  }
  </code></pre>
  
  <p>I searched and didn't find a satisfying answer, so sorry if I'm needlessly spamming. Thanks in advance for any kind of help.</p>
  
  <p>Here is full main:</p>
  
  <pre><code> #include &lt;stdio.h&gt;
   #include &lt;stdlib.h&gt;
   #include &lt;sys/types.h&gt;
   #include &lt;sys/ipc.h&gt;
   #include &lt;sys/sem.h&gt;
   #include &lt;sys/shm.h&gt;
   #include &lt;sys/wait.h&gt;
  
  #define S1KEY 467221
  #define S2KEY 379231
  #define S3KEY 217411
  #define MAX 10
  
  
  
  int main(int argc, char* argv[])
  {
  srand(time(NULL));
  int fork_id;
  int *buf1, *buf2, *buf3;
  int semid1, semid2, semid3, i, j, r, smd1;
  int prod_info[3]={0,0,0}, cons_info[3]={0,0,0};
  
  printf("-------------------\n");
  buf1=allocate_memory(buf1,S1KEY);
  buf2=allocate_memory(buf2,S2KEY);
  buf3=allocate_memory(buf3,S3KEY);
  printf("-------------------\n");
  semid1=make_semaphores(S1KEY);
  semid2=make_semaphores(S2KEY);
  semid3=make_semaphores(S3KEY);
  
  smd1=semget(S1KEY,3,0666);//here is okay
  if(semid1!=smd1)
  {
      printf("semid1: %d              smd1: %d\n",semid1,smd1);
      perror("S1KEY does not exist");
  }
  printf("----------------\n");
  for(i=0;i&lt;4;++i)
  {
      fork_id=fork();
      if(fork_id&lt;0)
      {
          perror("fork()");
          exit(1);
      }
      else if(fork_id==0)// one of four children
      {
          smd1=semget(S1KEY,3,0666);//here is NOT okay, smd returns -1
              if(smd1==-1)
              {
                  printf("[%d]semid1: %d              smd1: %d\n",i,semid1,smd1);
                  perror("S1KEY does not exist");
              }
          if(i==3) //producer for 3 buffers
          {
              for(j=0;j&lt;100;j++)
              {
                  r=(rand()%3)+1;
                  printf("-------------------------\n");
                  if(r==1)
                  {
                      printf("Prod:1||%d||%d\n",prod_info[0],getval_semaphores(semid1,0));
                      printf("        %d\n", buf1[0]);
                      produce(buf1,semid1,i);
                      prod_info[0]++;
                  }
                  else if(r==2)
                  {
                      printf("Prod:2||%d||%d\n",prod_info[1],getval_semaphores(semid2,0));
                      printf("        %d\n", buf2[0]);
                      produce(buf2,semid2,i);
                      prod_info[1]++;
                  }
                  else
                  {
                      printf("Prod:3||%d||%d\n",prod_info[2],getval_semaphores(semid3,0));
                      printf("        %d\n", buf3[0]);
                      produce(buf3,semid3,i);
                      prod_info[2]++;
                  }
              }
              printf("Produced: 1:%d 2:%d 3:%d\n", prod_info[0],prod_info[1],prod_info[2]);
              exit(1);
          }
          else if(i==2) //3 consumers for 3 buffers (1,2,3)
          {
                  for(j=0;j&lt;100;j++)
                  {
                      //usleep(1);
                      printf("Cons:3\n");
                      printf("%d\n", buf1[0]);
                      printf("%d\n", buf2[0]);
                      printf("%d\n", buf3[0]);
                      cons_info[0]=consume(buf1,semid1);
                      cons_info[1]=consume(buf2,semid2);
                      cons_info[2]=consume(buf3,semid3);
                      printf("Buf1: %d\n",cons_info[0]);
                      printf("Buf2: %d\n",cons_info[1]);
                      printf("Buf3: %d\n",cons_info[2]);
                  }
                  exit(1);
          }
          else if(i==1) //2 consumers for 2 buffers (2,3)
          {
                  for(j=0;j&lt;100;j++)
                  {
                      //usleep(1);
                      printf("Cons:2\n");
                      printf("%d\n", buf2[0]);
                      printf("%d\n", buf3[0]);
                      cons_info[1]=consume(buf2,semid2);
                      cons_info[2]=consume(buf3,semid3);
                      printf("Buf2: %d\n",cons_info[1]);
                      printf("Buf3: %d\n",cons_info[2]);
                  }
                  exit(1);
          }
          else if(i==0) //1 consumer for 1 buffer (3)
          {
                  for(j=0;j&lt;100;j++)
                  {
                      //usleep(1);
                      printf("Cons:1\n");
                      printf("%d\n", buf3[0]);
                      cons_info[2]=consume(buf3,semid3);
                      printf("Buf3: %d\n",cons_info[2]);
                  }
                  exit(1);
          }
      }
      else
      {
          if(i==4) //Parent, waits after making children
          {
              smd1=semget(S1KEY,3,0666);
              if(semid1!=smd1)
              {
                  printf("[%d]semid1: %d              smd1: %d\n",i,semid1,smd1);
                  perror("S1KEY does not exist");
              }
              printf("Parent\n");
              wait(NULL);
          }
      }
  }
  break_semaphores(semid1);
  break_semaphores(semid2);
  break_semaphores(semid3);
  return 0;
  }
  </code></pre>