首页 > 编程知识 正文

crontab定时任务配置每分钟执行,freertos时间片

时间:2023-05-04 08:55:39 阅读:33736 作者:2276

简单的时间片轮换Linux内核分析

Linux内核操作系统

by干宇宙飞行

原创作品转载请注明出处《Linux内核分析》 MOOC课程

实验环境: window S10专业虚拟盒5.14下Ubuntu 15.10 64第64位

采用快乐煎鸡蛋老师提供的mykernel环境

环境截图

mypcb.h代码

/* my kernel-- simplesimulationofthelinuxosprocessschedule

*

* linux/mykernel/mypcb.h

*

* kernelinternalmy _ timer _ handler

*

*版权(c ) 2013孟宁

*

*修改2014云全张

*

*

* youcanredistributeormodifythisprogramundertheterms

* ofthegnugeneralpubliclicenseaspublishedby

* the自由软件基础设施。

*

* youshouldhavereceivedacopyofthegnugeneralpubliclicense

* along with this program. If not,see。

*/

# definemax _ task _ num 10//maxnumoftaskinsystem

#defineKERNEL_STACK_SIZE1024*8

# define priority _ max 30//priorityrangefrom0to 30

/* CPU-specificstateofthistask * /

结构趋势{

无符号长整型; //point to cpu run address

无符号长sp; //pointtothethreadstack ' stop address

//todoaddotherattrubteofsystemthread

(;

//PCB结构

typedefstructPCB{

intpid; //pcb id

volatilelongstate; /* -1 unrunnable,0 runnable,0 stopped */

charstack[KERNEL_STACK_SIZE]; //each PCB堆叠大小is 1024 * 8

/* CPU-specificstateofthistask * /

structThreadthread;

统一长任务_入口; //thetaskexecuteentrymemoryaddress

结构PCB *下一步; //pcb is a circular linked list

无符号长优先级;//任务优先级

//todoaddotherattrubteofprocesscontrolblock

}tPCB;

//voidmy_schedule(intPID );

voidmy_schedule(void );

先看看mypcb.h里有什么

在thread中,ip、sp分别为eip和esp

PCBpid进程id

state进程状态-1就绪0就绪

stack定义了内核堆栈

真红

task_entry

自己类型的指针

定义了调度程序

my_schedule

mymain.c代码

/*

* linux/mykernel/mymain.c

*

* Kernel internal my_start_kernel

*

*版权(c ) 2013孟宁

*

*/

#包含

#包含

#包含

#包含

#包含

#include'mypcb.h '

tPCB task[MAX_TASK_NUM];

tPCB*my_current_task=NULL;

volatileintmy_need_sched=0;//是否需要调度

voidmy_process(void);

void__init my_start_kernel(void)

{

intpid=0;

inti;

/* 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=(unsignedlong)my_process;

task[pid].thread.sp=(unsignedlong)&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].state=-1;

task[i].thread.sp=(unsignedlong)&task[i].stack[KERNEL_STACK_SIZE-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];

asmvolatile(

"movl %1,%%espnt"/* set task[pid].thread.sp to esp */

"pushl %1nt"/* push ebp */

"pushl %0nt"/* push task[pid].thread.ip */

"retnt"/* pop task[pid].thread.ip to eip */

"popl %%ebpnt"

:

:"c"(task[pid].thread.ip),"d"(task[pid].thread.sp)/* input c or d mean %ecx/%edx*/

);

}

voidmy_process(void)

{

inti=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);

}

}

}

先把mypcb.h包含进来

先声明一个task的数组

当前的task的指针

声明一个标志 my_need_sched

my_start_kernel

零号进程初始化

状态就绪

起点、入口是my_process

即函数执行的是my_process

堆栈的栈顶,定义了一个stack

指针指向它自己,因为系统只有它自己。

将零号进程的状态,复制过来

每个进程有它自己的堆栈

新fork的进程添加到这个task链表的尾部

开始执行零号进程

有一段汇编代码,比较关键

asmvolatile(

"movl %1,%%espnt"/* set task[pid].thread.sp to esp */

"pushl %1nt"/* push ebp */

"pushl %0nt"/* push task[pid].thread.ip */

"retnt"/* pop task[pid].thread.ip to eip */

"popl %%ebpnt"

:

:"c"(task[pid].thread.ip),"d"(task[pid].thread.sp)/* input c or d mean %ecx/%edx*/

);

ret之后0号进程启动起来

下面我们来看一下my_process的代码

执行一千万次,之后主动调度

myinterrupt.c

/*

* linux/mykernel/myinterrupt.c

*

* Kernel internal my_timer_handler

*

* Copyright (C) 2013 Mengning

*

*/

#include

#include

#include

#include

#include

#include"mypcb.h"

externtPCB task[MAX_TASK_NUM];

externtPCB*my_current_task;

externvolatileintmy_need_sched;

volatileinttime_count=0;

/*

* Called by timer interrupt.

* it runs in the name of current running process,

* so it use kernel stack of current running process

*/

voidmy_timer_handler(void)

{

#if 1

if(time_count%100==0&&my_need_sched!=1)

{

printk(KERN_NOTICE">>>my_timer_handler here<<

my_need_sched=1;

}

time_count++;

#endif

return;

}

voidmy_schedule(void)

{

tPCB*next;

tPCB*prev;

if(my_current_task==NULL

||my_current_task->next==NULL)

{

return;

}

printk(KERN_NOTICE">>>my_schedule<<

/* schedule */

next=my_current_task->next;

prev=my_current_task;

if(next->state==0)/* -1 unrunnable, 0 runnable, >0 stopped */

{

/* switch to next process */

asmvolatile(

"pushl %%ebpnt"/* save ebp */

"movl %%esp,%0nt"/* save esp */

"movl %2,%%espnt"/* restore esp */

"movl $1f,%1nt"/* save eip */

"pushl %3nt"

"retnt"/* restore eip */

"1:t"/* next process start here */

"popl %%ebpnt"

:"=m"(prev->thread.sp),"=m"(prev->thread.ip)

:"m"(next->thread.sp),"m"(next->thread.ip)

);

my_current_task=next;

printk(KERN_NOTICE">>>switch %d to %d<<pid,next->pid);

}

else

{

next->state=0;

my_current_task=next;

printk(KERN_NOTICE">>>switch %d to %d<<pid,next->pid);

/* switch to new process */

asmvolatile(

"pushl %%ebpnt"/* save ebp */

"movl %%esp,%0nt"/* save esp */

"movl %2,%%espnt"/* restore esp */

"movl %2,%%ebpnt"/* restore ebp */

"movl $1f,%1nt"/* save eip */

"pushl %3nt"

"retnt"/* restore eip */

:"=m"(prev->thread.sp),"=m"(prev->thread.ip)

:"m"(next->thread.sp),"m"(next->thread.ip)

);

}

return;

}

再来看myinterrupt的代码

全局变量声明三个变量

定义时间片 time_count

my_timer_handler中

当调度过一次之后,把my_need_sched赋为1

然后来看my_schedule的函数

前面链表相关的操作和检查

下面if 检查是否就绪

然后进行上下文切换。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。