Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: a7699a56bc9eac02e11f6060b32bc035fe09c7e0
Ticket: 550646ffffffffffffffffffffffffffffffffff
exec (fork) from huge process fails
User & Date: davidlallen on 2002-04-30 23:34:41
Changes

  1. icomment:
    Logged In: YES 
    user_id=530697
    
    Excerpts from discussion on comp.lang.tcl thread "Unix
    forking error encountered", mid-April 2002:
    
    I wrote:
    
    > Here's something you can try.  Take your favorite
    > tclsh and add a command with this C code.  (Yes, I
    > know this command leaks memory.  That's the point :-)
    >
    >    int i; void *p;
    >    for (i=0; i<512; i++) p = malloc(1024*1024);
    >
    > Each time you call it, it increases the virtual
    > memory size of your process by 512MB.  Now, in your
    > tcl shell, call this until the virtual memory size of
    > your process is greater than your remaining free
    > virtual memory.
    >
    > Then "exec ls", and you will get the out of memory
    > message.
    >
    > What I'm hoping to find is a way to run "ls" or other
    > similar unix commands when my virtual memory size is
    > larger than the free virtual memory.
    
    Dan Smart ([email protected]) wrote:
    
    > > > [...] how about: vfork(), then without diddling
    > > > any signal handlers immediately exec() a new
    > > > tclsh with command line arguments telling it that
    > > > it's completing a vfork/exec sequence so it can
    > > > tweak the signal handlers where they're supposed
    > > > to be in the child?
    > >
    > > Referring to the tcl 8.3.3 source code, "fork()"
    > > appears only once, in unix/tclUnixPipe.c, function
    > > TclpCreateProcess.  After fork()'ing, the child
    > > process code (pid == 0 means child process)
    > > executes RestoreSignals() before execvp()'ing.
    > >
    > > Based on my limited understanding, it seems to me
    > > that the code already does what you suggest.
    > >
    > > I think that line of the tcl source code could be
    > > changed to use vfork() instead of fork() and my VM
    > > problem would go away with no side effects.
    > >
    > > Am I missing something?
    >
    > Yes, an extra exec.
    >
    > Doing anything with signal handlers in the child
    > branch of a vfork is not a good idea, in fact doing
    > anything other than an exec is pretty much frowned
    > upon. If you want to use vfork(), you have to replace
    > the current process image (i.e. the copy that is the
    > child), with a new process image. The new process
    > image can then mess with its environment in anyway it
    > chooses, including change signal handling, mess with
    > file descriptors...  Thus to replace fork() with
    > vfork(), you need an intermediate process, that takes
    > the desired eventual command line, sets up a "sane
    > environment", and then execs the final process. The
    > definition of "sane environment" would also have to
    > be passed to the intermediate.
    >
    > Peter's suggestion was that the intermediate process
    > also be an incarnation of tclsh, with a magic flag to
    > tell it to perform the above function. In other words
    > the current (pseudoTCL):
    >
    >         if {[fork] == 0} {
    >                 RestoreSignals
    >                 exec myProgram and an arg
    >         }
    >
    > would become (more pseudoTCL)
    >
    >         if {[vfork] == 0} {
    >                 exec "tclsh -magic myProgram and an arg"
    >         }
    >
    > and tclsh would include the following (even more
    > pseudoTCL)
    >
    >         if {[string equal [lindex argv 0] "-magic"]} {
    >                 eval [lreplace $argv 0 0 exec]
    >         }
    >
    > Note that this does not include passing the necessary
    > context information to actually restore the signal
    > handlers (we need to set them to whatever the
    > original tclsh inherited from its parent, rather than
    > just to default).
    
    
    
  2. login: "davidlallen"
  3. mimetype: "text/plain"
  4. username: "davidlallen"