Munmap anonymous shared memory in a split child
I would like to know if it is necessary (or desirable) to disable shared memory (using munmap
) in the child created with fork
if the memory was fetched in the parent, before fork using mmap(..., MAP_ANONYMOUS | MAP_SHARED,...)
, and also will not be displayed in the parent which will be wait
for the child ... Also I would like to know if it is necessary (or desirable) to close the file in the child if the file was opened in the parent (before the fork using fopen
) and will be closed in the parent after the child completes.
I am thinking about using a custom signal and a signal handler where the parent will wait for the child processes and then the process - if it is the parent or not - will close the file and unpack the memory.This signal will be sent to all processes in the group from the process in which the error occurred ( i dont want to pass return values).
It's actually a little more complicated, but I just want to know if I need to do this:
void sig_handler() {
if (getpid() == getpgrp()) // parent
while (proc_count--)
wait(NULL); // signal has already been sent to all child processes
// every single process will do this:
fclose(memory->file);
munmap(memory, size);
exit(123);
}
or is this completely normal:
void sig_handler() {
if (getpid() == getpgrp()) {
while (proc_count--)
wait(NULL);
fclose(memory->file);
munmap(memory, size);
}
exit(123);
}
I tried to close the file in one child process; it didn't seem to affect other processes - I'm assuming the fd table is copied to fork. Is there a way to make this split between processes? (Probably not, I suspect)
Any answer is appreciated. Thank.
PS. Is there a reason why I can't start my question with a greeting (like Hello)?
source to share
If you are about to exit the process, there is no point in calling munmap()
. No memory will be mapped to this process when the process ends.
fclose()
will flush any buffers containing unwritten data to the file. In all processes - parent and child. Whether or not you want it to be up to you.
exit()
implicitly erases all buffers. _exit()
does not clear buffers or call any other output handlers.
source to share