How to share an existing char * with another process?

I am trying to share some memory with another forked + execed process using shmget and shmat :

 char test[]="test"; int shID; char *shptr; key_t shkey = 2404; shID = shmget(shkey, sizeof(char)*(strlen(test)+1), IPC_CREAT | 0666); if (shID >= 0) { shptr = shmat(shID, 0, 0); if (shptr==(char *)-1) { perror("shmat"); } else { memcpy(shptr, &test, strlen(test)+1); .... //forking and execing .... shmdt(shptr); } } else { perror("shmget"); } 

This works great.

The fact is that test[] will be a huge char* . Therefore, I liked to easily share text[] instead of copying it. Is there a better way to handle this?

+3
source share
1 answer

if you can read the file size or the exact memory that you want to read from the file and location than you can use mmap to map this part of the file to the memory.

0
source

Source: https://habr.com/ru/post/1241924/


All Articles