I am trying to setup s segment of shared memory to hold an array of strings. As an example this array
string example[] = {"This is", "An Example ", "of strings"};
I then try to call shmget so I can get reserve the appropriate amount os shared memory.
shmget(IPC_PRIVATE, sizeOf(example), IPC_CREAT);//I hope this creates the shared memory segment
I found this site which says "The key argument is a access value associated with the semaphore ID" What a semaphore ID and how do I create one.
This compiles using g++ -Wall but I don't know how to check and see if it has. According to the shm manual in linux where IPC_PRIVATE is I should specify something of type "key_t". My thoughts are the key will be how I identify the shared memory i.e. specify a string as the shared memory name or and in for an id number.
Reading the manual through points out that shmget() returns a "shmid". Again though what is an shmid is it an int, string, pointer or is it of some special type and is this then the way I identify my shared memory segment? Also if shmget() is returning something won't I need to assign it to something?
If you can recommend a tutorial or overview I would really appreciate it. In addition to the specific questions above I would like an example of accessing shared memory from a thread that will be created from the fork() command.
My ultimate goal is to spawn a thread for each string in my array that will perform a sort of inversion. This is just a small little task to start working with shared memory and pthreads since I havn't found a tutorial to work through.
shmget()
returns anint
(kernel.org/doc/man-pages/online/pages/man2/shmget.2.html). So ashmid
would be an integer. – Tim