Creating process
Creating process
How to create a process? I've never seen any explanation on the Wiki for this. I already have a memory manager (both physical and virtual) but no file system yet (of course I already have a console, keyboard driver, ISRs, IRQs, and any other pre-requirements). Is that enough?
Re: Creating process
Hi,
There's lots of details missing in my quick description though, like whether the scheduler runs processes or threads, and if the virtual address space is cloned (e.g. "fork()" with copy on write) or entirely new, and which things the new process inherits from the parent process (file handles, stdin, stdout, file permissions, environment, root directory, working directory, signal handling, CPU affinity, scheduling priority, etc).
Cheers,
Brendan
The general idea is to allocate a new virtual address space, create a structure or something describing the process (including which virtual address space and which EIP), and then tell the scheduler that the new process can be given CPU time. Then, when the scheduler gives the process some CPU time it'd start running some kernel code that gets the process ready (e.g. loads an executable file from disk, or sets up the anything else that's needed by the process). Then the kernel would "return" to the CPL=3 code (e.g. push CPL=3 values for EFLAGS, CS:EIP and SS:ESP on the stack and do an IRET).leledumbo wrote:How to create a process? I've never seen any explanation on the Wiki for this. I already have a memory manager (both physical and virtual) but no file system yet (of course I already have a console, keyboard driver, ISRs, IRQs, and any other pre-requirements). Is that enough?
There's lots of details missing in my quick description though, like whether the scheduler runs processes or threads, and if the virtual address space is cloned (e.g. "fork()" with copy on write) or entirely new, and which things the new process inherits from the parent process (file handles, stdin, stdout, file permissions, environment, root directory, working directory, signal handling, CPU affinity, scheduling priority, etc).
Cheers,
Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Re: Creating process
OK, the virtual memory manager can do that.allocate a new virtual address space
Well, that's not hard. I just need to know what info should be kept. Except for getting the EIP, I don't know how to do it. Something like:create a structure or something describing the process (including which virtual address space and which EIP)
Code: Select all
mov eax,eip
I guess this is where I really got lost. What is this scheduler? Is it a device or some other code?tell the scheduler that the new process can be given CPU time
- piranha
- Member
- Posts: 1391
- Joined: Thu Dec 21, 2006 7:42 pm
- Location: Unknown. Momentum is pretty certain, however.
- Contact:
Re: Creating process
The scheduler is the code that decides which task to run next, and then maybe load the task.
For me, the scheduler saves the state of the old task, decide which task to run next, and then load the new task and jump to it.
Check this out: http://jamesmolloy.co.uk/tutorial_html/ ... sking.html
-JL
For me, the scheduler saves the state of the old task, decide which task to run next, and then load the new task and jump to it.
Check this out: http://jamesmolloy.co.uk/tutorial_html/ ... sking.html
-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
Re: Creating process
I'll (re)read that (I already have the copy actually ).
Wow! So my OS is actually a combination of 3 tutorials: brendan, jamesmolloy, and brokenthorn. With a modification here and there plus my own craziness, of course .
Wow! So my OS is actually a combination of 3 tutorials: brendan, jamesmolloy, and brokenthorn. With a modification here and there plus my own craziness, of course .
Re: Creating process
Useleledumbo wrote:Except for getting the EIP, I don't know how to do it. Something like:fails with eip being undefined.Code: Select all
mov eax,eip
Code: Select all
db 0E8h, 00h, 00h ; call near $+3
pop eax ; load address of this instruction into eax
It's an algorithm you have to implement. On each timer interrupt, it selects the next thread to run. There are many variations to it, starting from simple round-robin to complex ones like the one used by the NT kernel. Besides this, you have to make it run very fast - those algorithms are the best that have a running time independent of the count of active threads (in other words, they have O(1) execution time).leledumbo wrote: I guess this is where I really got lost. What is this scheduler? Is it a device or some other code?
Re: Creating process
Thanks. But your code gives me:
Code: Select all
procedure DumpRegs;
var
reg: LongWord;
begin
asm
mov reg,eax
end;
WriteStrLn('EAX = '#9+HexStr(reg,8));
asm
mov reg,ebx
end;
WriteStrLn('EBX = '#9+HexStr(reg,8));
asm
mov reg,ecx
end;
WriteStrLn('ECX = '#9+HexStr(reg,8));
asm
mov reg,edx
end;
WriteStrLn('EDX = '#9+HexStr(reg,8));
asm
mov reg,esi
end;
WriteStrLn('ESI = '#9+HexStr(reg,8));
asm
mov reg,edi
end;
WriteStrLn('EDI = '#9+HexStr(reg,8));
asm
mov reg,esp
end;
WriteStrLn('ESP = '#9+HexStr(reg,8));
asm
mov reg,ebp
end;
WriteStrLn('EBP = '#9+HexStr(reg,8));
asm
db 0E8h, 00h, 00h
pop eax
mov reg,eax
end;
WriteStrLn('EIP = '#9+HexStr(reg,8));
asm
mov reg,cs
end;
WriteStrLn('CS = '#9+HexStr(reg,8));
asm
mov reg,ds
end;
WriteStrLn('DS = '#9+HexStr(reg,8));
asm
mov reg,es
end;
WriteStrLn('ES = '#9+HexStr(reg,8));
asm
mov reg,fs
end;
WriteStrLn('FS = '#9+HexStr(reg,8));
asm
mov reg,gs
end;
WriteStrLn('GS = '#9+HexStr(reg,8));
asm
mov reg,ss
end;
WriteStrLn('SS = '#9+HexStr(reg,8));
end;
What? Round robin is simple? Perhaps it's just my foolishness that I think of it as complicated (I got it in OS class). I just want to use FCFS, I don't care how bad, I just want to see that I can make it.... starting from simple round-robin ...
Re: Creating process
Does your Pascal compiler preserve all registers across procedure calls? If not, your DumpRegs procedure is flawed - it will print incorrect values for some registers. A procedure like this would work regardless of your compiler preserving registers or not:leledumbo wrote:Code: Select all
procedure DumpRegs; ...
Code: Select all
Procedure DumpRegs2;
Var
Reg : Array[16] of LongWord;
SReg : Array[6] of Word;
Eip : LongWord;
EFlags : LongWord;
Begin
Asm
; save registers while they are not modified by another procedure call. note that depending
; on your compiler settings, ebp may already be trashed (stack frame)
mov dword ptr Reg[4*0],eax
mov dword ptr Reg[4*1],ecx
mov dword ptr Reg[4*2],edx
mov dword ptr Reg[4*3],ebx
mov dword ptr Reg[4*4],esp ; esp is already incorrect since it was decreased by the amount of stack space the local variables require
mov eax, 16*4 + 6*2 + 4 + 4
add dword ptr Reg[4*4], eax ; correct esp
mov dword ptr Reg[4*5],ebp
mov dword ptr Reg[4*6],esi
mov dword ptr Reg[4*7],edi
; save segment registers
mov word ptr SReg[2*0], ds
mov word ptr SReg[2*1], es
mov word ptr SReg[2*2], cs
mov word ptr SReg[2*3], ss
mov word ptr SReg[2*4], fs
mov word ptr SReg[2*5], gs
; save eflags
pushfd
pop dword ptr EFlags
; now get eip.
call $+3
pop dword ptr Eip
End;
WriteLn('EAX = '#9 + HexStr(Reg[0],8));
WriteLn('ECX = '#9 + HexStr(Reg[1],8));
WriteLn('EDX = '#9 + HexStr(Reg[2],8));
WriteLn('EBX = '#9 + HexStr(Reg[3],8));
// etc.
End;
Code: Select all
mov eax, [esp+4 + size_of_local_variables] ; if no stack frames
mov eax, [ebp+4] ; if stack frames are generated
Actually, Round Robin is a preemptive version of First Come First Serve, if I remember my classes correctly; and as such, FCFS is suited for cooperative multitasking, where all applications voluntarily give up their time slice sooner or later. RR does not require cooperation from applications.What? Round robin is simple? Perhaps it's just my foolishness that I think of it as complicated (I got it in OS class). I just want to use FCFS, I don't care how bad, I just want to see that I can make it.
Edit: clarified difference between RR and FCFS.
Last edited by ru2aqare on Wed Nov 19, 2008 4:28 am, edited 1 time in total.
Re: Creating process
Ah... I didn't notice that. I use Free Pascal so the default calling convention would be Register. Since each WriteStrLn expects one argument, the value of EAX will be altered on each call. I should've known that. Everything works fine now, thanks.
P.S.: That's the way Oberon declare arrays, not Pascal
P.S.: That's the way Oberon declare arrays, not Pascal
Re: Creating process
Okay, good to hear that. And it seems I have seriously forgotten how to Pascal. I have been using C for the last eight years or so.leledumbo wrote:Everything works fine now, thanks.
P.S.: That's the way Oberon declare arrays, not Pascal
Re: Creating process
HI.
You know I've got the same problem with Jame's tutorial (e.g. pagefault on trying to create process). I was not able to find the solution, so I left that problem. Maybe someone know the solution?
Thanks!
You know I've got the same problem with Jame's tutorial (e.g. pagefault on trying to create process). I was not able to find the solution, so I left that problem. Maybe someone know the solution?
Thanks!
Don't think a ****, but in ukrainian schools English is TOO BAD!