Fork in linux

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

Fork in linux

Post 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...
jnc100
Member
Member
Posts: 777
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: Fork in linux

Post 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.
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

Re: Fork in linux

Post 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..
jnc100
Member
Member
Posts: 777
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: Fork in linux

Post by jnc100 »

I say again, fork creates a new process, rather than a new thread.

Regards,
John.
blackcatcoder
Member
Member
Posts: 132
Joined: Wed Nov 03, 2004 12:00 am
Location: Austria
Contact:

Re: Fork in linux

Post 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.
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

Re: Fork in linux

Post 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?
blackcatcoder
Member
Member
Posts: 132
Joined: Wed Nov 03, 2004 12:00 am
Location: Austria
Contact:

Re: Fork in linux

Post 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.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Fork in linux

Post 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
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Fork in linux

Post 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.
User avatar
Solar
Member
Member
Posts: 7637
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Fork in linux

Post 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()...
Every good solution is obvious once you've found it.
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: Fork in linux

Post 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
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

Re: Fork in linux

Post 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....?
User avatar
Solar
Member
Member
Posts: 7637
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Fork in linux

Post 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? ;)
Every good solution is obvious once you've found it.
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

Re: Fork in linux

Post 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...
jnc100
Member
Member
Posts: 777
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: Fork in linux

Post 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.
Post Reply