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

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
}

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?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.