tailieunhanh - Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems

Event-driven programming is a popular model for writing programs for tiny embedded systems and sensor network nodes. While event-driven programming can keep the memory overhead down, it enforces a state machine programming style which makes many programs dif cult to write, maintain, and debug. We present a novel programming abstraction called protothreads that makes it possible to write eventdriven programs in a thread-like style, with a memory overhead of only two bytes per protothread. We show that protothreads signi cantly reduce the complexity of a number of widely used programs previously written with event-driven state machines. For the examined programs the majority of the state machines could be entirely removed. In the other cases the. | Protothreads Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems Adam Dunkels Oliver Schmidt Thiemo Voigt Muneeb All í Swedish Institute of Computer Science Box 1263 SE-16429 Kista Sweden TU Delft Mekelweg 4 2628 CD Delft The Netherlands adam@ oliver@ thiemo@ Abstract Event-driven programming is a popular model for writing programs for tiny embedded systems and sensor network nodes. While event-driven programming can keep the memory overhead down it enforces a state machine programming style which makes many programs difficult to write maintain and debug. We present a novel programming abstraction called protothreads that makes it possible to write event-driven programs in a thread-like style with a memory overhead of only two bytes per protothread. We show that protothreads significantly reduce the complexity of a number of widely used programs previously written with event-driven state machines. For the examined programs the majority of the state machines could be entirely removed. In the other cases the number of states and transitions was drastically decreased. With protothreads the number of lines of code was reduced by one third. The execution time overhead of protothreads is on the order of a few processor cycles. Categories and Subject Descriptors Programming Techniques Concurrent Programming General Terms Design Experimentation Measurement Performance Keywords Wireless sensor networks Embedded systems Threads 1 Introduction Event-driven programming is a common programming model for memory-constrained embedded systems including sensor networks. Compared to multi-threaded systems event-driven systems do not need to allocate memory for perthread stacks which leads to lower memory requirements. For this reason many operating systems for sensor networks Work done at the Swedish Institute of Computer Science. Permission to make digital or hard copies of all or part of this work for .