Presentation is loading. Please wait.

Presentation is loading. Please wait.

A very simple kernel Anders P. Ravn April 2005. A kernel specification /* kernel.h Interface to a lightweight kernel that implements concurrent processes.

Similar presentations


Presentation on theme: "A very simple kernel Anders P. Ravn April 2005. A kernel specification /* kernel.h Interface to a lightweight kernel that implements concurrent processes."— Presentation transcript:

1 A very simple kernel Anders P. Ravn April 2005

2 A kernel specification /* kernel.h Interface to a lightweight kernel that implements concurrent processes and a release primitive `pause'. Anders P. Ravn, DTU, Denmark 24 April 1998 */ typedef void (*Program)(void); /* A program text is a function without parameters*/ typedef void * Thread; /* identifier for a process */ extern Thread create(Program p,unsigned int stacksize); /* creates a process with a stack of the specified size and starts it executing the program. If there is insufficient memory, the result is NULL */ extern void pause(void); /* release the processor */

3 Multiprogramming #include ”kernel.h” void process() { /* do something */ pause(); /* do something */ } void main() { Thread p1, p2; p1 = create(&process,2000); /* p1 is started */ p2 = create(&process,1000); /* p2 is started */ /* the kernel will see to it that main is left when both p1 and p2 has exited */ }

4 A kernel implementation I typedef unsigned long Register; typedef struct x {struct x* next; Register esp;} Threaddescriptor; static Threaddesriptor* ready; /* queue of threads linked cyclically; ready points to last, the first is current */ #define current ready->next esp1esp2esp3 ready: current

5 A kernel implementation II void pause() { Register sp; __asm__(” pushal movl %esp,%sp"); /* sp -> |saved registers... | eip return from call */ DISABLE; /* scheduling */ current->esp = sp; ready = current; sp = current->esp; __asm__(" movl %sp,%esp popal" ); ENABLE; } esp1esp2esp3 ready: current stack1 stack2

6 A kernel implementation III pause:pushl %ebp movl %esp,%ebp pushal movl %esp,%ecx sp = esp movl ready,%eax movl (%eax),%edx current->esp movl %ecx,4(%edx) = sp movl %edx,ready ready = current movl (%edx),%edx movl 4(%edx),%ecx sp = current->esp movl %ecx,%esp popal leave ret

7 A kernel implementation IV pause: pushal movl ready,%eax movl (%eax),%edx current->esp movl %esp,4(%edx) = esp movl %edx,ready ready = current movl (%edx),%edx movl 4(%edx),%esp esp = current->esp popal ret


Download ppt "A very simple kernel Anders P. Ravn April 2005. A kernel specification /* kernel.h Interface to a lightweight kernel that implements concurrent processes."

Similar presentations


Ads by Google