Linux2.4.x是一个基于非抢占式的多任务的分时操作系统,虽然在用户进程的调度上采用抢占式策略,但是而在内核还是采用了轮转的方法,如果有个内核态的线程恶性占有CPU不释放,那系统无法从中解脱出来,所以实时性并不是很强。这种情况有望在Linux 2.6版本中得到改善,在2.6版本中采用了抢占式的调度策略。
内核中根据任务的实时程度提供了三种调度策略:
① SCHED_OTHER为非实时任务,采用常规的分时调度策略;
② SCHED_FIFO为短小的实时任务,采用先进先出式调度,除非有更高优先级进程申请运行,否则该进程将保持运行至退出才让出CPU;
③ SCHED_RR任务较长的实时任务,由于任务较长,不能采用FIFO的策略,而是采用轮转式调度,该进程被调度下来后将被置于运行队列的末尾,以保证其他实时进程有机会运行。
在上述三种调度策略的基础上,进程依照优先级的高低被分别调系统。优先级是一些简单的整数,它代表了为决定应该允许哪一个进程使用CPU的资源时判断方便而赋予进程的权值——优先级越高,它得到CPU时间的机会也就越大。
在Linux中,非实时进程有两种优先级,一种是静态优先级,另一种是动态优先级。实时进程又增加了第三种优先级,实时优先级。
① 静态优先级(priority)——被称为“静态”是因为它不随时间而改变,只能由用户进行修改。它指明了在被迫和其它进程竞争CPU之前该进程所应该被允许的时间片的值(20)。
② 动态优先级(counter)——counter 即系统为每个进程运行而分配的时间片,Linux兼用它来表示进程的动态优先级。只要进程拥有CPU,它就随着时间不断减小;当它为0时,标记进程重新调度。它指明了在当前时间片中所剩余的时间量(初为20)。
③ 实时优先级(rt_priority)——值为1000。Linux把实时优先级与counter值相加作为实时进程的优先权值。较高权值的进程总是优先于较低权值的进程,如果一个进程不是实时进程,其优先权就远小于1000,所以实时进程总是优先。
在每个tick到来的时候(也就是时钟中断发生),系统减小当前占有CPU的进程的counter,如果counter减小到0,则将need_resched置1,中断返回过程中进行调度。update_process_times()为时钟中断处理程序调用的一个子函数:
void update_process_times(int user_tick)
{
struct task_struct *p = current;
int cpu = smp_processor_id(), system = user_tick ^ 1;
update_ONe_process(p, user_tick, system, cpu);
if (p->pid) {
if (--p->counter <= 0) {
p->counter = 0;
p->need_resched = 1;
}
if (p->nice > 0)
kstat.per_cpu_nice[cpu] = user_tick;
else
kstat.per_cpu_user[cpu] = user_tick;
kstat.per_cpu_system[cpu] = system;
} else if (local_bh_count(cpu) || local_irq_count(cpu) > 1)
kstat.per_cpu_system[cpu] = system;
}
Linux中进程的调度使在schedule()函数中实现的,该函数在下面的ARM汇编片断中被调用到:
/*
* This is the fast syscall return path. We do as little as
* possible here, and this includes saving r0 back into the SVC
* stack.
*/
ret_fast_syscall:
ldr r1, [tsk, #TSK_NEED_RESCHED]
ldr r2, [tsk, #TSK_SIGPENDING]
teq r1, #0 need_resched || sigpending
teqeq r2, #0
bne slow
fast_restore_user_regs
/*
* Ok, we need to do extra processing, enter the slow path.
*/
slow: str r0, [sp, #S_R0 S_OFF]! returned r0
b 1f
/*
* "slow" syscall return path. "why" tells us if this was a real syscall.
*/
reschedule:
bl SYMBOL_NAME(schedule)
ENTRY(ret_to_user)
ret_slow_syscall:
ldr r1, [tsk, #TSK_NEED_RESCHED]
ldr r2, [tsk, #TSK_SIGPENDING]
1: teq r1, #0 need_resched => schedule()
bne reschedule teq r2, #0 sigpending => do_signal()
blne __do_signal
restore_user_regs
而这段代码在中断返回或者系统调用返回中反复被调用到:
进程状态转换时: 如进程终止,睡眠等,当进程要调用sleep()或exit()等函数使进程状态发生改变时,这些函数会主动调用schedule()转入进程调度。
可运行队列中增加新的进程时。
ENTRY(ret_from_fork)
bl SYMBOL_NAME(schedule_tail)
get_current_task tsk
ldr ip, [tsk, #TSK_PTRACE] check for syscall tracing
mov why, #1
tst ip, #PT_TRACESYS are we tracing syscalls?
beq ret_slow_syscall
mov r1, sp
mov r0, #1 trace exit [IP = 1]
bl SYMBOL_NAME(syscall_trace)
b ret_slow_syscall
③ 在时钟中断到来后:Linux初始化时,设定系统定时器的周期为10毫秒。当时钟中断发生时,时钟中断服务程序timer_interrupt立即调用时钟处理函数do_timer( ),在do_timer()会将当前进程的counter减1,如果counter为0则置need_resched标志,在从时钟中断返回的过程中会调用schedule.
④进程从系统调用返回到用户态时;判断need_resched标志是否置位,若是则转入执行schedule()。系统调用实际上就是通过软中断实现的,下面是ARM平台下软中断处理代码:
.align 5
ENTRY(vector_swi)
save_user_regs
zero_fp
get_scno
enable_irqs ip
str r4, [sp, #-S_OFF]! push fifth arg
get_current_task tsk
ldr ip, [tsk, #TSK_PTRACE] check for syscall tracing
bic scno, scno, #0xff000000 mask off SWI op-code
eor scno, scno, #OS_NUMBER 《 20 check OS number
adr tbl, sys_call_table load syscall table pointer
tst ip, #PT_TRACESYS are we tracing syscalls?
bne __sys_trace
adrsvc al, lr, ret_fast_syscall
装载返回地址,用于在跳转调用后返回到上面的代码片断中的
ret_fast_syscall
cmp scno, #NR_syscalls check upper syscall limit
ldrcc pc, [tbl, scno, lsl #2] call sys_* routine
add r1, sp, #S_OFF
2: mov why, #0 no longer a real syscall
cmp scno, #ARMSWI_OFFSET
eor r0, scno, #OS_NUMBER 《 20 put OS number back
bcs SYMBOL_NAME(arm_syscall)
b SYMBOL_NAME(sys_ni_syscall) not private func
⑤ 内核处理完中断后,进程返回到用户态。
⑥ 进程主动调用schedule()请求进行进程调度。
----------------------------------------------
ARM Linux 进程调度:
switch_mm中是进行页表的切换,即将下一个的pgd的开始物理地址放入CP15中的C2寄存器。进程的pgd的虚拟地址存放在task_struct结构中的pgd指针中,通过__virt_to_phys宏可以转变成成物理地址。
static inline void
switch_mm(struct mm_struct *prev, struct mm_struct *next,
struct task_struct *tsk, unsigned int cpu)
{
if (prev != next)
cpu_switch_mm(next->pgd, tsk);
}
#define cpu_switch_mm(pgd,tsk) cpu_set_pgd(__virt_to_phys((unsigned long)(pgd)
))
#define cpu_get_pgd() \
({ \
unsigned long pg; \
__asm__("mrc p15, 0, %0, c2, c0, 0" \
: "=r" (pg)); \
pg &= ~0x3fff; \
(pgd_t *)phys_to_virt(pg); \
})
switch_to()完成进程上下文的切换,通过调用汇编函数__switch_to完成,其实现比较简单,也就是保存prev进程的上下文信息,该上下文信息由context_save_struct结构描述,包括主要的寄存器,然后将next的上下文信息读出,信息保存在task_struct中的thread.save中TSS_SAVE标识了thread.save在task_struct中的位置。
/*
* Register switch for ARMv3 and ARMv4 processors
* r0 = previous, r1 = next, return previous.
* previous and next are guaranteed not to be the same.
*/
ENTRY(__switch_to)
stmfd sp!, {r4 - sl, fp, lr} Store most regs on
stack
mrs ip, cpsr
str ip, [sp, #-4]! Save cpsr_SVC
str sp, [r0, #TSS_SAVE] Save sp_SVC
ldr sp, [r1, #TSS_SAVE] Get saved sp_SVC
ldr r2, [r1, #TSS_DOMAIN]
*
* Returns amount of memory which needs to be reserved.
*/
long ed_init(long mem_start, int mem_end)
{
int i,
ep;
short tshort,
version,
length,
s_ofs;
if (register_blkdev(EPROM_MAJOR,"ed",&ed_fops)) {
printk("EPROMDISK: Unable to get major %d.\n", EPROM_MAJOR);
return 0;
}
blk_dev[EPROM_MAJOR].request_fn = DEVICE_REQUEST;
for(i=0;i< 4) {
printk("EPROMDISK: Length (%d) Too short.\n", length);
return 0;
}
ed_length = length * 512;
sector_map = ep 6;
sector_offset = ep s_ofs;
printk("EPROMDISK: Version %d installed, %d bytes\n", (int)version, ed_length);
return 0;
}
int get_edisk(unsigned char *buf, int sect, int num_sect)
{
short ss, /* Sector start */
tshort;
int s; /* Sector offset */
for(s=0;s0;) {
sock = bp / EPROM_SIZE;
page = (bp % EPROM_SIZE) / EPAGE_SIZE;
offset = bp % EPAGE_SIZE;
nb = (len offset)>EPAGE_SIZE?EPAGE_SIZE-(offset%EPAGE_SIZE):len;
cr1 = socket[sock] | ((page 《 4) & 0x30) | 0x40; /* no board select for now */
cr2 = (page 》 2) & 0x03;
outb((char)cr1,CONTROL_REG1);
outb((char)cr2,CONTROL_REG2);
memcpy(buf bofs,(char *)(EPROM_WINDOW offset),nb);
len -= nb;
bp = nb;
bofs = nb;
}
return 0;
}
med.c代码如下:
/* med.c - make eprom disk image from ramdisk image */
#include
#include
#include
#define DISK_SIZE (6291456)
#define NUM_SECT (DISK_SIZE/512)
void write_eprom_image(FILE *fi, FILE *fo);
int main(int ac, char **av)
{
FILE *fi,
*fo;
char fin[44],
fon[44];
if (ac > 1) {
strcpy(fin,av[1]);
} else {
strcpy(fin,"hda3.ram");
}
if (ac > 2) {
strcpy(fon,av[2]);
} else {
strcpy(fon,"hda3.eprom");
}
fi = fopen(fin,"r");
fo = fopen(fon,"w");
if (fi == 0 || fo == 0) {
printf("Can't open files\n");
exit(0);
}
write_eprom_image(fi,fo);
fclose(fi);
fclose(fo);
}
void write_eprom_image(FILE *fi, FILE *fo)
{
char *ini;
char *outi; /* In and out images */
short *smap; /* Sector map */
char *sp;
char c = 0;
struct {
unsigned short version;
unsigned short blocks;
unsigned short sect_ofs;
} hdr;
int ns,
s,
i,
fs;
ini = (char *)malloc(DISK_SIZE); /* Max disk size is currently 6M */
outi = (char *)malloc(DISK_SIZE); /* Max disk size is currently 6M */
smap = (short *)malloc(NUM_SECT*sizeof(short));
if (ini == NULL || outi == NULL || smap == NULL) {
printf("Can't allocate memory :(\n");
exit(0);
}
if (DISK_SIZE != fread(ini,1,DISK_SIZE,fi)) {
printf("Can't read input file :(\n");
exit(0);
}
memcpy(outi,ini,512); /* Copy in first sector */
smap[0] = 0;
ns = 1; /* Number of sectors in outi */
免责声明: 凡注明来源本网的所有作品,均为本网合法拥有版权或有权使用的作品,欢迎转载,注明出处。非本网作品均来自互联网,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。