免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1951 | 回复: 0
打印 上一主题 下一主题

Contiki学习笔记:事件驱动机制和protothread机制 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-23 03:25 |只看该作者 |倒序浏览

摘要:

    本文以通俗的语言阐述了Contiki的两个主要机制:事件驱动和protothread机制。


Contiki两个主要机制:事件驱动和protothread机制,前者是为了降低功耗,后者是为了节省内存。

一、事件驱动

    嵌入式系统常常被设计成响应周围环境的变化,而这些变化可以看成一个个事件。事件来了,操作系统处理之,没有事件到来,就跑去休眠了(降低功耗),这就是所谓的事件驱动,类似于中断。

二、protothread机制

2.1 概述

    传统的操作系统使用栈保存进程上下文,每个进程需要一个栈,这对于内存极度受限的传感器设备将难以忍受。protothread机制恰解决了这个问题,通过保存进程被阻塞处的行数(进程结构体的一个变量,unsiged short类型,只需两个字节),从而实现进程切换,当该进程下一次被调度时,通过switch(__LINE__)跳转到刚才保存的点,恢复执行。整个Contiki只用一个栈,当进程切换时清空,大大节省内存。

2.2 特点

    protothread(- Lightweight, Stackless Threads in C)最大特点就是轻量级,每个protothread不需要自己的堆栈,所有的protothread使用同一个堆栈,而保存程序断点用两个字节保存被中断的行数即可。具体特点如下[1]:

Very small RAM overhead - only two bytes per protothread and no extra stacks 

Highly portable - the protothreads library is 100% pure C and no architecture specific assembly code 

Can be used with or without an OS 

Provides blocking wait without full multi-threading or stack-switching 

Freely available under a BSD-like open source license


    protothread机制很早就有了,Contiki OS只要运用这种机制,protothread机制还可以用于以下情形[1]:

Memory constrained systems 

Event-driven protocol stacks 

Small embedded systems 

Sensor network nodes 

Portable C applications

2.3 编程提示

    谨慎使用局部变量。当进程切换时,因protothread没有保存堆栈上下文(只使用两个字节保存被中断的行号),故局部变量得不到保存。这就要求使用局部变量要特别小心,一个好的解决方法,使用局部静态变量(加static修饰符),如此,该变量在整个生命周期都存在。

2.4 调度[2]

    直接看原文吧。A protothread is driven by repeated calls to the function in which the protothread is running. Each time the function is called, the protothread will run until it blocks or exits. Thus the scheduling of protothreads is done by the application that uses protothreads.


    如果还想进一步了解protothread,可以到[3]下载源码,仔细研读。



参考资料:

[1]Protothreads - Lightweight, Stackless Threads in C : http://www.sics.se/~adam/pt/

[2]About protothreads : http://www.sics.se/~adam/pt/about.html

[3]SourceForge--protothread : http://sourceforge.net/projects/protothread/

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP