in reply to What is PERL_COPY_ON_WRITE Compile Option About

CoW refers to a method of optimizing memory usage for multithreaded programs. Consider a program that fork()s after intitlizing a variable. Under normal usage, those variables are copied into the memory space for each program. Under CoW, they point to the orginal variable in the parent. As long as each thread only reads the variables, this is safe. When a modification occurs, the variable is copied into the specific thread's memory space, thus keeping the change from other threads.

"There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Replies are listed 'Best First'.
Re^2: What is PERL_COPY_ON_WRITE Compile Option About
by ikegami (Patriarch) on Mar 04, 2005 at 17:34 UTC
    I'm confused by your answer. You talk of threads created by fork, but fork creates new processes, not new threads (except in Windows). Does CoW affect multitasking or multithreading or both?

      "Processes are threads with an attitude. Or the other way around." (I belive this is attributed to Larry, and may not be an exact quote).

      Forks and threads are conceputally similar, and for the purposes of CoW, are basically the same thing.

      "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

        So does this imply that if one is not using threads, such as, when compiling perl for DBD::Oracle, then the CoW option is unnecessary? Please elaborate when one would want or need to turn on CoW.

        DeadPoet

        Similar yes, but the difference is how they share memory. Threads, by definition, share memory so that changing a variable in one thread changes the the same variable in all other threads of that process.

        Are you're saying that:

        1) CoW saves memory when creating new processes by sharing memory until a modification is made to to that part of memory, and

        2) CoW prevent threads from sharing modified variables like they normally do?

        That seems odd to me.