1

我正在测试一个在dispatch()函数中实现的上下文切换。据我了解,当我在中断计时器内调用调度时会出现问题。如果我这样做,它甚至不会编译!奇怪的是,如果我实现调度只是为了调用定时器中断和定时器来进行上下文切换,它可以完美地工作。我试图让调度宏或内联函数,但问题是一样的 - 当我尝试构建它时,发生了一些意外的异常?

这足以让你告诉我有什么问题吗?

class PCB{ // this class represents a thread
  public:
unsigned sp;
unsigned ss;
Status status;
Time kvant; // time slice
static PCB* running;

void createProcess(void (*body)());
};

PCB* PCB::running = 0;

void PCB::createProcess(void (*body)()){

unsigned* stek = new unsigned[1024];

st1[1023] =0x200; // PSW register with I flag setted to be 1
st1[1022] = FP_SEG(body);
st1[1021] = FP_OFF(body);

this->sp = FP_OFF(st1+1012); // space for registers ax,bx,cx,d,si,es,ds,di,bp
this->ss = FP_SEG(st1+1012);

this->status = NEW;
}

void interrupt timer(){
timer_counter--; 
if (timer_counter == 0) dispatch(); 

 else asm int 60h; // invoking the old routine
}

void interrupt dispatch(){
asm cli;
    asm {
        // store sp
        mov tsp, sp
        mov tss, ss
    }

    PCB::running->sp = tsp;
    PCB::running->ss = tss;

    cout<<"Context change!"<<endl;

    if(PCB::running->status !=END){
     PCB::running->status = READY;
     Scheduler::put(PCB::running);
    }
    PCB::running=Scheduler::get();
    PCB::running->status = RUNNING;

    tsp = PCB::running->sp;
    tss = PCB::running->ss; 

    timer_counter = PCB::running->kvant;

    asm {
        mov sp, tsp   // restore sp
        mov ss, tss
    } 

    asm sti;
}
4

0 回答 0