Professional Web Applications Themes

question about fork() - UNIX Programming

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 ...

  1. #1

    Default question about fork()

    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
    Tejas Guest

  2. #2

    Default Re: question about fork()

    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.
     
    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas

    "Yeah. It's an urban legend. But it's a *great* urban legend!"
    Artie Guest

  3. #3

    Default Re: question about fork()

    On Sat, 28 Feb 2004 13:42:30 -0600
    Artie Gold <rr.com> 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.[/ref]
    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!"[/ref]


    --
    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
    Jerry Guest

Similar Threads

  1. fork not available?
    By walter@mwsewall.com in forum Ruby
    Replies: 22
    Last Post: September 23rd, 02:56 PM
  2. FD Inhertiance on fork()
    By Peteris Krumins in forum UNIX Programming
    Replies: 3
    Last Post: July 23rd, 07:33 PM
  3. Problem with fork
    By Krishna in forum PERL Miscellaneous
    Replies: 2
    Last Post: June 28th, 01:31 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139