Page 1 of 2

Fork in linux

Posted: Sun Jul 27, 2008 11:21 am
by salil_bhagurkar
I once tried to implement fork in my os. But due to the different design of my scheduler and lack of my skill, i was unable to do so.
But I was just wondering about one concept of fork. Fork creates a new thread that has everything same except that the stack is different. Hence all the local variables used by the function will get duplicated whereas the globals will remain same.
The compiler uses [ebp+4], [ebp+8] and so on as local variables. So when the stack would change there would be a new copy of these automatically.
But a new context of the cpu registers is obviously created in fork which is the copy of the parent's context except for the esp and ebp stack registers. This context is then used by the child thread. My doubt goes as below:
If by chance one of the cpu registers (may be ebx) held the address of a stack variable (may be [ebp+16]) then this would cause serious trouble. A new cpu register set is created, but the register that holds the address of a stack variable still points to the variable in the parent's stack. How is this possible. This obviously never happens in linux. But how does it not happen?
Please tell me if my conceptual base is wrong. Thanks...

Re: Fork in linux

Posted: Sun Jul 27, 2008 11:25 am
by jnc100
Fork creates a new process with exactly the same state (i.e. cpu registers and contents of the virtual address space) as the parent process. Therefore the stack (and local variables) should be located at the same virtual address as in the parent process. You would typically use copy on write to make the new address space.

Regards,
John.

Re: Fork in linux

Posted: Sun Jul 27, 2008 11:36 am
by salil_bhagurkar
Fine, so mean to say that the CR3 is swapped even for child threads and we have the same stack address for the child but the actual ram location is different.... Cool... Got it..

Re: Fork in linux

Posted: Sun Jul 27, 2008 11:42 am
by jnc100
I say again, fork creates a new process, rather than a new thread.

Regards,
John.

Re: Fork in linux

Posted: Sun Jul 27, 2008 2:37 pm
by blackcatcoder
Fork in Linux as already said, creates a new Process with the same stack, code segment etc, but with an own pid, if you want to realize fork in your Os i would suggest to do it as Linux do it, just create a new process but don't copy all pages and etc into a new adress space(costs a lot of time), just map the processes virtual pages to the origin process who hoas done the fork. So if the new created process (or also called child) wants to access adress space you might get a Page Fault, and that's the point, when this pgf happens you copy the page or do your own mechanisn to grant the child access to the "region" (this is ment biy copy on write).


P. S. : Don't mix up processes with threads, processes have their own stack codesegment etc., threads just get their own registers and stack all other things are shared.
So IPC(inter process communication) in processes can only be realized with "pipes", sigs, msgs and shared mem.

Re: Fork in linux

Posted: Mon Jul 28, 2008 11:36 am
by salil_bhagurkar
I get it. Sorry for my wrong terminology.. I understand your concept of cow @blackcatcoder. Talking about implementing it in my os, i don't have the paging as part of my memory management. I only had done paging for fun and due to my curiosity. I do not intend to implement paging and virtual memory in my os, since my os is going to be a 'vm' os with an embedded processor in it.

But i do have native kernel threads (not processes obviously :) ) in my os. But what i was thinking of implementing was the 'fork like' effect wherein the control just splits in a very fascinating way to create two controls. I now realize how it is possible with the virtual memory support of the processor.

So is it possible for me to implement a fork only for threads (just splitting the control) in my os withouth using virtual memory?

Re: Fork in linux

Posted: Mon Jul 28, 2008 11:40 pm
by blackcatcoder
hmm,.. i need morre information on how you build up the whole thread stack and control it's flow.

but in genereal it should be possible to write such a "fork like" thing without vitrual memory, the only thing u will have to do is to copy the whole "thread" with all it's current allocated memory, if u really want that the "fork like" thing should split up one running thread into two runnning threads during it's runtime.

Re: Fork in linux

Posted: Tue Jul 29, 2008 1:40 am
by AJ
salil_bhagurkar wrote:So is it possible for me to implement a fork only for threads (just splitting the control) in my os withouth using virtual memory?
I think this should be entirely possible - all you need is a new stack. As with all threads, you just need to be careful about locking accesses to memory, which will now be shared between parent and child user threads.

Cheers,
Adam

Re: Fork in linux

Posted: Tue Jul 29, 2008 2:07 am
by JamesM
paraphrased wrote:I want a fork-like function that just spawns threads
Try clone(). Clone() is how linux implements threading - the pthread library uses it directly.

Re: Fork in linux

Posted: Tue Jul 29, 2008 3:25 am
by Solar
Implementing anything "fork()-like" without virtual memory is a royal pain in the backside, to the point of "don't bother".

Why, you ask?

Well, if you don't do virtual memory, i.e. no memory protection, any address at any location could actually be a pointer to some other location. Unless you have complete control over all code running on your system (e.g. a completely virtualized runtime environment including reference checking), you simply cannot know what you would have to copy.

Depending on how your executable / library loading is designed, copied code would also require attention. You might have to make sure that any relative addressing referring to other code (e.g. libraries) is getting patched, while any relative addressing referring to the code itself remains untouched.

All this would imply lots of additional bookkeeping, and would tend to be never truly "complete".

How do I know? Because AmigaOS - on which I "grew up" - faced the exact same problem. There was a Unix compatibility layer (libnix) that was capable of running virtually everything written for a Unix, with one big ugly unfixable exception: fork()...

Re: Fork in linux

Posted: Tue Jul 29, 2008 8:04 am
by jal
Solar wrote:Implementing anything "fork()-like" without virtual memory is a royal pain in the backside, to the point of "don't bother".
I agree with what you are saying, I'm just wondering how old UNIX-clones like XENIX and Minix run on the 8086. They supply fork().


JAL

Re: Fork in linux

Posted: Tue Jul 29, 2008 11:12 am
by salil_bhagurkar
@Solar, thats exactly what i said in my first post when i started the thread. If we didn't have virtual memory, we wouldn't know how to copy stuff in fork....
But then like @jal says how do these unix clones for 8086 do it....?

Re: Fork in linux

Posted: Tue Jul 29, 2008 12:24 pm
by Solar
As I never programmed the 8086 and am not familiar with MINIX / XENIX designs, this is somewhat like blindfolding me and demand I point into the right direction.

However, a gut feeling tells me... segments? ;)

Re: Fork in linux

Posted: Wed Jul 30, 2008 1:00 am
by salil_bhagurkar
Oh! thats right. Why do you think thats gut feeling... I guess that could be the most convincing answer... You keep different segments but the ip/sp is same for different processes. Hence they 'think' they are at the same memory location. Hence the problem of invalid memory references should not occur with forked children...

Re: Fork in linux

Posted: Wed Jul 30, 2008 1:14 am
by jnc100
As I recall, at least for 8086 Minix, that (segments) is exactly how they do it. I presume it means you can't use far pointers though.

Regards,
John.