5

I'm using fork() in C to split up the work of running through local arrays, having each process run through half and then multiply the numbers at each point in the arrays and then set the product in a third array.

pid_t  pid;
pid = fork();

if (pid == 0){
    for (i=1; i<((SIZE/2)+1); i++)
    {
        output[i] = (one[i] * two[i]);
    }
    exit(0);
}
else{
    wait(NULL);
        for (i=((SIZE/2)+1); i<(SIZE+1); i++)
        {
            output[i] = one[i]*two[i];
        }         
}

However, when I print the product array after this segment of code i'm only receiving the section set by the parent process, i'm assuming this is because the child process is storing it's values elsewhere in memory which the parent is unable to pick up when printing the product array, but i'm not entirely sure. Thanks in advance for any help.

Satchel76
  • 53
  • 1
  • 3

2 Answers2

9

it seems that you have fork confused with threading.

Forking copies the whole process. Forking isn't like firing off a thread (well it is similar, but threads share the process memory, forking copies the process memory). Changes made after the fork aren't shared between parent or children. If you want to share memory between a parent and child on UNIX while using fork() you need to setup a shared memory segment and put that array within that memory. Lookup shared memory (shmget, smctl) if you want to stick with the fork semantics.

forking has its uses, but is an older, traditional multi-processing API that has in most cases been superseded by multithreading. Forking a new process is much more expensive than creating a new thread, even though fork is optimized on modern OSes that support it. Probably the most common use of fork() is to create a daemon (fork + parent exit) or to execute a command (pipe + fork + exec) as in the implementation of the popen() call.

If using C, you should look into the pthreads API or some other thread library that supports a system thread. Of course, looking at your intended task, you can still use fork, but once you get the hang of threads, it isn't any more complex than using fork with shared memory, unless the algorithm you are implementing is complex.

x4nd3r
  • 855
  • 1
  • 7
  • 20
0xeaea0000
  • 106
  • 5
  • 3
    May I add another useful use of forking a new process, is that you can run a possible faulty code (may be due to bad input) or some testing code in its own process. This protects the main process from being killed by a signal like SIGSEGV for example. The parent process can still report the status of the child (using `wait()`). – hesham_EE Sep 29 '14 at 04:41
4

When you fork, the new child process gets a copy of the parent's address space. It is completely separate. If you need to communicate between parent and child, you will need to use pipes, shared memory, or such.

Note: in any modern Linux, the child's page table is pointing to all of the parent's pages, and both pages table's entries are marked "copy on write". Thus both processes are actually looking at the same physical memory. However, as soon as either process tries to write to a page of memory, it traps and then get's a private copy of the page to modify. From the processes' point of view, it is the same, except that the fork is a lot faster.

DoxyLover
  • 3,366
  • 1
  • 15
  • 19