鄭卓彬 + 原創(chuàng)作品轉(zhuǎn)載請(qǐng)注明出處 + 《Linux內(nèi)核分析》MOOC課程http://mooc.study.163.com/course/USTC-1000029000
前言:
該實(shí)驗(yàn)泽裳,主要是老師讓我們理解內(nèi)核中進(jìn)程切換的原理瞒斩。
內(nèi)容:
代碼解析:
mypch.h
#define MAX_TASK_NUM? ? ? ? 4
#define KERNEL_STACK_SIZE? 1024*2?
/* CPU-specific state of this task */
struct Thread {
unsigned long ip;
unsigned long sp;
};
typedef struct PCB{
int pid;
volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
unsigned long stack[KERNEL_STACK_SIZE];
/* CPU-specific state of this task */
struct Thread thread;
unsigned long task_entry;
struct PCB *next;
}tPCB;
void my_schedule(void);
解析:該頭文件主要定義了如下兩個(gè)結(jié)構(gòu)
1、Thread : 線程涮总,里面就定義了 ip胸囱、sp兩個(gè)屬性,分別用來(lái)存儲(chǔ)線程切換時(shí)的eip和esp的值妹卿,在下回調(diào)度該進(jìn)程時(shí)可以用該線程回推出進(jìn)程的上下文旺矾。
2、PCB:進(jìn)程管理塊夺克,里面存儲(chǔ)了狀態(tài)變量箕宙、進(jìn)程棧、管理的進(jìn)程铺纽、下一個(gè)調(diào)度的pcb的指針柬帕。
mymain.c
#include
#include
#include
#include
#include
#include "mypcb.h"
tPCB task[MAX_TASK_NUM];
tPCB * my_current_task = NULL;
volatile int my_need_sched = 0;
void my_process(void);
void __init my_start_kernel(void)
{
int pid = 0;
int i;
/* Initialize process 0*/
task[pid].pid = pid;
task[pid].state = 0;/* -1 unrunnable, 0 runnable, >0 stopped */
task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process;
task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1];
task[pid].next = &task[pid];
/*fork more process */
for(i=1;i
{
memcpy(&task[i],&task[0],sizeof(tPCB));
task[i].pid = i;
task[i].thread.sp = (unsigned long)&task[i].stack[KERNEL_STACK_SIZE-1];
*((unsigned long *)task[i].thread.sp - 1) = task[i].thread.sp;
task[i].thread.sp -= 1;
task[i].next = task[i-1].next;
task[i-1].next = &task[i];
}
/* start process 0 by task[0] */
pid = 0;
my_current_task = &task[pid];
asm volatile(
"movl %1,%%esp\n\t" /* set task[pid].thread.sp to esp */
"pushl %1\n\t" ? ? ? ? /* push ebp */
"pushl %0\n\t" ? ? ? ? /* push task[pid].thread.ip */
"ret\n\t" ? ? ? ? ? ? /* pop task[pid].thread.ip to eip */
"popl %%ebp\n\t"
:
: "c" (task[pid].thread.ip),"d" (task[pid].thread.sp) /* input c or d mean %ecx/%edx*/
);
}
void my_process(void)
{
int i = 0;
while(1)
{
i++;
if(i%10000000 == 0)
{
printk(KERN_NOTICE "this is process %d -\n",my_current_task->pid);
if(my_need_sched == 1)
{
my_need_sched = 0;
my_schedule();
}
printk(KERN_NOTICE "this is process %d +\n",my_current_task->pid);
}
}
}
解析:
1、__init my_start_kernel ?方法是內(nèi)核啟動(dòng)后會(huì)執(zhí)行的方法:
? ? 在方法里我們定義了4個(gè)線程,并使他們形成一個(gè)環(huán)陷寝,內(nèi)核一開始就調(diào)用進(jìn)程0.之后進(jìn)程0執(zhí)行my_process方法锅很,
2、my_process 方法是每個(gè)進(jìn)程會(huì)執(zhí)行的函數(shù):
? ? 在該方法里凤跑,會(huì)一直反復(fù)循環(huán)爆安,每循環(huán)100000000次,就會(huì)檢查進(jìn)程的狀態(tài)仔引,如果狀態(tài)值等于1則會(huì)執(zhí)行進(jìn)程的調(diào)度扔仓,然后下一個(gè)進(jìn)程進(jìn)入執(zhí)行。
myinterrupt.c
#include
#include
#include
#include
#include
#include "mypcb.h"
extern tPCB task[MAX_TASK_NUM];
extern tPCB * my_current_task;
extern volatile int my_need_sched;
volatile int time_count = 0;
void my_schedule(void)
{
tPCB * next;
tPCB * prev;
if(my_current_task == NULL
|| my_current_task->next == NULL)
{
return;
}
printk(KERN_NOTICE ">>>my_schedule<<<\n");
/* schedule */
next = my_current_task->next;
prev = my_current_task;
if(next->state == 0)/* -1 unrunnable, 0 runnable, >0 stopped */
{
my_current_task = next;
printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid);
/* switch to next process */
asm volatile(
"pushl %%ebp\n\t" ? ? /* save ebp */
"movl %%esp,%0\n\t" /* save esp */
"movl %2,%%esp\n\t"? ? /* restore? esp */
"movl $1f,%1\n\t"? ? ? /* save eip */
"pushl %3\n\t"
"ret\n\t" ? ? ? ? ? ? /* restore? eip */
"1:\t"? ? ? ? ? ? ? ? ? /* next process start here */
"popl %%ebp\n\t"
: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
: "m" (next->thread.sp),"m" (next->thread.ip)
);
}
return;
}
解析:該調(diào)度咖耘,使用了匯編代碼翘簇,在匯編代碼中,將下個(gè)進(jìn)程的sp和ip放入了cpu的esp和eip中儿倒,讓cpu去該進(jìn)程的ip取指版保,并將棧頂寄存器esp指向了該線程的棧頂。
運(yùn)行結(jié)果:
總結(jié):
進(jìn)程的切換第一步:就是當(dāng)前進(jìn)程上下文夫否,在本文中就是sp和ip值彻犁。
第二步:將下一個(gè)進(jìn)程的上下文提取到cpu中。在本文就是將sp和ip的值分別取到esp和eip中慷吊。
第三步:cpu直接執(zhí)行eip地址的指令袖裕。這樣進(jìn)程就完成了切換曹抬。
所以溉瓶,操作系統(tǒng)就是一直運(yùn)行著的,對(duì)進(jìn)程進(jìn)行調(diào)度谤民,資源進(jìn)行管理堰酿,文件管理的連接軟件和硬件的程序。