question about fork()
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > question about fork()




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    question about fork()  
Tejas Kokje


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-28-04 07:33 PM

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.



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;

}



Regards,
Tejas Kokje
University of Southern California





[ Post a follow-up to this message ]



    Re: question about fork()  
Artie Gold


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
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 ]



    Re: question about fork()  
Jerry Feldman


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-29-04 02:33 PM

On Sat, 28 Feb 2004 13:42:30 -0600
Artie Gold <artiegold@austin.rr.com> wrote:

> Tejas Kokje wrote: 
>
> 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.
Not only is Artie correct. The array that you malloc'd is also copied.
I see some problems here:
First, you don't use wait(2), so the child processes may become zombies.
Secondly, you don't free your array in the parent. While allocated
storage is freed when you exit, it is always good practice to free
anything you allocate.
Lastly, you do not check to see if fork(2) returns an error.

None of these are serious problems.
 
> HTH,
> --ag
>
> --
> Artie Gold -- Austin, Texas
>
> "Yeah. It's an urban legend. But it's a *great* urban legend!"


--
Jerry Feldman <gaf-nospam-at-blu.org>
Boston Linux and Unix user group
http://www.blu.org PGP key id:C5061EA9
PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 10:31 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register