Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could look into doing a shared memory object using <code>shmget()</code>, <code>shmat()</code>, etc. First have the process that obtains the right to initialize the shared memory object read in your file and copy it into the shared memory object address space. Now any other process that simply gets a return shared memory ID value can access the data in the shared memory space. So for instance, you could employ some type initialization scheme like the following:</p> <pre><code>#include &lt;sys/shm.h&gt; #define KEYVALUE 1000 //arbitrary value ... just needs to be shared between your processes int file_size //read your file and obtain its size in bytes; //try to create the shared memory object int shared_mem_id; void* shared_mem_ptr = NULL; if ((shared_mem_id = shmget(KEYVALUE, file_size, S_IRUSR | S_IWUSR IPC_CREAT | IPC_EXCL)) == -1) { if (errno == EEXIST) { //shared memory segment was already created, so just get its ID value shared_mem_id = shmget(KEYVALUE, file_size, S_IRUSR | S_IWUSR); shared_mem_ptr = shmat(shared_mem_id, NULL, 0) } else { perror("Unable to create shared memory object"); exit(1); } } else { shared_mem_ptr = shmat(shared_mem_id, NULL, 0); //copy your file into shared memory via the shared_mem_ptr } //work with the shared data ... </code></pre> <p>The last process to use the shared memory object, will, just before destroying it, copy the modified contents from shared memory back into the actual file. You may also want to allocate a structure at the beginning of your shared memory object that can be used for synchronization, i.e., there would be some type of "magic number" that the initializing process will set so that your other processes will know that the data has been properly initialized in the shared memory object before accessing it. Alternatively you could use a named semaphore or System V semaphore to make sure that no process tries to access the shared memory object before it's been initialized.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload