UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » Processes
  • » correct way to delete a shared memory segment

#1 2007-11-28 11:33 PM

darthsidious
Member
Registered: 2007-11-18
Posts: 14

Re: correct way to delete a shared memory segment

hello, it's me again.

this time i need to know if what i'm doing is OK.
One of the key points of what i'm programming is to delete
every shared memory segment when the client process (or server)
finishes. I'm using shared memory to comunicate two different processes running in the same machine (not father-son fork() but two different processes).

I'm creating the shared memory in server this way:

   int shmId;
   keysh = ftok("/bin/cat", 45);
   shmId = shmget(keysh, sizeof(int), 0777 | IPC_CREAT);
   pointerToShMem = shmat(shmId, 0,0);

and, also in the server, i delete it this way

shmdt(pointerToShMem);
shmctl(shmId, IPC_RMID, 0);


In the client, i do this:
   
   int shmId;
   keysh = ftok("/bin/cat", 45);
   shmId = shmget(keysh, sizeof(int), 0777);
   pointerToShm = shmat(shmId, 0,0);

and i "delete" it in this way

   shmdt(pointerToShm);


so the question is: is this the correct way to do it?
if i run ipcs after quiting the program, i can see
some segments still there. Maybe i'm missing something.

Thanks in advance!!!

Edward.-

Offline

#2 2007-12-04 12:41 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: correct way to delete a shared memory segment


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#3 2007-12-04 04:17 PM

darthsidious
Member
Registered: 2007-11-18
Posts: 14

Re: correct way to delete a shared memory segment

thanks!!!

the server always removes the shm segment after the client has gone. The client tells the server that he's going and detaches the shm, so, after that, the server removes it.

:-)

Offline

  • Index
  • » Processes
  • » correct way to delete a shared memory segment

Board footer

Powered by FluxBB