Reputation: 105
So I have an assignment to create a library of functions that can create a posix shared memory table, add a record, delete a record and close the table. For the close_table function my prof wants it to only disconnect the current process calling close_table, so if there are two or more processes connect to the shared memory, only the one that called close_table will be disconnected. Looking at the different functions for working with posix shared memory it seems shm_unlink is the only thing that does something like that, except is deletes the shared memory object itself. is there a function that only disconnects only the process calling it?
/* Close connection to the given table. This should only disconnect
* the current process from the table.
*/
void close_table(table_t *tbl);
Upvotes: 3
Views: 582
Reputation: 1
From the POSIX shm_open()
documentation, the creation of the shared memory region is:
fd = shm_open("/myregion", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
if (fd == -1)
/* Handle error */;
if (ftruncate(fd, sizeof(struct region)) == -1)
/* Handle error */;
/* Map shared memory object */
rptr = mmap(NULL, sizeof(struct region),
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
To remove the mapping:
munmap( rptr, sizeof(struct region));
That's all that's needed.
You should have probably closed the file descriptor after calling mmap()
as it's no longer needed or even very useful at that point.
Upvotes: 3