1

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.

Application1.c

#include <stdio.h> #include <sys/shm.h> #include <sys/stat.h> 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, &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; } 

process1.c

#include <stdio.h> #include <sys/shm.h> #include <sys/stat.h> 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; } 

output:

shared memory attached at address 0x7f616e4f2000 segment size: 25600 shared memory22 attached at address 0x7f8746d17000 

The output is not printing data in the shared memory. I want the output to print "hello, world".

Thank you

3
  • Why do you want to use shared memory as IPC. That's horribly hard work. The alternatives are orders of magnitude easier. Commented Aug 24, 2011 at 18:40
  • oh really. This is my first time doing program on IPC and I have to cover all IPC's that is the work assigned to me by my TL. Commented Aug 24, 2011 at 19:14
  • @DavidHeffernan alternatives? pipe, fifo, socket? Commented Aug 16, 2020 at 4:53

1 Answer 1

1

A couple of things:

1) The first argument to shmget is the key. You're using IPC_PRIVATE in both processes, which means that it'll allocate a "new" piece of shared memory in both processes. What you want to do is to make arrangements so that both processes use the same key, but not the IPC_PRIVATE key.

2) The shared_memory pointer in both process DO NOT need to be the same value for things to work. Yes, the memory is shared, but that doesn't mean that the pointers will have the same value. The shared memory can be mapped to different memory locations in each process.

Sign up to request clarification or add additional context in comments.

4 Comments

how to initialize shm_key. int shm_key = 10 and can i replace IPC_PRIVATE with shm_key. is this correct way.
You can do that. IPC_CREAT | IPC_EXCL will cause shmget to fail if that key is already in use though. You should remove IPC_EXCL from process1.c. linux.die.net/man/2/shmget Also, this number is global to your entire machine, so something else on your machine might already be using shm_key = 10. Because of this you'll want to handle shmget failing (if it returns -1).
i have initialized it as key_t shm_key and i replaced IPC_PRIVATE with shm_key. I have also removed IPC_CREATE | IPC_EXCL from flags. Then the program worked. Now it prints the output as "hello, world". Thank you Erik.
@Erik I am having the same issue, how to determine unique key and share that with unrelated processes. Using pid of creator is a good idea normally but how to share that with other processes is confusing. We can write to tmp file but I am not sure whether it is a good idea. What do you think?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.