02-28-04 08:34 PM
Tejas Kokje wrote:
> Hi,
>
> I have written following simple program for fork.
> I am allocating memory in the parent process. The question I have is I
> am freeing this memory in the child process. since childid is a
> pointer, will the entire allocated memory in parent process gets free.
> or does fork copy the childid and allocates memory for every child
> process and thus childid array of parent remain intact.
Calling fork() creates a new process whose address space is
(conceptually) a complete copy of the address space of the parent process.
Nothing you do in the child process will affect the memory in the parent
process.
>
>
>
> int main()
> {
> pid_t *childid=(pid_t *)malloc(sizeof(pid_t)*10);
> u_short i;
>
> for(i=0;i<10;i++)
> {
> if((childid[i]=fork())==0)
> {
> free(childid);
> fprintf(stdout,"Hi I am child process %d\n",i);
> fprintf(stdout,"My process id is %d and my parent id is
> %d\n\n",getpid(),getppid());
> exit(1);
> }
> else
> continue;
> }
> printf("Hi I am parent process\n");
> printf("My id is %d",getpid());
> return EXIT_SUCCESS;
>
> }
>
HTH,
--ag
--
Artie Gold -- Austin, Texas
"Yeah. It's an urban legend. But it's a *great* urban legend!"
[ Post a follow-up to this message ]
|