title: when i pass string from child process to parent process via shared memory
body: <p>i am trying to pass a string from a child process to parent process and the parent process has to capitalise  that string from parent process ,but when the parent process receives the string i get segmentation fault when i use strlen. It is like parent process doesnt recognize the string that both parent and child use.
  This is my program:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.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;unistd.h&gt;
  #include &lt;sys/wait.h&gt;
  
  #define SEM1KEY 1234
  #define SEM2KEY 4321
  #define SHM1KEY 5678
  #define SHM2KEY 8765
  #define SHM1SIZE sizeof(struct in_ds)
  #define SHM2SIZE sizeof(struct out_ds)
  
  typedef struct in_ds{
      pid_t pid;
      char *line;
  }in_ds;
  
  typedef struct out_ds{
      pid_t pid;
      char *cap_line;
  }out_ds;
  
  union semun{
      int val;
      struct semid_ds *buff;
      unsigned short *array;
  };
  
  int main(int argc, char *argv[])
  {
      int n=5,k=5;
  
      int semid[2], shmid[2];
      int i, j, result, pid_match=0, overall_pid_match=0;
      union semun arg;
      struct sembuf oparray[2] = {{0,-1,0},{0,1,0}};
      char buffer[]="abcde", *c_line;
      pid_t pid;
      in_ds *inds;
      out_ds *outds;
      FILE *fp;
  ////////////////////shm//////////////////
  
      shmid[0] = shmget((key_t)1234,SHM1SIZE,IPC_CREAT|0600);
      if(shmid[0]==-1)
      {
          perror("shmget failed!");
          return -1;
      }
  
      shmid[1] = shmget((key_t)4321,SHM2SIZE,IPC_CREAT|0600);
      if(shmid[1]==-1)
      {
          perror("shmget failed!");
          return -1;
      }
  
      inds = (in_ds*)shmat(shmid[0],(in_ds*)0,0);
      outds = (out_ds*)shmat(shmid[1],(out_ds*)0,0);
      if(inds==NULL || outds==NULL)
      {
          printf("shmat failed!");
          return -1;
      }
      printf("ok1");
  ////////////////////sem///////////////////
  
      semid[0] = semget(SEM1KEY,1,IPC_CREAT|0600);
      if(semid[0]==-1)
      {
          perror("semget failed!");
          return -1;
      }
  
      semid[1] = semget(SEM2KEY,1,IPC_CREAT|0600);
      if(semid[1]==-1)
      {
          perror("semget failed!");
          return -1;
      }
  
      arg.val = 0;
      if(semctl(semid[0],0,SETVAL,arg)==-1)
      {
          perror("semaphore initialization failed!");
          return -1;
      }
      arg.val = 1;
      if(semctl(semid[1],0,SETVAL,arg)==-1)
      {
          perror("semaphore initialization failed!");
          return -1;
      }
  
  
      outds-&gt;pid = 0;
      inds-&gt;pid = 0;
  
      for(i=0; i&lt;n; i++)
      {
          pid = fork();
          if(pid&gt;0)
          {
              continue;       //parent
          }else if(pid == 0){
              break;          //child
          }else{
              printf("fork failed!");     //ERROR
              return -1;
          }
      }
  
      if(pid&gt;0)           //C process
      {
          for(i=0; i&lt;k+1; i++)
          {
              semop(semid[1],&amp;oparray[0],1);      //down(sem2)
  
              if(inds-&gt;pid!=0)
              {
                  c_line = malloc((strlen(inds-&gt;line)+1)*sizeof(char));       
                  strcpy(c_line,inds-&gt;line);
                  free(inds-&gt;line);
  
                  j=0;
                  while(c_line[j]!='\0')
                  {
                      c_line[j] += -32;
                      j++;
                  }
  
  
                  outds-&gt;pid = inds-&gt;pid;
                  outds-&gt;cap_line = malloc((strlen(c_line)+1)*sizeof(char));
                  strcpy(outds-&gt;cap_line,c_line);
                  free(c_line);
  
              }
  
              semop(semid[0],&amp;oparray[1],1);      //up(sem1)
          }
      }else{
          fp = fopen("textfile","r");
          for(i=0; i&lt;k; i++)
          {       
              printf("ok2");
              semop(semid[0],&amp;oparray[0],1);      //down(sem1)
  
              if(outds-&gt;pid == 0)
              {}          
              else if(outds-&gt;pid == -1)
              {
                  fclose(fp);
                  exit(pid_match);
              }else{
                  if(outds-&gt;pid == getpid())pid_match++;
                  printf("Line:%s , pid:%d, mypid:%d",outds-&gt;cap_line,outds-&gt;pid,getpid());
                  free(outds-&gt;cap_line);
              }
  
  
              inds-&gt;line = malloc((strlen(buffer)+1)*sizeof(char));
              strcpy(inds-&gt;line,buffer);
              inds-&gt;pid = getpid();
              semop(semid[1],&amp;oparray[1],1);
          }
      }
  
  
      semctl(semid[0],0,IPC_RMID,0);
      semctl(semid[1],0,IPC_RMID,0);
  
      shmdt(inds);
      shmdt(outds);
  
      shmctl(shmid[0],IPC_RMID,NULL);
      shmctl(shmid[1],IPC_RMID,NULL); 
  }
  
  when i debug i see:
  
    Program received signal SIGSEGV, Segmentation fault.
    strlen () at ../sysdeps/x86_64/strlen.S:106
    106   ../sysdeps/x86_64/strlen.S: No such file or directory.
    c_line = malloc((strlen(inds-&gt;line)+1)*sizeof(char));
  
  this is the second time parent process is running
  and it gets a string which is inds-&gt;line
  </code></pre>