Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using fork: accessing child process memory from parent

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.

like image 630
Satchel76 Avatar asked Sep 29 '14 04:09

Satchel76


People also ask

Can I fork in child process?

It's exactly like how you create the first child process. You need to fork() again in the child process to create another process. Use wait(2) to wait for the child processes.

When a process is created using fork () what is shared between parent process and child process?

3.5 When a process creates a new process using the fork()operation, which of the following states is shared between the parent process and the child process? Answer: Only the shared memory segments are shared between the parent pro- cess and the newly forked child process.

Does fork return child PID?

When the main program executes fork(), an identical copy of its address space, including the program and all data, is created. System call fork() returns the child process ID to the parent and returns 0 to the child process.

Does a forked process share memory?

After fork the parent and child can update their own copies of the variables in their own way, since they dont actually share the variable. Here we show how they can share memory, so that when one updates it, the other can see the change.


2 Answers

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.

like image 124
0xeaea0000 Avatar answered Sep 21 '22 00:09

0xeaea0000


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.

like image 39
DoxyLover Avatar answered Sep 21 '22 00:09

DoxyLover