mockers
Would you like to react to this message? Create an account in a few clicks or log in to continue.

fork-mayank pandey

2 posters

Go down

fork-mayank pandey Empty fork-mayank pandey

Post  Admin Tue Feb 03, 2009 11:35 pm

can anybody really explain how fork works??

Admin
Admin

Posts : 47
Join date : 2009-01-29

https://mockers.forumotion.com

Back to top Go down

fork-mayank pandey Empty Re: fork-mayank pandey

Post  Beagle Tue Feb 03, 2009 11:37 pm

Algorithm of fork:-

{
...........check for available kernel resources;
...........get free proc table slot, unique PID number;
...........check that user not running too many processes;
...........mark child state "being copied";
...........copy data from parent proc table slot to new child slot;
...........increment counts on current directory inode and changed
root(if applicable);
...........increment open file counts in file table;
...........make copy of parent context (u area, text , data, stack) in memory;
...........push dummy system level context layer onto child system level contet;
..................dummy context contains data allowing child process
to recognize itself, and start running from here when
..................scheduled;
...........if( executing process is parent process)
...........{
..................change child state to "ready to run";
..................return(child ID); /* from system to user */
...........}
...........else /* executing process is the child process */
...........{
..................initialize u area timing fields;
..................return (0);
...........}
}


The kernel sets the program counter and other registers in the saved
register context so that it can "restore" the child context, even
though it had never executed before, and so that the child process can
recognize itself as the child when it runs. For instance, if the
kernel code tests the value of register 0 to decide if the process is
the parent or the child, it writes the appropriate value in the child
saved register context layer 1.

When the child context is ready, the parent completes its part of fork
by changing the child state to "ready to run ( in memory) " and by
returning the child process ID to the user. The kernel later schedules
the child process for execution via the normal scheduling algorithm,
and the child process "completes" it's part of the fork. The context
of the child process was set up by the parent process; to the kernel,
the child process appears to have awakened after awaiting a resource.
The child process executes part of the code for the fork system call,
according to the program counter that the kernel restored from the
saved register context in context layer 2, and returns a 0 from the
system call.

Now according to the implementation, there are two copies of fork
created; one for the parent process itself (which actually makes this
system call to be handled by the kernel) and one for the child process
( which is not executed at all ). When the kernel is checking for the
available resources and doing all the work prior to the if - else
part, the kernel uses the register values to store some predefined
values for the child and the parent. Through this register value, the
child recognizes itself. Once the child recognizes itself, and the
kernel is done with the scheduling of the child process (through nice
value of the parent, initial CPU usage, etc.) The kernel finds the
child of the process which created it. So having two copies of fork,
the kernel checks which process it's running currently. If it's
running the parent process, the Child ID is returned to the process.
If the running process is the child (already identified by the kernel
and the child itself), a 0 value is returned by the kernel. The kernel
returns the value because it is the one who is handling the fork
system call.

I think I am clear in my explanation regarding the two values. In
actual scenario, Fork() does return a single value.

Correct me where I am going wrong,

Beagle

Posts : 40
Join date : 2009-01-30

Back to top Go down

fork-mayank pandey Empty Re: fork-mayank pandey

Post  Admin Wed Feb 04, 2009 2:01 am

reference:
Chapter:Process Creation
Book:The Design of Unix operating system.

Admin
Admin

Posts : 47
Join date : 2009-01-29

https://mockers.forumotion.com

Back to top Go down

fork-mayank pandey Empty Re: fork-mayank pandey

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum