詳解ARM Linux 2.4.x進(jìn)程調(diào)度
出處:互聯(lián)網(wǎng) 發(fā)布于:2011-09-04 12:44:24
Linux2.4.x是一個(gè)基于非搶占式的多任務(wù)的分時(shí)操作系統(tǒng),雖然在用戶進(jìn)程的調(diào)度上采用搶占式策略,但是而在內(nèi)核還是采用了輪轉(zhuǎn)的方法,如果有個(gè)內(nèi)核態(tài)的線程惡性占有CPU不釋放,那系統(tǒng)無法從中解脫出來,所以實(shí)時(shí)性并不是很強(qiáng)。這種情況有望在Linux 2.6版本中得到改善,在2.6版本中采用了搶占式的調(diào)度策略。
內(nèi)核中根據(jù)任務(wù)的實(shí)時(shí)程度提供了三種調(diào)度策略:
?、?SCHED_OTHER為非實(shí)時(shí)任務(wù),采用常規(guī)的分時(shí)調(diào)度策略;
?、?SCHED_FIFO為短小的實(shí)時(shí)任務(wù),采用先進(jìn)先出式調(diào)度,除非有更高優(yōu)先級(jí)進(jìn)程申請(qǐng)運(yùn)行,否則該進(jìn)程將保持運(yùn)行至退出才讓出CPU;
?、?SCHED_RR任務(wù)較長的實(shí)時(shí)任務(wù),由于任務(wù)較長,不能采用FIFO的策略,而是采用輪轉(zhuǎn)式調(diào)度,該進(jìn)程被調(diào)度下來后將被置于運(yùn)行隊(duì)列的末尾,以保證其他實(shí)時(shí)進(jìn)程有機(jī)會(huì)運(yùn)行。
在上述三種調(diào)度策略的基礎(chǔ)上,進(jìn)程依照優(yōu)先級(jí)的高低被分別調(diào)系統(tǒng)。優(yōu)先級(jí)是一些簡單的整數(shù),它代表了為決定應(yīng)該允許哪一個(gè)進(jìn)程使用CPU的資源時(shí)判斷方便而賦予進(jìn)程的權(quán)值——優(yōu)先級(jí)越高,它得到CPU時(shí)間的機(jī)會(huì)也就越大。
在Linux中,非實(shí)時(shí)進(jìn)程有兩種優(yōu)先級(jí),一種是靜態(tài)優(yōu)先級(jí),另一種是動(dòng)態(tài)優(yōu)先級(jí)。實(shí)時(shí)進(jìn)程又增加了第三種優(yōu)先級(jí),實(shí)時(shí)優(yōu)先級(jí)。
?、?靜態(tài)優(yōu)先級(jí)(priority)——被稱為“靜態(tài)”是因?yàn)樗浑S時(shí)間而改變,只能由用戶進(jìn)行修改。它指明了在被迫和其它進(jìn)程競爭CPU之前該進(jìn)程所應(yīng)該被允許的時(shí)間片的值(20)。
?、?動(dòng)態(tài)優(yōu)先級(jí)(counter)——counter 即系統(tǒng)為每個(gè)進(jìn)程運(yùn)行而分配的時(shí)間片,Linux兼用它來表示進(jìn)程的動(dòng)態(tài)優(yōu)先級(jí)。只要進(jìn)程擁有CPU,它就隨著時(shí)間不斷減??;當(dāng)它為0時(shí),標(biāo)記進(jìn)程重新調(diào)度。它指明了在當(dāng)前時(shí)間片中所剩余的時(shí)間量(初為20)。
③ 實(shí)時(shí)優(yōu)先級(jí)(rt_priority)——值為1000。Linux把實(shí)時(shí)優(yōu)先級(jí)與counter值相加作為實(shí)時(shí)進(jìn)程的優(yōu)先權(quán)值。較高權(quán)值的進(jìn)程總是優(yōu)先于較低權(quán)值的進(jìn)程,如果一個(gè)進(jìn)程不是實(shí)時(shí)進(jìn)程,其優(yōu)先權(quán)就遠(yuǎn)小于1000,所以實(shí)時(shí)進(jìn)程總是優(yōu)先。
在每個(gè)tick到來的時(shí)候(也就是時(shí)鐘中斷發(fā)生),系統(tǒng)減小當(dāng)前占有CPU的進(jìn)程的counter,如果counter減小到0,則將need_resched置1,中斷返回過程中進(jìn)行調(diào)度。update_process_times()為時(shí)鐘中斷處理程序調(diào)用的一個(gè)子函數(shù):
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中進(jìn)程的調(diào)度使在schedule()函數(shù)中實(shí)現(xiàn)的,該函數(shù)在下面的ARM匯編片斷中被調(diào)用到:
/*
* 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
而這段代碼在中斷返回或者系統(tǒng)調(diào)用返回中反復(fù)被調(diào)用到:
進(jìn)程狀態(tài)轉(zhuǎn)換時(shí): 如進(jìn)程終止,睡眠等,當(dāng)進(jìn)程要調(diào)用sleep()或exit()等函數(shù)使進(jìn)程狀態(tài)發(fā)生改變時(shí),這些函數(shù)會(huì)主動(dòng)調(diào)用schedule()轉(zhuǎn)入進(jìn)程調(diào)度。
可運(yùn)行隊(duì)列中增加新的進(jìn)程時(shí)。
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
?、?在時(shí)鐘中斷到來后:Linux初始化時(shí),設(shè)定系統(tǒng)定時(shí)器的周期為10毫秒。當(dāng)時(shí)鐘中斷發(fā)生時(shí),時(shí)鐘中斷服務(wù)程序timer_interrupt立即調(diào)用時(shí)鐘處理函數(shù)do_timer( ),在do_timer()會(huì)將當(dāng)前進(jìn)程的counter減1,如果counter為0則置need_resched標(biāo)志,在從時(shí)鐘中斷返回的過程中會(huì)調(diào)用schedule.
?、苓M(jìn)程從系統(tǒng)調(diào)用返回到用戶態(tài)時(shí);判斷need_resched標(biāo)志是否置位,若是則轉(zhuǎn)入執(zhí)行schedule()。系統(tǒng)調(diào)用實(shí)際上就是通過軟中斷實(shí)現(xiàn)的,下面是ARM平臺(tái)下軟中斷處理代碼:
.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
裝載返回地址,用于在跳轉(zhuǎn)調(diào)用后返回到上面的代碼片斷中的
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
⑤ 內(nèi)核處理完中斷后,進(jìn)程返回到用戶態(tài)。
?、?進(jìn)程主動(dòng)調(diào)用schedule()請(qǐng)求進(jìn)行進(jìn)程調(diào)度。
----------------------------------------------
ARM Linux 進(jìn)程調(diào)度:
switch_mm中是進(jìn)行頁表的切換,即將下一個(gè)的pgd的開始物理地址放入CP15中的C2寄存器。進(jìn)程的pgd的虛擬地址存放在task_struct結(jié)構(gòu)中的pgd指針中,通過__virt_to_phys宏可以轉(zhuǎn)變成成物理地址。
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)
?。?BR> #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()完成進(jìn)程上下文的切換,通過調(diào)用匯編函數(shù)__switch_to完成,其實(shí)現(xiàn)比較簡單,也就是保存prev進(jìn)程的上下文信息,該上下文信息由context_save_struct結(jié)構(gòu)描述,包括主要的寄存器,然后將next的上下文信息讀出,信息保存在task_struct中的thread.save中TSS_SAVE標(biāo)識(shí)了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 */
版權(quán)與免責(zé)聲明
凡本網(wǎng)注明“出處:維庫電子市場網(wǎng)”的所有作品,版權(quán)均屬于維庫電子市場網(wǎng),轉(zhuǎn)載請(qǐng)必須注明維庫電子市場網(wǎng),http://m.58mhw.cn,違反者本網(wǎng)將追究相關(guān)法律責(zé)任。
本網(wǎng)轉(zhuǎn)載并注明自其它出處的作品,目的在于傳遞更多信息,并不代表本網(wǎng)贊同其觀點(diǎn)或證實(shí)其內(nèi)容的真實(shí)性,不承擔(dān)此類作品侵權(quán)行為的直接責(zé)任及連帶責(zé)任。其他媒體、網(wǎng)站或個(gè)人從本網(wǎng)轉(zhuǎn)載時(shí),必須保留本網(wǎng)注明的作品出處,并自負(fù)版權(quán)等法律責(zé)任。
如涉及作品內(nèi)容、版權(quán)等問題,請(qǐng)?jiān)谧髌钒l(fā)表之日起一周內(nèi)與本網(wǎng)聯(lián)系,否則視為放棄相關(guān)權(quán)利。
- ARM技術(shù)架構(gòu)與應(yīng)用開發(fā)實(shí)踐指南2026/1/6 10:40:19
- 嵌入式實(shí)時(shí)操作系統(tǒng)(RTOS)選型與移植技術(shù)指南2025/12/31 10:42:31
- 工業(yè)嵌入式系統(tǒng):通信接口技術(shù)選型與抗干擾設(shè)計(jì)實(shí)踐2025/12/15 14:36:53
- 深入解析嵌入式 OPENAMP 框架:開啟異核通信新時(shí)代2025/7/22 16:27:29
- 一文快速了解OPENWRT基礎(chǔ)知識(shí)2025/7/14 16:59:04









