[uClinux-dev] how to implement vfork()

Ahan Hsieh =?Big5?B?wcKqWrp+?= ahan.hsieh at sunplus.com
Wed Jun 6 07:56:04 EDT 2007


Dear Phil,

I take this simple example:

    int main(void)
    {
        pid_t pid;
        int status, wpid;
        if ((pid = vfork()) == 0) {
            execvp(argv[0], argv);
            _exit(0);
        }
        return EXIT_SUCCESS;
    }

          |             |
          |             |
          |             |
    0x60  +-------------+
          |             |
          |             | vfork() frame
          |             |
    0x100 +-------------+
          |             |
          |             | main() frame 
          |             |
    0x200 +-------------+
    stack content before processes return vfork


          |             |
          |             |
          |             |
    0x60  +-------------+
          |             |
          |             | vfork() frame
          |             |
    0x100 +-------------+
          |             |
          |             | main() frame 
          |             |
    0x200 +-------------+
    stack content after child return vfork


          |             |
          |             |
    0x40  +-------------+
          |             |
          |             |
          |             | execvp() frame
          |             |
    0x100 +-------------+
          |             |
          |             | main() frame 
          |             |
    0x200 +-------------+
    stack content after child call execvp

I observe that after the child process call the execvp(), the vfork() 
frame is corrupted.
After the parent process returns from the vfork() system call, the vfork() 
frame is corrupted.
So, unexpected result will be occured.

Best regards,
Ahan Hsieh




Phil Wilshire <pwilshire at cox.net> 
Sent by: uclinux-dev-bounces at uclinux.org
2007/06/06 ¤U¤È 07:35
Please respond to
uClinux development list <uclinux-dev at uclinux.org>


To
uClinux development list <uclinux-dev at uclinux.org>
cc

Subject
Re: [uClinux-dev] how to implement vfork()






Hi Ahan,

Sorry to state the obvious but limit what you do in the child process 
before calling execve.

You can run the same code as the parent using execve and identify it as 
a child process by setting some options in its arg list.
You have control over both the arg list and the environment for the new 
child process.

Take a look at some examples in
simpleinit and inetd

Regards
   Phil Wilshire

Ahan Hsieh ÁªZº~ wrote:
> 
> Hi folk,
> 
> I'm porting uClinux to our new no-mmu architecture.
> There is a trouble occured.
> 
> After system call vfork() is called, the parent is suspended and
> cannot continue executing until the child exits or calls exec(),
> the system call used to start a new application.
> The child, directly after returning from vfork(),
> is running on the parent's stack and is using the parent's memory and 
data.
> This means the child can corrupt the data structures or the stack in the 

> parent, resulting in failure.
> 
> How can I avoid the problem? Is there any solution?
> 
> Thanks!
> 
> Best regards,
> Ahan Hsieh
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> uClinux-dev mailing list
> uClinux-dev at uclinux.org
> http://mailman.uclinux.org/mailman/listinfo/uclinux-dev
> This message was resent by uclinux-dev at uclinux.org
> To unsubscribe see:
> http://mailman.uclinux.org/mailman/options/uclinux-dev

_______________________________________________
uClinux-dev mailing list
uClinux-dev at uclinux.org
http://mailman.uclinux.org/mailman/listinfo/uclinux-dev
This message was resent by uclinux-dev at uclinux.org
To unsubscribe see:
http://mailman.uclinux.org/mailman/options/uclinux-dev


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.uclinux.org/pipermail/uclinux-dev/attachments/20070606/867c88fe/attachment.html


More information about the uClinux-dev mailing list