Click to See Complete Forum and Search --> : forking in C


threadhead
08-07-2003, 04:47 PM
hi!

assume we have the following sample code snippet.



pid_t child;

if ( (child = fork()) == 0){
/* child */
something();
exit(0);
}
/* parent */
something2();

waitpid(child, NULL, 0);

return 0;



the child only consists of the function call to something() and then exits.

while the child is calling the function the parent is calling something2().

does all that happen at the same time?
how can i make myself a better picture of what is happening in this code?

thanks

Stuka
08-07-2003, 05:42 PM
Think of it like this: the entire binary code is copied into another section of memory, and runs separately. So instead of having 1 set of code, you have an identical clone, the only difference being in one copy, fork() returned 0, where in the other, it returned teh pid.

threadhead
08-07-2003, 05:46 PM
now it makes sense to me.
thank you :)

Stuka
08-07-2003, 05:58 PM
I had the exact same problem, until someone told me the same basic thing :)

Sepero
08-09-2003, 08:41 PM
Hey, I might be completely wrong, but instead of:pid_t child;

if ( (child = fork()) == 0){
/* child */
something();
exit(0);
}Shouldn't it be:
child->something();
or
child.something();

I know that this is a little off topic, but I was just wondering?

threadhead
08-10-2003, 03:21 AM
maybe thats correct for c++?
looks like OO programming to me...

Sepero
08-10-2003, 03:53 AM
Yeah, but I didn't think c and c++ differed that much in function calls.

Wolface
08-10-2003, 04:48 AM
In functions calls and everything else!
I almost went mad finding a book that teaches the exact version and type of C that I was using. Every other just did't work right

bwkaz
08-10-2003, 01:56 PM
Originally posted by Sepero
Shouldn't it be:
child->something();
or
child.something(); Ehh? ;)

pid_t is not a class. It's a typedef (usually typedef'ed to either int or unsigned int).

You can't call member functions (even in C++) of an int.

And you can't call member functions of any class or struct in C, unless you make it explicit that the address of the function is what the struct contains (see, for example, the VFS header files defining stuff like struct file_operations (or whatever it is that they call it), in the kernel source). But even this isn't completely the same -- in C++, member functions take up zero space in the struct, whereas in this kind of struct, they'd take up the size of a pointer (probably 4 bytes).

No, something(); is the right call. It's just the function that you want to run inside the child process.

stoe
08-10-2003, 02:25 PM
when speaking of linux on all modern architectures with a MMU, when a process forks, the memory address space of the parent is NOT copied to the child. the code memory pages are shared between both the parent and child as they are read-only. the data pages are initally shared between the parent and child after a fork, however if either process tries to write to a shared data page, a hardware exception occurs, which traps execution into the kernel. the kernel then duplicates the page that was written to and places a pointer to the new page in childs page table. then, execution is returned to the process which caused the exeception and the offending instruction is rexecuted. thus, the parent and child happily share all data until modifications are made, at which time only the pages being modified are copied. this procedure is known as copy on write. it is much more efficient than copying the entire address space.

Sepero
08-10-2003, 08:13 PM
Originally posted by bwkaz
Ehh? ;)

pid_t is not a class. It's a typedef (usually typedef'ed to either int or unsigned int).Duh! I should have known that. :D
I'm just so used to creating another object in Java to start a new thread. My mistake. :)

Stuka
08-11-2003, 10:52 AM
stoe: I realize all that - copy-on-write for data, and read-only code pages are Good Things(R), but the point of what I wrote was to help visualize the concept. I really don't think the extra detail (while correct, important, and useful information) was really needed ;).