Re: Help! Shared memory is getting deleted after process is killed



Anuradha wrote:
After killing my process, when I do ipcs, I am unable to see the shared
memory created. Also, I am getting the errno as ENOENT, which means
that it has created it since key does not exist.(#define ENOENT 2
/* No such file or directory */)

It works fine for me; the shm segment stays around after the process is
killed. Test program below.

GH



#include <stdio.h>
#include <errno.h>
#include <sys/shm.h>

int do_shm(void)
{
static key_t key;
int shmId;
void *shmBase;

if ((key = ftok("/tmp", 87654321)) == (key_t)-1)
{
printf("ftok failed - %s\n", strerror(errno));
return 0;
}
if ((shmId = shmget(key, 1, IPC_CREAT | IPC_EXCL | 0600)) == -1)
{
if (errno == EEXIST)
{
printf("Key %x exists -- try shmget without CREAT\n", key);
shmId = shmget(key, 1, 0);
}
if (shmId == -1)
{
printf("shmget failed - %s\n", strerror(errno));
return 0;
}
}
else
printf("Key %x created\n", key);
if ((shmBase = shmat(shmId, NULL, 0)) == (void *)-1)
{
printf("Unable to attach to shared memory - %s\n",
strerror(errno));
return 1;
}
printf("Attached to key %x\n", key);
return 1;
}

int main(int ac, char **av)
{
if (!do_shm())
return 1;
pause();
return 0;
}

.



Relevant Pages

  • Re: Help! Shared memory is getting deleted after process is killed
    ... After killing my process, when I do ipcs, I am unable to see the shared ... Also, I am getting the errno as ENOENT, which means ... I have a requirement wherein the shared memory *should not be* ...
    (comp.os.linux.development.apps)
  • Re: system calls
    ... Error found!: Cannot allocate memory ... As I said in my reply to Keith, you should only print errno if ... The c program seems to keep the memory locked up until it exits ... you can release it by calling free() before ...
    (comp.unix.shell)
  • Re: Handling read() errors
    ... what errno values correspond to these cases? ... the what kind of object you're reading from - if you're ... doesn't have enough memory you may get ENOBUFS or ENOMEM. ...
    (comp.unix.programmer)
  • Re: Standard integer types vs types
    ... Not all integers count things in memory. ... I could simply replace that array with a switch statement with no ... an int to temporarily hold the value of errno when I must preserve it ...
    (comp.lang.c)
  • Re: Is there a equivalent API on Unix as _get_errno on Windows?
    ... Then I think it should be safe to use errno directly on both Windows ... struct stat sb; ... Note how a single strerror_rcall can cover both ENOENT, ...
    (comp.unix.programmer)