操作系统进程切换:
main(){
if(!fork()) {while(1) printf("A");}
if(!fork()) {while(1) printf("B");}
wait();
}
---》
main(){
mov _NR_fork,%eax
int 0x80
100:mov %eax,res 将返回值传给res
cmpl res,0 判断res是否等于0
AB的用户栈转到内核栈
(2)set_system_gata(0x80,&system_call)
(3) system_call:
call sys_call_table (%eax,4)
sys_fork:
push1...
call copy_process
copy_process(.....long eip....){
p=(PCB*) get_free_page();
p->tss.esp0=p+4K;
p->tss.esp=esp;
p->tss.eax=0;p->tss.eip=eip......
}建立PCB结构块
如何实现AB的切换:
main(){
......wait(); mov _NR_wait
int 0x80
system_call:
call sys_waitpid
sys_waitpid(){
current->state=TASK_INTERRUPTIBL 将当前进程转成阻塞状态
schedule(); 调度
}
schedule(){
if((*p)->state==TASK_RUNNING && (*p)->count>c)
c=(*p)->counter,next=i;
.....
switch_to(next);
}
时钟中断:
void sched_init(void)
{
set_intr_gate(0x20,&timer_interrupt);
void _timer_interruput;
........
call do_timer
void do_timer(){
if((--current->count>0) return;
current->count=0;
schedule();
}
}
jne 208 跳到208
200:printf(“A”)
jmp200
208:。。。。。
304:wait()
}
汇编前序:
dw:定义以字型位单位,一个字型占2个内存空间
在代码段中使用栈:
定义数据,定义0来分配栈的内存空间,在循环入栈
ds,cs,ss:8086CPU不允许将一个数值直接送入段寄存器中,需要通用寄存器ax等来转接
段总是16的倍数,及时没占满也是要分配的,17就需要2个段
and指令:通过该指令可将操作对象的相应位设为0,其他位不变。
or指令:通过该指令可将操作对象的相应位设为1,其他位不变。
大写字母(前)与小写字母的ASCⅡ码值差20H,32,也是二进制第五位0,1之差