OSDev.org
https://forum.osdev.org/

How do I fork/create a new process?
https://forum.osdev.org/viewtopic.php?f=1&t=33441
Page 1 of 1

Author:  Jush [ Mon Jan 14, 2019 5:12 am ]
Post subject:  How do I fork/create a new process?

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.

Author:  bzt [ Tue Jan 15, 2019 6:11 am ]
Post subject:  Re: How do I fork/create a new process?

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

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/