简体   繁体   中英

Linux shmget() function in c++

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 ie 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.

Ok, before I explain all the minor details, one thing that is quite important when sharing data through shared memory is that you understand what you are actually putting into the share memory.

In your example, you have:

string example[] = {"This is", "An Example ", "of strings"};

You probably haven't thought very hard about this, but how do you think the acutal string is stored inside a "std::string"? I don't know the exact details in this particular case, but one fairly typical implementation would be something like this [this is simplified, a real "class string" is a template class declaration using a basic_string class as a foundation].

class string
{
    char *str;
    int len;
}

So, if you copy example into the shared memory, then you will have in your shared memory, three pointers and three integers. These pointers point to some memory that is almost certainly NOT in the shared memory... Clearly, that memory address in the "other process" will not be what you expect [well, at least if the strings aren't constant and your process is forked before the strings were created - but if the strings are constant and/or already there when the fork happens, you don't really need shared memory, right?]

The typical solution to this is to ONLY store data in the shared memory that is "plain old data" (so no objects that have member functions or pointers inside them). One way to achieve this is to "serialize" the data - this is the same thing you'd do if you wanted to store the data in a file.

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 ie specify a string as the shared memory name or and in for an id number.

You can either specify a key or use IPC_PRIVATE (which in the man-page is explained as an "unfortunate name", and that "IPC_NEW" would be a better coice)

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?

The site you link to actually explains how to store the shmid :

int shmid;

... Several lines of code... 

if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {

I think this should be enough to get you started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM