Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create a new process and communicate with that using shared memory
    primarykey
    data
    text
    <p>Hi i am trying to create an application1 which takes input data "hello world". I am creating a new process using system() and I want to access data of application1 in this process using shared memory(interprocess communication). I tried to run this program but couldn't get the output "hello world". How to attach the shared memory in application1 and process1 to the same address location. please help me with this.</p> <p><strong>Application1.c</strong></p> <pre><code>#include &lt;stdio.h&gt; #include &lt;sys/shm.h&gt; #include &lt;sys/stat.h&gt; int main () { int segment_id; char* shared_memory; struct shmid_ds shmbuffer; int segment_size; const int shared_segment_size = 0x6400; /* Allocate a shared memory segment. */ segment_id = shmget (IPC_PRIVATE, shared_segment_size, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); /* Attach the shared memory segment. */ shared_memory = (char*) shmat (segment_id, 0, 0); printf ("shared memory attached at address %p\n", shared_memory); /* Determine the segment’s size. */ shmctl (segment_id, IPC_STAT, &amp;shmbuffer); segment_size = shmbuffer.shm_segsz; printf ("segment size: %d\n", segment_size); /* Write a string to the shared memory segment. */ sprintf (shared_memory, "Hello, world."); /* Detach the shared memory segment. */ system("./process1"); shmdt (shared_memory); shmctl (segment_id, IPC_RMID, 0); return 0; } </code></pre> <p><strong>process1.c</strong></p> <pre><code>#include &lt;stdio.h&gt; #include &lt;sys/shm.h&gt; #include &lt;sys/stat.h&gt; int main () { int segment_id; char* shared_memory; struct shmid_ds shmbuffer; int segment_size; const int shared_segment_size = 0x6400; /* Allocate a shared memory segment. */ segment_id = shmget (IPC_PRIVATE, shared_segment_size, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); /* Attach the shared memory segment. */ shared_memory = (char*) shmat (segment_id, 0, 0); printf ("shared memory2 attached at address %p\n", shared_memory); printf ("%s\n", shared_memory); /* Detach the shared memory segment. */ shmdt (shared_memory); return 0; } </code></pre> <p>output:</p> <pre><code>shared memory attached at address 0x7f616e4f2000 segment size: 25600 shared memory22 attached at address 0x7f8746d17000 </code></pre> <p>The output is not printing data in the shared memory. I want the output to print "hello, world".</p> <p>Thank you</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload