1
votes

"abc" string was stored to shared memory.

The program has finished (quit) by return 0; but the string is still stored in memory.

  • How long will it remain there?

  • Will it remain there until system reboot?

  • Can it be relied on that it will remain there?

Relevant code:

int main() {

    int mem_id;
    key_t key;
    char * string;

    key = 01;

    int str_len;
    str_len = strlen("abc") + 1;

    mem_id = shmget(key, str_len, IPC_CREAT | 0666);
    string = shmat(mem_id, NULL, 0);

    strcpy ( string, "abc" );

    return 0;
}
1
Shared resources are usually reference-counted. When the count is decremented to 0, the resource is freed.Martin James
This is the default behavior of shared memory. To control various possibilities look at shmctl function.Marian
You shouldn't probably be using SysV shared memory for anything new in 2017, use the POSIX shared memory insteadAntti Haapala -- Слава Україні
System V IPC is kernel persistence. That is a kernel-persistent IPC object exists until either it is explicitly deleted or the system is shut down.Seek Addo

1 Answers

0
votes

The string will remain there until another process overwrites it or deletes the shared memory segment, or the system is rebooted.

See also man ipcrm for removing shared SysV resources from the command line.