Re: Re: vfork implementation...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



 


On Thu, 15 Jun 2006 Jinesh K J wrote :
>On 6/14/06, Raseel Bhagat <raseelbhagat@xxxxxxxxx> wrote:
>>Hi Abu,
>>
>>
>>On 6/14/06, Jinesh K J <jineshkj.newsletters@xxxxxxxxx> wrote:
>> > On 6/14/06, Abu M. Muttalib <abum@xxxxxxxxx> wrote:
>> >
>>
>> > > As mentioned in the description of vfork call, it is said that child is
>>not
>> > > allowed to write to the address space, but in the following example its
>>not
>> > > so. The child is able to write to the process address space. This
>>program
>> > > was tested with Linux Kernel 2.6.9. Why is it so?
>> >
>> > i think you might probably have misread it. the child is not
>> > disallowed to write to the address space, but its just not healthy.
>> > since child is using the same address space as that of the parent,
>> > whatever global variables accessed by the child and parent will both
>> > be the same.
>>
>>
>>  Exactly, it does not mention in the man page of vfork that the child is NOT
>>ALLOWED to write to the address space.
>>
>>
>> > u can refer to the arch/xxx/kernel/process.c and see the sys_vfork
>> > implementation. the child's virtual memory is a clone of that of the
>> > parent. also note that the parent is put on hold till the child exits
>> > or execvs, so that this technique cannot be used for truly parallel
>> > IPC between parent and child. its intention is clearly mentioned in
>> > the man page itself.
>>
>>
>>  Exactly, the vfork is primarily used in cases when it is expected that the
>>child will execve immediately (before writing anything) after it is born.
>>
>>
>> > >
>> > > fork.c
>> > >
>>----------------------------------------------------------------------------
>> > > ---------------------------------------
>> > > #include <stdio.h>
>> > >
>> > > unsigned char *glob_var = NULL;
>> > >
>> > > void main()
>> > > {
>> > >        int pid = -8,i;
>> > >        pid = vfork();
>> > >
>> > >        if(pid < 0)
>> > >                printf("\n FORK ERROR \n");
>> > >
>> > >        if(pid == 0)
>> > >        {
>> > >                unsigned char * local_var = NULL;
>> > >                local_var = (unsigned char *)malloc(5);
>> > >                strcpy(local_var,"ABCD");
>> > >                glob_var = local_var;
>> > >                printf("\nCHILD :Value of glob_var is  %X local_var is
>>%X glob_var is %c
>> > > \n",glob_var,local_var,*glob_var);
>> > >                for(i=0;i<4;i++)
>> > >                {
>> > >                        printf("\n CHAR is %c \n",glob_var[i]);
>> > >                }
>> > >                printf("\nCHILD1 :Value of glob_var is %X
>>%c\n",glob_var,*(glob_var));
>> > >        }
>> > >
>> > >        if(pid > 0)
>> > >        {
>> > >                printf("\nParent : Value of glob_var is  %X
>>%c\n",glob_var,*(glob_var));
>> > >                free(glob_var);
>> > >                printf("\nParent : Value of glob_var is %X
>>%c\n",glob_var,*(glob_var));
>> > >                exit(0);
>> > >        }
>> > > }
>> > > ------------------------------
>>
>>
>>  Btw, the same code gives Segmentation Fault on my Solaris 8 machine  !!!!
>>
>i think its because the vfork implementation varies across platforms.
>may be in solaris, the child would be sharing parents VM but could be
>in write protected mode.
>>

In Linux also the child shares the parents VM and that is how it is implemented. The child is given a chance to run first, so that it can do an execve() and move on. generally vfork() is called when there we know that the child will do an execve().

-Rohit
>>
>>--
>>Raseel.
>
>--
>Kernelnewbies: Help each other learn about the Linux kernel.
>Archive:      http://mail.nl.linux.org/kernelnewbies/
>FAQ:          http://kernelnewbies.org/faq/
>




[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux