954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

destroy memory segment on server program

Hi everyone. i have an issue with a server program i'm developing for a university project.
The issue is the following:

The server program i'm building uses shared memory and forks. However i have a problem as to how can i destroy the shared memory segments i've created. A simplified version of my code is as follows |
|
\ /
v

int main()
{
 //connection to clients an stuff
 prossid = fork()
 if(prossid == 0)
 {
  //do stuff for child prossess
 }
 else
 {
  close(ns)//if it's the father prossess close the connection with the client program
 }
}


I want the father prosses to destroy the memory segment when there are no child prossesses left AND it has to exit. Any advise on has i can proceed from this point?

P.S. For any lack of information please let me know. Thank you in advance

SpyrosMet
Light Poster
46 posts since Feb 2010
Reputation Points: 7
Solved Threads: 3
 

Hi everyone. i have an issue with a server program i'm developing for a university project. The issue is the following:

The server program i'm building uses shared memory and forks. However i have a problem as to how can i destroy the shared memory segments i've created. A simplified version of my code is as follows | | \ / v

int main()
{
 //connection to clients an stuff
 prossid = fork()
 if(prossid == 0)
 {
  //do stuff for child prossess
 }
 else
 {
  close(ns)//if it's the father prossess close the connection with the client program
 }
}

I want the father prosses to destroy the memory segment when there are no child prossesses left AND it has to exit. Any advise on has i can proceed from this point?

P.S. For any lack of information please let me know. Thank you in advance


There are functions like wait or waitpid by which parent process can wait for the child to terminate before it continues execution..

The code will be like

if (pid == 0)
{
 //child process
}
else
{
 wait(&state); //Parent Waits here -- 
 //parent
 //destroy shm
}
sree_ec
Junior Poster
119 posts since Jan 2010
Reputation Points: 22
Solved Threads: 14
 

This has so many unanswered questions....

How does the listening part of the server know that no more clients are pending without waiting?

You mentioned shared memory segments at first then you say memory segment, which is it? Does the listening server create one memory segment for all the clients? Does the listening server create one segment for each client or do you have some other scheme?

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

This has so many unanswered questions....

How does the listening part of the server know that no more clients are pending without waiting?

You mentioned shared memory segments at first then you say memory segment, which is it? Does the listening server create one memory segment for all the clients? Does the listening server create one segment for each client or do you have some other scheme?

it is a shared memory segment used by all the clients (haven't reached the semaphore part yet so don't worry about it). As for the listening part of the program, i honestly don't know anything because my project teamates created it. If you need the code to see for yourself what's going on in that part i'll post it but it has no comments. Just tell me if you need it.

SpyrosMet
Light Poster
46 posts since Feb 2010
Reputation Points: 7
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: