title: Using shared memory between two programs
body: <p>I'm having some issues with shared memory. I want to pass a value into shared memory from program a that will be messed with and put back into that shared memory from program b, that program one can then read. As of now I can only get it to show me the value that I put in. I don't know if its the timing that needs to be corrected between reading from the shared memory and writing to it or if I have the wrong idea in general. Any help would be greatly appreciated.</p>
  
  <p>When I run the programs, the command line looks like: ./a 2 3 4 ./b</p>
  
  <p>It then asks for a value: 3</p>
  
  <p>output: 3</p>
  
  <p>Its supposed to raise 3 to some powers and multiply by those coefficients in the command line.</p>
  
  <p>Program a:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/ipc.h&gt;
  #include &lt;sys/shm.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(int ac, char** a)
  {
    key_t key=22;
    int size=1, shmflg=0;
    int id=0,ok=0,i=0, count=0;
    int *shmptr;
    int *pid;
    int status;
    double sum, value;
    char shmstr[10];
    char istr[10];   /* index for shared memory*/
    char arg1[10];
    char arg2[10];
    char power[10];
  
    count =(ac-2);
    size = count*sizeof(int);
    pid = (int*) malloc(size);
    shmflg = IPC_CREAT | SHM_R | SHM_W;
    id=shmget(key,size,shmflg);
    printf("%d get shmget\n",id);
    sprintf(shmstr, "%d",id);
    shmptr = shmat(id,0,0);
    for (i=0; (i &lt; count); i=i+1){
       shmptr[i]=0;
    }  
    for (i=0; (i &lt; count); i=i+1){
      if ((pid[i]=fork()) == 0) {
        sprintf(istr, "%d",ok);
        sprintf(power, "%d",((ac-2)-i));
        execlp(a[(ac-1)], a[(ac-1)], shmstr, istr,a[i+1],power, NULL);
      }
    }
  
  /* /////////////////////////////////// I believe this is were the issue lies.*/
    printf("value?");
    scanf("%lf",&amp;value);
    while(value!=0){
      sum=1;
      shmptr[ok]=value;``
      sum=shmptr[(ok+1)];
      printf("and the value is now %f \n", sum);
      printf("value?\n");
      scanf("%lf",&amp;value);
    }
  
    for (i=0; (i &lt; count); i=i+1){
        ok=waitpid(pid[i],&amp;status,0);
        printf("%d wait on %d ok\n",ok, i);
      }
  
    ok=shmdt(shmptr);
    ok = shmctl(id,IPC_RMID,0);
  
  }
  </code></pre>
  
  <p>Program b:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/shm.h&gt;
  
  
  int main(int ac, char** a)
  {
    int id=0,ok=0, i=0, j=0, power;
    int *shmptr;
    double x, mult, mycontrib, total;
    id = atoi(a[1]);
    i = atoi(a[2]);
    mult=atof(a[3]);
    power=atoi(a[4]);
    shmptr = shmat(id,0,0);
    x=shmptr[i];
    printf("child %d shmptr index value %d\n",i,shmptr[i]);
    while(x!=0){
      mycontrib=1;
      for(j=0;(j&lt;power);j=j+1){
        mycontrib=mycontrib*x;
      }
  
      total=total+(mycontrib*mult);
      shmptr[i]=total;
    }
     ok=shmdt(shmptr);
     printf("%d child detach ok\n",ok);
  }
  </code></pre>