OSDev.org

The Place to Start for Operating System Developers
It is currently Wed Jun 19, 2019 2:54 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: How do I fork/create a new process?
PostPosted: Mon Jan 14, 2019 5:12 am 
Offline

Joined: Fri Jan 11, 2019 7:59 am
Posts: 6
I wan't to create a process to run my shell, run a signal calling program and possibly more in the future. I don't want to use the GNU C Library.


Top
 Profile  
 
 Post subject: Re: How do I fork/create a new process?
PostPosted: Tue Jan 15, 2019 6:11 am 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 298
Hi,
Jush wrote:
I wan't to create a process to run my shell, run a signal calling program and possibly more in the future. I don't want to use the GNU C Library.

The GNU C library has nothing to do with forking a process. It just contains a fork() function which is nothing more than a syscall wrapper, but the real magic is done on the kernel side.

Steps you need to create a new process with fork:
1. create a new address space (could be done on-demand, with copy-on-write method)
2. register the new address space in the process list, and with that, get the pid of the new process
3. alter process state so that the syscall returns the pid for the parent and zero for the child
4. add the new process to the scheduler's list as an active, executable process
5. return from syscall, let the scheduler code multiplex the parent and the child process

Creating a new address space refers to many things. The point is, duplicate everything that makes up your process (stack, paging tables, file descriptors etc.) This probably includes the copying of the paging tables (but not necessarily, depends on your design). You may need to copy the file descriptors as well: if those are stored within the address space, then step 1 has already done that, but for a micro-kernel design you need to inform the FS task to duplicate the descriptors, etc.

The basic difference between the UNIXy fork+exec approach and Win's CreateProcess is that in UNIX fork creates an identical copy of the address space, then the code path for fork()==0 uses exec() to overwrite the text segment (but keeps the copy of the data segment). On the other hand CreateProcess creates a brand new empty address space with a new text and data segment. Everything that needs to be set in the data segment has to be passed as an argument to the call. Which one you prefer is totally up to you, that's your choice.

Cheers,
bzt


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group