[CC += linux-man@] Hi Chen, On Mon, Feb 03, 2025 at 02:25:14PM +0800, Chen Linxuan wrote: > Alejandro Colomar <alx@xxxxxxxxxx> ???2025???2???3????????? 00:56????????? > > On Tue, Jan 21, 2025 at 11:13:51AM +0800, Chen Linxuan wrote: > > > While reading the help manual for clone.2, I notice that the parent > > > process in the example code does not release the stack of the child > > > process. > > > > > > This is not a problem for the example program, but it is somewhat > > > misleading. > > > > > > Signed-off-by: Chen Linxuan <chenlinxuan@xxxxxxxxxxxxx> > > > --- > > > man/man2/clone.2 | 2 ++ > > > 1 file changed, 2 insertions(+) > > > > > > diff --git a/man/man2/clone.2 b/man/man2/clone.2 > > > index 3ffe8e7b8..5e6b2ef1f 100644 > > > --- a/man/man2/clone.2 > > > +++ b/man/man2/clone.2 > > > @@ -1910,6 +1910,8 @@ main(int argc, char *argv[]) > > > child commences execution in childFunc(). */ > > > \& > > > pid = clone(childFunc, stackTop, CLONE_NEWUTS | SIGCHLD, argv[1]); > > > + if (munmap(stack, STACK_SIZE)) > > > + err(EXIT_FAILURE, "munmap"); > > > if (pid == \-1) > > > > Would you mind clarifying why this munmap(2) call goes before the error > > handling of clone(2)? I'm not very familiar with clone(2). > > The memory we mmap here is used as the stack of child process we are > going to create. > Once child process is created, I mean clone(2) return without error, > it's OK to munmap(2) memory, > as the child process has its own memory space, and that memory is not > used in parent process at all. > So, whether the clone(2) is success or not, we both need to call > munmap(2) to release the memory. In case of a clone(2) error, exit(3) (called within err(3)) will release the memory, right? Why do we need an explicit munmap(2) call? > > If we call munmap(2) after the error handling of clone(2), we will > print a error message and then exit > before we free those memory. exit(3) releases all memory, doesn't it? Why would we want to explicitly munmap(2) it before printing the error message? Have a lovely day! Alex -- <https://www.alejandro-colomar.es/>
Attachment:
signature.asc
Description: PGP signature