嵌入式操作系统内核原理和开发(任务创建和堆栈溢出检查)
上一篇 / 下一篇 2012-06-11 10:15:05 / 个人分类:Linux
#s*j4JZ/C0 其实,通过上面一篇博客,我们就已经清楚任务的创建是怎么一回事,但是我们还是愿意就这个问题讲得更细一点,说得更多一点。系统本身是多线程的,那说明 所有线程的地址空间都是共享的。由于资源都是操作系统本身提供的,所以线程本身的要求就很低,函数名、堆栈、入口点、堆栈大小、优先级,大体上也就是这么 多。至于这个堆栈是哪里的内存,其实已经不太重要了。为了简单起见,我们对原来的初始化函数 稍微修改了一下,
3L"GH(hs7QZ)n,_p051Testing软件测试网7k nA n0Rq3`/Ka~,^ void task_init()51Testing软件测试网X)yl,ib:K_)]+yj @&oqb.RASR0 memset((void*)data, 0, STACK_LENGTH * sizeof(UINT32));51Testing软件测试网6?2^Bl)@ |
上面的操作比较简陋,只是对堆栈进行了设置。这是线程初始化的时候必须要做的一步。当然,这里的hello就是我们的函数入口点。因为这里用SIGALRM代替的时钟中断是没有办法做到抢占的,所以我们可以人为多设置一些调度点,比如象这样,
#ikb]!c w*]0void hello()51Testing软件测试网1n7\3a3h6`W { "@c|/FoA!k0 printf("count = %d in sub!\n", count ++); .E nB*s+k#g+wa P&x9G0 swap(&new, &old);51Testing软件测试网[ h2RP8e } printf("count = %d in sub!\n", count ++);51Testing软件测试网G3i0F We6S \ swap(&new, &old); Z_#A t4B+pb3r v+VP0 printf("count = %d in sub!\n", count ++);51Testing软件测试网*ZUtGF(j%_+c swap(&new, &old);51Testing软件测试网/G+~EqR c printf("count = %d in sub!\n", count ++); 0DRZ'b YXD0 swap(&new, &old);51Testing软件测试网(u4pjK6t-O printf("count = %d in sub!\n", count ++); %Uj(z?~c3Y0 quit = 1; 5acK'X3j2K%x3I0 swap(&new, &old); KMnQB#Q!e0} |
%N y)~dl.p/R0 在编写程序的时候,最恐怖的事情就是堆栈溢出了。但是在操作系统中,我们完全可以自己判断当前的堆栈是否已经溢出。因为我们知道,在线程调度的时候,保存的堆栈esp永远指向最低的那个地址。51Testing软件测试网aaI e'lk7[-@}
YTJh#b5OF
J
N0int check_stack_overflow(unsigned int base, unsigned int current)51Testing软件测试网]"xbe1xp:?ETp return (current < base) ? 1 :0; |
9Uq:n1q&y AR[0 当然,这些说的都是线程调度的事,你也可以编写输入输出命令,实现对嵌入式操作系统的某种控制。要打印什么,设置什么,保存什么,都可以通过你的输入命令来解析执行,这些都是和signal处理是分开来的。后面这部分还要详细讨论,这里可以稍微添加一下,
6[,|b"fB#]3r01TB8G#cJM:e#JRm6U4w0int main()51Testing软件测试网f7[I:[.l!I9h`
{
\9{l@EV/N u
UK0 char val;
task_init();
%A+d+gt,lcP0 set_timer();51Testing软件测试网3v9\
PQ5~x$|
signal(SIGALRM, signal_handler);51Testing软件测试网B;L)c N
D9J
7cG
t3nfx0 while(1)
,?%FV$~Q9JT*Q0 {51Testing软件测试网/i#Vz1q4~-u5BT*S
scanf("%c", &val);51Testing软件测试网rCC Fp*C
}
G
Y6u/_6js+Iv@`0 exit(0);
7t3}*Os+N,G0s0 return 1;
+Z)vuK0L E9u?'^&d+{m&z0}
*?#L)z!nq&mZ0最后,还是老规矩,附上详细的代码。虽然这一过程有点繁琐和冗余,但是至少看上去更完整一些。51Testing软件测试网Y~}V~*]$`
]K{@*e c+p{.KK0
H\.}q H051Testing软件测试网'[m~ A8m;IM #include <stdio.h> /]BdMZ0#define UINT32 unsigned int51Testing软件测试网JT:\lJ~ fb static struct itimerval oldtv; void set_timer()51Testing软件测试网N5ZZ0r
PH void swap(UINT32* prev, UINT32* next)51Testing软件测试网`:me@;P,g#h$o V "lea 0xc(%%ebp), %%eax\n\t" /u-H%}:[ znL\+^:UG0void hello() void task_init()51Testing软件测试网F1x!p_V S/T
o memset((void*)data, 0, STACK_LENGTH * sizeof(UINT32));51Testing软件测试网;[ZBeL[ SB,Yp^
?0int check_stack_overflow(unsigned int base, unsigned int current) a5W#MBa0 return (current < base) ? 1 :0;51Testing软件测试网 y(Oc]-TW)PBYr7?)| void signal_handler(int m) DX.N*SH2Zw4T0 printf("count = %d in main!\n", count ++);51Testing软件测试网
qP
s3M&_~6S int main()51Testing软件测试网W'q&p\s ch
@WG
e "V*|{-c5k8Jn.i0 task_init(); while(1)51Testing软件测试网%s6F-@ JGh @&Z/k exit(0);51Testing软件测试网_;@ G-n*{"Um Nt |
相关链接:
X4W0F+b{0K-xq|Y oU`b0bF{*y8ND*tx0f;x1lY@02A/S;m |_0G0,{:V ah,X| lk(a0嵌入式操作系统内核原理和开发(线程切换)51Testing软件测试网~/lJ+w H AN
TAG:
不要让那些真正对你好的人,慢慢的从你的生活中消失,无论爱情还是友情,都需要用心经营。
我的栏目
标题搜索
日历
|
|||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
1 | 2 | 3 | 4 | 5 | 6 | ||||
7 | 8 | 9 | 10 | 11 | 12 | 13 | |||
14 | 15 | 16 | 17 | 18 | 19 | 20 | |||
21 | 22 | 23 | 24 | 25 | 26 | 27 | |||
28 | 29 | 30 |
我的存档
数据统计
- 访问量: 3339989
- 日志数: 1640
- 建立时间: 2011-12-07
- 更新时间: 2019-12-24