Search code examples
perlfork

When I fork a process in Perl, can the child process see its parent's data


Say I am building a webcrawler and I have a hash that contains all the visited URLs.

When I fork child processes I would like to be able to access the hash of visited URLs from the child process but I don't want to copy the hash since it's big and it will drain the computer's memory.

When I fork using Parallel::ForkManager and I print the address of a hash that was declared in the parent process I get the same address when I print from the parent and from the child e.g HASH(0x7fc59d017f38)

But I want to make sure the data structure isn't being copied to the child process


Solution

  • No. Each process has its own address space. The child's address space starts as a copy of its parent's, but changing either will have no effect on the other. You'll need to explicitly exchange the data you want to "share" by using one of a variety of possible communication channel (pipes, memcached, database, file, etc).