⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scheduler.c

📁 一个操作系统的源代码
💻 C
字号:
/** scheduler.c                                   **                                                               ** Original Author: Kasper Verdich Lund                          ** Date: 11.13.99      **  ** Description: ** Scheduler      **                                        ** Revision History:  ** First version      ** ** 0.0.1 11.13.99 Kasper Verdich Lund - (no comments) ** ** This program is free software, you can redistribute it and/or ** modify it under the terms of the GNU General Public License   ** as published by the Free Software Foundation; either version  ** 2 of the License, or (at your option) any later version.      **                                                               ** This program is distributed in the hope that it will be       ** useful, but WITHOUT ANY WARRANTY; without even the implied    ** warranty or MERCHANTABILITY or FITNESS FOR A PARTICULAR       ** PURPOSE.  See the GNU General Public License for more        ** details.                                                      **                                                               ** You should have received a copy of the GNU General Public     ** License along with this program; if not, write to the         ** Free Software Foundation, Inc., 59 Temple Place, Suite 330,   ** Boston, MA 02111-1307 USA                                     **                                                               *********************************************************Apostle OS**/#include <scheduler.h>#include <activation.h>#include <idt.h>#include <io.h>#include <gdt.h>#include <halt.h>#include <pic.h>#include <process.h>#include <request.h>#ifdef DEBUG#include <debug/conio.h>#endifActivation *currentActivation;Process *currentProcess;ProcessQueue readyQueue = NULL;RequestQueue kernelRequests = NULL;long currentTick = 0;void setupScheduler(void){  interruptIDT(&IDT[32], KERNEL_CS, (dword)&__SchedulerISR, 0);  unmaskIRQ(0);}void Scheduler(dword edi, dword esi, dword ebp,	       dword ebx, dword edx, dword ecx, dword eax,	       dword eip, dword cs, dword eflags, 	       dword esp, dword ss){  Request *request;  /* increase current ticks */  currentTick++;  /* acknowledge IRQ */  outb(0x20, 0x20);#ifdef DEBUG  if (currentActivation == NULL)    {      DebugPrintF("Scheduling with activation == NULL?");      halt();    }#endif  /* save current state in ACB */  currentActivation->eax = eax;  currentActivation->ebx = ebx;  currentActivation->ecx = ecx;   currentActivation->edx = edx;  currentActivation->esi = esi;  currentActivation->edi = edi;  currentActivation->eflags = eflags;  currentActivation->eip = eip;  currentActivation->esp = esp;   currentActivation->ebp = ebp;        /* if kernel request then */  if ((request = TimedOutRequest(&kernelRequests)) != NULL)	  ResumeRequest(request, 0);  else    {      if (currentProcess->singleThreaded)        {          /* process is single threaded */	  if (currentActivation->quantum <= 1)	    {	      InsertProcess(&readyQueue, currentProcess, FALSE);	      Reallocate();	    }	  else	    {	      currentActivation->quantum--;	      /* we haven't changed context or kernel stack */	      ProcessResume(currentActivation);	    }	}      else        {          /* process is multithreaded */	  if (currentActivation->quantum <= 1)	    {		      /* process.pending = deallocate(CPU) + preempt(current activation) */	      currentProcess->deallocated = 1;              currentProcess->pendingUpcallType = Preempted;              currentProcess->pendingActivation = currentActivation;	      /* reallocate CPU */	      InsertProcess(&readyQueue, currentProcess, FALSE);	      Reallocate();	    }	  else	    {		      /* create new activation */	      Activation *oldActivation = currentActivation;	      currentActivation = NewActivation();	      /* new activation.quantum = current activation.quantum - 1 */	      currentActivation->quantum = oldActivation->quantum - 1;	      /* upcall with preemption(old activation) + pending */	      setKernelStack(systemTSS, currentActivation->esp0);	      Upcall(currentProcess, 1, Preempted, oldActivation);	    }	}    }}void Reallocate(void){  Process *newProcess;  /* find process to allocate to */  if ((newProcess = RemoveProcess(&readyQueue)) == NULL)    {#ifdef DEBUG      DebugPrintF("No more processes!");#endif      halt();    }  if (newProcess->singleThreaded)    {      /* process is single threaded */      currentProcess = newProcess;      currentActivation = currentProcess->singleActivation;      currentActivation->quantum = QUANTUM;      set_CR3((dword)(currentProcess->addressSpace));      setKernelStack(systemTSS, currentActivation->esp0);      ProcessResume(currentActivation);    }  else    {      /* process is multithreaded */      currentActivation = NewActivation();      currentProcess = newProcess;      /* upcall with allocate(CPU) + pending */      set_CR3((dword)(currentProcess->addressSpace));      currentProcess->allocated = 1;      setKernelStack(systemTSS, currentActivation->esp0);      Upcall(currentProcess, 1, None, NULL);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -