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
}