问题
I'm trying to share some memory with an other 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 fine.
The thing is that test[]
is going to be a huge char*
. So I liked easy to share text[]
instead of copying it.Is there any better way to handle this?
回答1:
if you can read file size or exact memory which you want to read from file and location than you can use mmap to map that part of file to memory.
来源:https://stackoverflow.com/questions/18946281/how-to-share-existing-char-with-an-other-process