How to use shared memory with Linux in C How to use shared memory with Linux in C linux linux

How to use shared memory with Linux in C


There are two approaches: shmget and mmap. I'll talk about mmap, since it's more modern and flexible, but you can take a look at man shmget (or this tutorial) if you'd rather use the old-style tools.

The mmap() function can be used to allocate memory buffers with highly customizable parameters to control access and permissions, and to back them with file-system storage if necessary.

The following function creates an in-memory buffer that a process can share with its children:

#include <stdio.h>#include <stdlib.h>#include <sys/mman.h>void* create_shared_memory(size_t size) {  // Our memory buffer will be readable and writable:  int protection = PROT_READ | PROT_WRITE;  // The buffer will be shared (meaning other processes can access it), but  // anonymous (meaning third-party processes cannot obtain an address for it),  // so only this process and its children will be able to use it:  int visibility = MAP_SHARED | MAP_ANONYMOUS;  // The remaining parameters to `mmap()` are not important for this use case,  // but the manpage for `mmap` explains their purpose.  return mmap(NULL, size, protection, visibility, -1, 0);}

The following is an example program that uses the function defined above to allocate a buffer. The parent process will write a message, fork, and then wait for its child to modify the buffer. Both processes can read and write the shared memory.

#include <string.h>#include <unistd.h>int main() {  char parent_message[] = "hello";  // parent process will write this message  char child_message[] = "goodbye"; // child process will then write this one  void* shmem = create_shared_memory(128);  memcpy(shmem, parent_message, sizeof(parent_message));  int pid = fork();  if (pid == 0) {    printf("Child read: %s\n", shmem);    memcpy(shmem, child_message, sizeof(child_message));    printf("Child wrote: %s\n", shmem);  } else {    printf("Parent read: %s\n", shmem);    sleep(1);    printf("After 1s, parent read: %s\n", shmem);  }}


Here is an example for shared memory :

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#define SHM_SIZE 1024  /* make it a 1K shared memory segment */int main(int argc, char *argv[]){    key_t key;    int shmid;    char *data;    int mode;    if (argc > 2) {        fprintf(stderr, "usage: shmdemo [data_to_write]\n");        exit(1);    }    /* make the key: */    if ((key = ftok("hello.txt", 'R')) == -1) /*Here the file must exist */ {        perror("ftok");        exit(1);    }    /*  create the segment: */    if ((shmid = shmget(key, SHM_SIZE, 0644 | IPC_CREAT)) == -1) {        perror("shmget");        exit(1);    }    /* attach to the segment to get a pointer to it: */    data = shmat(shmid, NULL, 0);    if (data == (char *)(-1)) {        perror("shmat");        exit(1);    }    /* read or modify the segment, based on the command line: */    if (argc == 2) {        printf("writing to segment: \"%s\"\n", argv[1]);        strncpy(data, argv[1], SHM_SIZE);    } else        printf("segment contains: \"%s\"\n", data);    /* detach from the segment: */    if (shmdt(data) == -1) {        perror("shmdt");        exit(1);    }    return 0;}

Steps :

  1. Use ftok to convert a pathname and a project identifier to a System V IPC key

  2. Use shmget which allocates a shared memory segment

  3. Use shmat to attache the shared memory segment identified by shmid to the address space of the calling process

  4. Do the operations on the memory area

  5. Detach using shmdt


These are includes for using shared memory

#include<sys/ipc.h>#include<sys/shm.h>int shmid;int shmkey = 12222;//u can choose it as your choiceint main(){  //now your main starting  shmid = shmget(shmkey,1024,IPC_CREAT);  // 1024 = your preferred size for share memory  // IPC_CREAT  its a flag to create shared memory  //now attach a memory to this share memory  char *shmpointer = shmat(shmid,NULL);  //do your work with the shared memory   //read -write will be done with the *shmppointer  //after your work is done deattach the pointer  shmdt(&shmpointer, NULL);