title: Unable to run shared memory program
body: <p>I have written the below programs as shared memory example. I want to write some message from the shared memory created in the write.c file and want display it in the read.c process from the same memory. But when I try to run the programs, I am getting the error message: </p>
  
  <pre><code>Segmentation fault (core dumped)
  </code></pre>
  
  <p>Tried but unable to find the error in my code. </p>
  
  <p><strong>write.c file:</strong></p>
  
  <pre><code>#include &lt;sys/ipc.h&gt;
  #include &lt;sys/shm.h&gt;
  #include &lt;stdio.h&gt;
  
  main() {
      key_t key=1235;
      int shm_id;
      void *shm;
      char *message = "hello";
      shm_id = shmget(key,10*sizeof(char),IPC_CREAT);
      shm = shmat(shm_id,NULL,NULL);
      sprintf(shm,"%s",message);
  }
  </code></pre>
  
  <p><strong>read.c file:</strong></p>
  
  <pre><code>#include &lt;sys/ipc.h&gt;
  #include &lt;sys/shm.h&gt;
  #include &lt;stdio.h&gt;
  
  main() {
      key_t key=1235;
      int shm_id;
      void *shm;
      char *message;
      message = malloc(10*sizeof(char));
      shm_id = shmget(key,10*sizeof(char),NULL);
      shm = shmat(shm_id,NULL,NULL);
      if(shm == NULL)
      {
          printf("error");
      }
      sscanf(shm,"%s",message);
      printf("\n message = %s\n",message);
  }
  </code></pre>