|
Home > Archive > Unix Programming > February 2004 > question about fork()
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
question about fork()
|
|
| Tejas Kokje 2004-02-28, 2: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
| |
| Artie Gold 2004-02-28, 3: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!"
| |
| Jerry Feldman 2004-02-29, 9:33 am |
| 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
|
|
|
|
|