title: Why do I get an error "undefined reference to 'sem_init' " when I compile C code with shared memory
body: <p>I am a starter to work with shared memory and I have implemented a parallel adder in which each of the k processors is implemented as a child process. Specifically, given a set of n integers and value of k, the main program creates k child processes, assigns each child process to compute the total of its assigned ceiling of n/k numbers, waits for the sub-total from each of the k child processes, sum up the sub-totals, and print the result of each sub-total as well as the overall total. I have not use threads.</p>
  
  <p>This program is created for assignment in the college and they it is expected to run in any department computer. </p>
  
  <p>This code correctly compile on Kali Linux but I can not compile and run on other Linux versions. 
  When I tried to compile on Ubuntu it gives the error saying </p>
  
  <blockquote>
    <p>undefined reference  to 'sem_init'</p>
  </blockquote>
  
  <p>I used -lrt in the line of compiling. Please help me with this problem.</p>
  
  <p>This is the code that I created.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;sys/ipc.h&gt;
  #include&lt;sys/wait.h&gt;
  #include&lt;sys/stat.h&gt;
  #include&lt;sys/types.h&gt;
  #include&lt;fcntl.h&gt;
  #include&lt;errno.h&gt;
  #include&lt;semaphore.h&gt;
  #include&lt;unistd.h&gt;
  #include&lt;math.h&gt;
  #include&lt;stdlib.h&gt;
  
  #define BUFFER_SIZE 100
  #define BUFFER_SUB 2
  
  typedef struct
  {
      int bufMax;
      int datalimit;
      int buff[BUFFER_SIZE];
      sem_t mutex, empty, full;
  
  } shared_inputs;
  typedef struct
  {
          int sub[BUFFER_SUB];
          sem_t mutex,empty,full;
  }sub_tot;
  
  int main(int argc, char *argv[])
  {
      int x = 0;
      int data,count,i,j,tot;
      int n;
      int k =atoi(argv[2]);
      int assign_size;
      count = tot =0;
  
  
  
  
      int segment_id;
          size_t segment_size = sizeof(shared_inputs);
          segment_id = shmget(IPC_PRIVATE,segment_size,IPC_CREAT|0666);
          shared_inputs *shared_memory = shmat(segment_id,NULL,0);
  
      sem_init(&amp;shared_memory-&gt;mutex,1,1);
          sem_init(&amp;shared_memory-&gt;empty,1,BUFFER_SIZE);
          sem_init(&amp;shared_memory-&gt;full,1,0);
  
  
  
      int segment_idSubs;
          size_t segment_size1 = sizeof(sub_tot);
          segment_idSubs = shmget(IPC_PRIVATE,segment_size1,IPC_CREAT|0666);
          sub_tot *subtotal = shmat(segment_idSubs,NULL,0);
  
      sem_init(&amp;subtotal-&gt;mutex,1,1);
          sem_init(&amp;subtotal-&gt;empty,1,BUFFER_SUB);
          sem_init(&amp;subtotal-&gt;full,1,0);
  
      FILE *numFile;
      numFile = fopen(argv[1], "r");
  
  
  
      while(!feof(numFile))
          {
          fscanf(numFile,"%d",&amp;data);
                  sem_wait(&amp;shared_memory-&gt;empty);
                  sem_wait(&amp;shared_memory-&gt;mutex);
                  shared_memory-&gt;buff[x] = data;
                  sem_post(&amp;shared_memory-&gt;mutex);
                  sem_post(&amp;shared_memory-&gt;full);
          printf("%d ", shared_memory-&gt;buff[x]);
                  x++;
          n = x;
  
          }
      assign_size = ceil((double)n/(double)k);
      printf("\n");
      shared_memory-&gt;datalimit = 0;
      shared_memory-&gt;bufMax = n-1;
      printf("assigned size : %d \n", assign_size);
      printf("n : %d , k : %d \n",n,k);
  
  
      for(i =0; i &lt; k; i++)
      {
          int id;
          int subt = 0;
  
          id = fork();
          if(id &lt; 0) // error in fork
          {
              perror("Error in fork ");
              exit(300);
          }
          else if(id == 0)//the new child process
          {
  
  
              for(j=0;j&lt; assign_size; j++)//getting items from the shared memory
              {
  
                  sem_wait(&amp;shared_memory-&gt;full);
                                  sem_wait(&amp;shared_memory-&gt;mutex);
                                  int num = shared_memory-&gt;buff[shared_memory-&gt;datalimit];
                  //printf("%d \n",shared_memory-&gt;buff[shared_memory-&gt;datalimit]);
                  shared_memory-&gt;datalimit++;
                                  sem_post(&amp;shared_memory-&gt;mutex);
                                  sem_post(&amp;shared_memory-&gt;empty);
                                  subt = subt + num;
                  if(shared_memory-&gt;datalimit == shared_memory-&gt;bufMax)
                  {
                      break;
                  }
  
              }
  
              int pid = getpid();
                          sem_wait(&amp;subtotal-&gt;empty);
                          sem_wait(&amp;subtotal-&gt;mutex);
                          subtotal-&gt;sub[0] = pid;
                          subtotal-&gt;sub[1] = subt;
                          sem_post(&amp;subtotal-&gt;mutex);
                          sem_post(&amp;subtotal-&gt;full);
              printf("Sub-total produced by Processor with ID %d: %d \n",pid,subt);
  
              exit(0);
          }
          else//parent process
          {
              int status;
                          wait(&amp;status);
                          sem_wait(&amp;subtotal-&gt;full);
                          sem_wait(&amp;subtotal-&gt;mutex);
                          int sub = subtotal-&gt;sub[1];
                          sem_post(&amp;subtotal-&gt;mutex);
                          sem_post(&amp;subtotal-&gt;empty);
                          tot = tot+sub;
          }
      }
      printf("Total: %d \n",tot);
  
  
      return 0;
  
  }
  </code></pre>