Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to pass data using shared memory and save the received data to file
    primarykey
    data
    text
    <p>I am new to linux environment. I just know the basics of C. I am trying to learn linux programming. For this I am trying an example on shared memory. Please someone help me with this example. I am trying to send person details (like name, phone number &amp; address) to another process using Shared memory. After receiving the data by the second process, I am trying to save received data into a file. This is the task I am doing. I am able to send just the name and receive it in the second process. Can someone please help how to send the data(like name, phone number &amp; address) to second process and in the second process it must print the data and it should save the data to a file. Here is my code:</p> <pre><code>address.c char *shared_memory; int main() { int select; int segment_id; char* shared_memory; int segment_size; key_t shm_key; const int shared_segment_size = 0x6500; shm_key = ftok("/home/madan/programs/shm_tok",'C'); if(shm_key &lt; 0) { printf("failed to create the key %s\n",strerror(errno)); } /* Allocate a shared memory segment. */ segment_id = shmget (shm_key, shared_segment_size, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); if(segment_id &lt; 0) { printf("error geting the segment id %s\n",strerror(errno)); } printf("segment ID:%d\n", segment_id); /* Attach the shared memory segment. */ shared_memory = (char*) shmat (segment_id, 0, 0); printf ("shared memory attached at address %p\n", shared_memory); /* I want to send these details to the shared memory. Can someone suggest me the correct way to send these details to shared memory so that second process can retrieve them*/ sprintf(shared_memory, "maddy\n"); sprintf(shared_memory, "767556686"); sprintf(shared_memory, "Ontario"); system("./address-insert"); /* Detach the shared memory segment. */ shmdt (shared_memory); / * Deallocate the shared memory segment.*/ shmctl (segment_id, IPC_RMID, 0); } </code></pre> <hr> <pre><code>addres-insert.c int main () { int segment_id; char* shared_memory; FILE *fp; char *name; int segment_size; key_t shm_key; shm_key = ftok("/home/madan/programs/shm_tok",'C'); const int shared_segment_size = 0x6500; /* Allocate a shared memory segment. */ segment_id = shmget (shm_key, shared_segment_size, S_IRUSR | S_IWUSR); if(segment_id &lt; 0) { printf("error:[%s]",strerror(errno)); } printf("segment id %d\n",segment_id); /* Attach the shared memory segment. */ shared_memory = (char*) shmat (segment_id, 0, 0); if(shared_memory == NULL) { printf("failed to attach the shared memory %s",strerror(errno)); } printf ("shared memory2 attached at address %p\n", shared_memory); /* printing the data from shared memory send by first process*/ printf ("name=%s\n", shared_memory); /*copying the data in shared memory so i can save them to a file*/ strcpy(name, shared_memory); printf("%s", name); /*here i have to save the data to a file. But i don't know how to do it, can someone help me with this please*/ /* Detach the shared memory segment. */ shmdt (shared_memory); return 0; } </code></pre>
    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.
    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