Page 2 of 2

Re: Fork and Processes

Posted: Mon Mar 02, 2009 10:04 am
by yemista
Creature wrote:
yemista wrote:Why do you read the up? If schedule process is called doesnt that mean the process has switched?
Schedule process is called when a timer interrupt occurs and switches processes all the time. Since I'm using JamesM's tutorials, I'm going to quote what James says (he explains best):
JamesM's Tutorials wrote: Read the instruction pointer. We do some cunning logic here:
One of two things could have happened when this function exits -
(a) We called the function and it returned the EIP as requested.
(b) We have just switched tasks, and because the saved EIP is essentially the instruction after read_eip(), it will seem as if read_eip has just returned.

In the second case we need to return immediately. To detect it we put a dummy
value in EAX further down at the end of this function. As C returns values in EAX,
it will look like the return value is this dummy value! (0x12345).
This is what happens at the ReadEIP() in 'ScheduleProcess'.
I think I am missing something here, but the way I understand how the scheduler should work is that it figures out what process to run next, saves the current context, and then restores the context of the process it wants to run, and when the iret is executed, you have set things up so that the eip of the processs you want to run is on the stack, so it smoothly transitions as if nothing happened but an interrupt occured and returned.

Re: Fork and Processes

Posted: Mon Mar 02, 2009 10:24 am
by Creature
I think I understand what you mean. So you're saying the unsigned integers 'ESP' and 'EBP' should better not be created because they mess up my stack and the variables I have to read? So, if I don't allocate them, would it be better to do something like:

Code: Select all

/* Is this the parent process? */
	if(CurrentProcess == Parent)
	{
		/* Set up the child process. */
		ASMV("mov %%ESP, %0" : "=r" (RetProcess->ESP) :);
		ASMV("mov %%EBP, %0" : "=r" (RetProcess->EBP) :);

		RetProcess->EIP = EIP;

		/* Re-enable interrupts. */
		ASMV("sti");

		return RetProcess->PID;
	}
So the Assembly code writes the values directly into the structure without a variable being created up front?

Re: Fork and Processes

Posted: Mon Mar 02, 2009 12:38 pm
by yemista
Well it might be a little different since you are forking a process rather than just switching it, but what I mean is that since the scheduler is an interrupt, when it gets called, the context it was being called from, ie all the registers, should be on the stack. So the scheduler has to figure out what to run next, then store all the values of the registers from the stack info a structure for that process, and then get all the register contents from the new process, put them all on the stack, and when it returns it will return as if that process was running the whole time. And yes, this includes esp and ebp and the stack should balance out after the return.