SlideShare a Scribd company logo
1
Protothreads –
Simplifying Programming of Memory-Constrain
ed Embedded Systems
By
Salah
Adopted from
Adam Dunkels et. al.,
http://dunkels.com/adam/pt/expansion.html
2
Protothreads
uProtothreads are a extremely lightweight, stackless type of threads that
provides a blocking context on top of an event-driven system, without the
overhead of per-thread stacks.
uThe purpose of Protothreads is to implement sequential flow of control
without complex state machines or full multi-threading.
u Protothreads provides conditional blocking inside C functions..
3
Why not just use multithreading?
Multithreading the basis of (almost) all embedded OS/RTOSes!
◦ WSN community: Mantis, BTNut (based on multithreading); Contiki (multithr
eading on a per-application basis)
Nothing wrong with multithreading
◦ Multiple stacks require more memory
◦ Networked = more concurrency than traditional embedded
◦ Can lead to more expensive hardware
◦ Preemption
◦ Threads: explicit locking; Protothreads: implicit locking
Protothreads are a new point in the design space
◦ Between event-driven and multithreaded
4
Threads require per-thread stack memory
Four threads, each with its own stack
Thread 1 Thread 2 Thread 3 Thread 4
5
Events require one stack
Four threads, each with its own stack
Thread 1 Thread 2 Thread 3 Thread 4
Eventhandler 1Eventhandler 2Eventhandler 3
Stack is reused for
every event handler
Threads require per-thread stack memory
● Four event handlers, one
stack
Eventhandler 4
6
Protothreads require one stack
Four threads, each with its own stack
Thread 1 Thread 2 Thread 3 Thread 4
Threads require per-thread stack memory
● Four protothreads, one
stack
Events require one stack
● Four event handlers, one
stack
Protothread 1Protothread 2Protothread 3Protothread 4
Just like events
7
Main features
u No machine specific code since the protothreads library is pure C
u Does not use error-prone functions such as longjmp()
u Very small RAM overhead - only two bytes per protothread
u Can be used with or without an OS
u Provides blocking wait without full multi-threading or stack-
switching
8
Examples applications:
- Memory constrained systems
- Event-driven protocol stacks
- Deeply embedded systems
- Sensor network nodes
9
10
Pt file structure
Data Structures
struct Pt
Initialization
#define PT_INIT(pt)
Initialize a protothread.
Declaration and definition
#define PT_THREAD(name_args)
Declaration of a protothread.
#define PT_BEGIN(pt)
Declare the start of a protothread inside the C functi
on implementing the protothread.
#define PT_END(pt)
Declare the end of a protothread.
11
Pt file structure
Blocked wait
#define PT_WAIT_UNTIL(pt, condition)
Block and wait until condition is true.
#define PT_WAIT_WHILE(pt, cond)
Block and wait while condition is true.
Hierarchical protothreads
#define PT_WAIT_THREAD(pt, thread)
Block and wait until a child protothread completes.
#define PT_SPAWN(pt, child, thread)
Spawn a child protothread and wait until it exits.
12
Pt file structure
Exiting and restarting
#define PT_RESTART(pt)
Restart the protothread.
#define PT_EXIT(pt)
Exit the protothread.
Calling a protothread
#define PT_SCHEDULE(f)
Schedule a protothread.
13
Pt file structure
Yielding from a protothread
#define PT_YIELD(pt)
Yield from the current protothread.
#define PT_YIELD_UNTIL(pt, cond)
Yield from the protothread until a condition occurs.
Defines
#define PT_WAITING 0
#define PT_YIELDED 1
#define PT_EXITED 2
#define PT_ENDED 3
14
Protothread scheduling
A protothread runs in a C function
We schedule a protothread by invoking its function
We can invoke the protothread from an event handler
◦ Protothreads as blocking event handlers
We can let the operating system invoke our protothreads
◦ Contiki
Protothreads can invoke other protothreads
◦ Can wait until a child protothread completes
◦ Hierarchical protothreads
15
How do we implement protothreads?
16
Simple example program using
protothread
--waits for a counter to reach a certain threshold,
-prints out a message, and resets the counter.
- This is done in a while() loop that runs forever.
- The counter is increased in the main() function.
17
Example’s coding
18
C preprocessor expand the above code,
19
C-switch expansion
20
Blocked wait macro
21
C-switch expansion-another example
int a_protothread(struct pt *pt) {
PT_BEGIN(pt);
PT_WAIT_UNTIL(pt, condition1);
if(something) {
PT_WAIT_UNTIL(pt, condition2);
}
PT_END(pt);
}
int a_protothread(struct pt *pt) {
switch(pt->lc) { case 0:
pt->lc = 5; case 5:
if(!condition1) return 0;
if(something) {
pt->lc = 10; case 10:
if(!condition2) return 0;
}
} return 1;
}
Line numbers

More Related Content

What's hot

systemd
systemdsystemd
systemd
nussbauml
 
Linux Internals - Part I
Linux Internals - Part ILinux Internals - Part I
file handling c++
file handling c++file handling c++
file handling c++Guddu Spy
 
eBPF Trace from Kernel to Userspace
eBPF Trace from Kernel to UserspaceeBPF Trace from Kernel to Userspace
eBPF Trace from Kernel to Userspace
SUSE Labs Taipei
 
Memory model
Memory modelMemory model
Memory model
Yi-Hsiu Hsu
 
Effective testing with pytest
Effective testing with pytestEffective testing with pytest
Effective testing with pytest
Hector Canto
 
Interrupts
InterruptsInterrupts
Interrupts
Anil Kumar Pugalia
 
The TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux KernelThe TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux Kernel
Divye Kapoor
 
Introduction to Rust language programming
Introduction to Rust language programmingIntroduction to Rust language programming
Introduction to Rust language programming
Rodolfo Finochietti
 
I2C Subsystem In Linux-2.6.24
I2C Subsystem In Linux-2.6.24I2C Subsystem In Linux-2.6.24
I2C Subsystem In Linux-2.6.24Varun Mahajan
 
Linux device driver
Linux device driverLinux device driver
Linux device driver
chatsiri
 
semaphore & mutex.pdf
semaphore & mutex.pdfsemaphore & mutex.pdf
semaphore & mutex.pdf
Adrian Huang
 
Dynamic Memory allocation
Dynamic Memory allocationDynamic Memory allocation
Dynamic Memory allocation
Grishma Rajput
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)
shimosawa
 
Slab Allocator in Linux Kernel
Slab Allocator in Linux KernelSlab Allocator in Linux Kernel
Slab Allocator in Linux Kernel
Adrian Huang
 
Fun with Network Interfaces
Fun with Network InterfacesFun with Network Interfaces
Fun with Network Interfaces
Kernel TLV
 
08. handling file streams
08. handling file streams08. handling file streams
08. handling file streams
Haresh Jaiswal
 
Linux Kernel - Virtual File System
Linux Kernel - Virtual File SystemLinux Kernel - Virtual File System
Linux Kernel - Virtual File System
Adrian Huang
 
Manipulators in c++
Manipulators in c++Manipulators in c++
Manipulators in c++
Ashok Raj
 

What's hot (20)

systemd
systemdsystemd
systemd
 
Linux Internals - Part I
Linux Internals - Part ILinux Internals - Part I
Linux Internals - Part I
 
file handling c++
file handling c++file handling c++
file handling c++
 
eBPF Trace from Kernel to Userspace
eBPF Trace from Kernel to UserspaceeBPF Trace from Kernel to Userspace
eBPF Trace from Kernel to Userspace
 
Memory model
Memory modelMemory model
Memory model
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
 
Effective testing with pytest
Effective testing with pytestEffective testing with pytest
Effective testing with pytest
 
Interrupts
InterruptsInterrupts
Interrupts
 
The TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux KernelThe TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux Kernel
 
Introduction to Rust language programming
Introduction to Rust language programmingIntroduction to Rust language programming
Introduction to Rust language programming
 
I2C Subsystem In Linux-2.6.24
I2C Subsystem In Linux-2.6.24I2C Subsystem In Linux-2.6.24
I2C Subsystem In Linux-2.6.24
 
Linux device driver
Linux device driverLinux device driver
Linux device driver
 
semaphore & mutex.pdf
semaphore & mutex.pdfsemaphore & mutex.pdf
semaphore & mutex.pdf
 
Dynamic Memory allocation
Dynamic Memory allocationDynamic Memory allocation
Dynamic Memory allocation
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)
 
Slab Allocator in Linux Kernel
Slab Allocator in Linux KernelSlab Allocator in Linux Kernel
Slab Allocator in Linux Kernel
 
Fun with Network Interfaces
Fun with Network InterfacesFun with Network Interfaces
Fun with Network Interfaces
 
08. handling file streams
08. handling file streams08. handling file streams
08. handling file streams
 
Linux Kernel - Virtual File System
Linux Kernel - Virtual File SystemLinux Kernel - Virtual File System
Linux Kernel - Virtual File System
 
Manipulators in c++
Manipulators in c++Manipulators in c++
Manipulators in c++
 

Similar to protothread and its usage in contiki OS

RTOS implementation
RTOS implementationRTOS implementation
RTOS implementation
Rajan Kumar
 
P-Threads
P-ThreadsP-Threads
P-Threads
Bharathwaj_v
 
Geep networking stack-linuxkernel
Geep networking stack-linuxkernelGeep networking stack-linuxkernel
Geep networking stack-linuxkernel
Kiran Divekar
 
Ppl for students unit 4 and 5
Ppl for students unit 4 and 5Ppl for students unit 4 and 5
Ppl for students unit 4 and 5Akshay Nagpurkar
 
Ppl for students unit 4 and 5
Ppl for students unit 4 and 5Ppl for students unit 4 and 5
Ppl for students unit 4 and 5Akshay Nagpurkar
 
PG Day'14 Russia, PostgreSQL System Architecture, Heikki Linnakangas
PG Day'14 Russia, PostgreSQL System Architecture, Heikki LinnakangasPG Day'14 Russia, PostgreSQL System Architecture, Heikki Linnakangas
PG Day'14 Russia, PostgreSQL System Architecture, Heikki Linnakangas
pgdayrussia
 
Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++Shuo Chen
 
Cassandra in Operation
Cassandra in OperationCassandra in Operation
Cassandra in Operation
niallmilton
 
Securing Applications and Pipelines on a Container Platform
Securing Applications and Pipelines on a Container PlatformSecuring Applications and Pipelines on a Container Platform
Securing Applications and Pipelines on a Container Platform
All Things Open
 
Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01Aravindharamanan S
 
Linux Conference Australia 2018 : Device Tree, past, present, future
Linux Conference Australia 2018 : Device Tree, past, present, futureLinux Conference Australia 2018 : Device Tree, past, present, future
Linux Conference Australia 2018 : Device Tree, past, present, future
Neil Armstrong
 
Operating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - ThreadsOperating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - Threads
Peter Tröger
 
Study notes for CompTIA Certified Advanced Security Practitioner
Study notes for CompTIA Certified Advanced Security PractitionerStudy notes for CompTIA Certified Advanced Security Practitioner
Study notes for CompTIA Certified Advanced Security Practitioner
David Sweigert
 
Analysis Of Internet Protocol ( IP ) Datagrams
Analysis Of Internet Protocol ( IP ) DatagramsAnalysis Of Internet Protocol ( IP ) Datagrams
Analysis Of Internet Protocol ( IP ) Datagrams
Emily Jones
 
Threads and multi threading
Threads and multi threadingThreads and multi threading
Threads and multi threading
Antonio Cesarano
 
Here comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfHere comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdf
Krystian Zybała
 

Similar to protothread and its usage in contiki OS (20)

Pthread
PthreadPthread
Pthread
 
RTOS implementation
RTOS implementationRTOS implementation
RTOS implementation
 
P-Threads
P-ThreadsP-Threads
P-Threads
 
Lec13 multidevice
Lec13 multideviceLec13 multidevice
Lec13 multidevice
 
Geep networking stack-linuxkernel
Geep networking stack-linuxkernelGeep networking stack-linuxkernel
Geep networking stack-linuxkernel
 
Ppl for students unit 4 and 5
Ppl for students unit 4 and 5Ppl for students unit 4 and 5
Ppl for students unit 4 and 5
 
Ppl for students unit 4 and 5
Ppl for students unit 4 and 5Ppl for students unit 4 and 5
Ppl for students unit 4 and 5
 
PG Day'14 Russia, PostgreSQL System Architecture, Heikki Linnakangas
PG Day'14 Russia, PostgreSQL System Architecture, Heikki LinnakangasPG Day'14 Russia, PostgreSQL System Architecture, Heikki Linnakangas
PG Day'14 Russia, PostgreSQL System Architecture, Heikki Linnakangas
 
Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++
 
Cassandra in Operation
Cassandra in OperationCassandra in Operation
Cassandra in Operation
 
Securing Applications and Pipelines on a Container Platform
Securing Applications and Pipelines on a Container PlatformSecuring Applications and Pipelines on a Container Platform
Securing Applications and Pipelines on a Container Platform
 
Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01
 
Linux Conference Australia 2018 : Device Tree, past, present, future
Linux Conference Australia 2018 : Device Tree, past, present, futureLinux Conference Australia 2018 : Device Tree, past, present, future
Linux Conference Australia 2018 : Device Tree, past, present, future
 
Operating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - ThreadsOperating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - Threads
 
Study notes for CompTIA Certified Advanced Security Practitioner
Study notes for CompTIA Certified Advanced Security PractitionerStudy notes for CompTIA Certified Advanced Security Practitioner
Study notes for CompTIA Certified Advanced Security Practitioner
 
Analysis Of Internet Protocol ( IP ) Datagrams
Analysis Of Internet Protocol ( IP ) DatagramsAnalysis Of Internet Protocol ( IP ) Datagrams
Analysis Of Internet Protocol ( IP ) Datagrams
 
Pthreads linux
Pthreads linuxPthreads linux
Pthreads linux
 
Threads and multi threading
Threads and multi threadingThreads and multi threading
Threads and multi threading
 
Here comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfHere comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdf
 
P threads
P threadsP threads
P threads
 

More from Salah Amean

ICT role in Yemen
ICT role in Yemen ICT role in Yemen
ICT role in Yemen
Salah Amean
 
Contiki os timer tutorial
Contiki os timer tutorialContiki os timer tutorial
Contiki os timer tutorial
Salah Amean
 
WSN protocol 802.15.4 together with cc2420 seminars
WSN protocol 802.15.4 together with cc2420 seminars WSN protocol 802.15.4 together with cc2420 seminars
WSN protocol 802.15.4 together with cc2420 seminars
Salah Amean
 
ContikiMAC : Radio Duty Cycling Protocol
ContikiMAC : Radio Duty Cycling ProtocolContikiMAC : Radio Duty Cycling Protocol
ContikiMAC : Radio Duty Cycling Protocol
Salah Amean
 
Location in ubiquitous computing, LOCATION SYSTEMS
Location in ubiquitous computing, LOCATION SYSTEMSLocation in ubiquitous computing, LOCATION SYSTEMS
Location in ubiquitous computing, LOCATION SYSTEMS
Salah Amean
 
Bonjour protocol
Bonjour protocolBonjour protocol
Bonjour protocol
Salah Amean
 
Optimization Analysis
Optimization AnalysisOptimization Analysis
Optimization Analysis
Salah Amean
 
Mobile apps-user interaction measurement & Apps ecosystem
Mobile apps-user interaction measurement & Apps ecosystemMobile apps-user interaction measurement & Apps ecosystem
Mobile apps-user interaction measurement & Apps ecosystem
Salah Amean
 
ict culturing conference presentation _presented 2013_12_07
 ict culturing conference presentation _presented 2013_12_07 ict culturing conference presentation _presented 2013_12_07
ict culturing conference presentation _presented 2013_12_07
Salah Amean
 
Data Mining: Concepts and Techniques_ Chapter 6: Mining Frequent Patterns, ...
Data Mining:  Concepts and Techniques_ Chapter 6: Mining Frequent Patterns, ...Data Mining:  Concepts and Techniques_ Chapter 6: Mining Frequent Patterns, ...
Data Mining: Concepts and Techniques_ Chapter 6: Mining Frequent Patterns, ...
Salah Amean
 
Data mining :Concepts and Techniques Chapter 2, data
Data mining :Concepts and Techniques Chapter 2, dataData mining :Concepts and Techniques Chapter 2, data
Data mining :Concepts and Techniques Chapter 2, data
Salah Amean
 
Data Mining:Concepts and Techniques, Chapter 8. Classification: Basic Concepts
Data Mining:Concepts and Techniques, Chapter 8. Classification: Basic ConceptsData Mining:Concepts and Techniques, Chapter 8. Classification: Basic Concepts
Data Mining:Concepts and Techniques, Chapter 8. Classification: Basic Concepts
Salah Amean
 
Data mining: Concepts and Techniques, Chapter12 outlier Analysis
Data mining: Concepts and Techniques, Chapter12 outlier Analysis Data mining: Concepts and Techniques, Chapter12 outlier Analysis
Data mining: Concepts and Techniques, Chapter12 outlier Analysis
Salah Amean
 
Data Mining: Concepts and techniques classification _chapter 9 :advanced methods
Data Mining: Concepts and techniques classification _chapter 9 :advanced methodsData Mining: Concepts and techniques classification _chapter 9 :advanced methods
Data Mining: Concepts and techniques classification _chapter 9 :advanced methods
Salah Amean
 
Data Mining: Concepts and techniques: Chapter 13 trend
Data Mining: Concepts and techniques: Chapter 13 trendData Mining: Concepts and techniques: Chapter 13 trend
Data Mining: Concepts and techniques: Chapter 13 trend
Salah Amean
 
Data Mining: Concepts and techniques: Chapter 11,Review: Basic Cluster Analys...
Data Mining: Concepts and techniques: Chapter 11,Review: Basic Cluster Analys...Data Mining: Concepts and techniques: Chapter 11,Review: Basic Cluster Analys...
Data Mining: Concepts and techniques: Chapter 11,Review: Basic Cluster Analys...
Salah Amean
 
Data Mining Concepts and Techniques, Chapter 10. Cluster Analysis: Basic Conc...
Data Mining Concepts and Techniques, Chapter 10. Cluster Analysis: Basic Conc...Data Mining Concepts and Techniques, Chapter 10. Cluster Analysis: Basic Conc...
Data Mining Concepts and Techniques, Chapter 10. Cluster Analysis: Basic Conc...
Salah Amean
 
Data Mining: Concepts and Techniques chapter 07 : Advanced Frequent Pattern M...
Data Mining: Concepts and Techniques chapter 07 : Advanced Frequent Pattern M...Data Mining: Concepts and Techniques chapter 07 : Advanced Frequent Pattern M...
Data Mining: Concepts and Techniques chapter 07 : Advanced Frequent Pattern M...
Salah Amean
 
Data Mining: Concepts and Techniques (3rd ed.) — Chapter 5
Data Mining:  Concepts and Techniques (3rd ed.)— Chapter 5 Data Mining:  Concepts and Techniques (3rd ed.)— Chapter 5
Data Mining: Concepts and Techniques (3rd ed.) — Chapter 5
Salah Amean
 
Data Mining: Concepts and Techniques (3rd ed.) — Chapter _04 olap
Data Mining:  Concepts and Techniques (3rd ed.)— Chapter _04 olapData Mining:  Concepts and Techniques (3rd ed.)— Chapter _04 olap
Data Mining: Concepts and Techniques (3rd ed.) — Chapter _04 olap
Salah Amean
 

More from Salah Amean (20)

ICT role in Yemen
ICT role in Yemen ICT role in Yemen
ICT role in Yemen
 
Contiki os timer tutorial
Contiki os timer tutorialContiki os timer tutorial
Contiki os timer tutorial
 
WSN protocol 802.15.4 together with cc2420 seminars
WSN protocol 802.15.4 together with cc2420 seminars WSN protocol 802.15.4 together with cc2420 seminars
WSN protocol 802.15.4 together with cc2420 seminars
 
ContikiMAC : Radio Duty Cycling Protocol
ContikiMAC : Radio Duty Cycling ProtocolContikiMAC : Radio Duty Cycling Protocol
ContikiMAC : Radio Duty Cycling Protocol
 
Location in ubiquitous computing, LOCATION SYSTEMS
Location in ubiquitous computing, LOCATION SYSTEMSLocation in ubiquitous computing, LOCATION SYSTEMS
Location in ubiquitous computing, LOCATION SYSTEMS
 
Bonjour protocol
Bonjour protocolBonjour protocol
Bonjour protocol
 
Optimization Analysis
Optimization AnalysisOptimization Analysis
Optimization Analysis
 
Mobile apps-user interaction measurement & Apps ecosystem
Mobile apps-user interaction measurement & Apps ecosystemMobile apps-user interaction measurement & Apps ecosystem
Mobile apps-user interaction measurement & Apps ecosystem
 
ict culturing conference presentation _presented 2013_12_07
 ict culturing conference presentation _presented 2013_12_07 ict culturing conference presentation _presented 2013_12_07
ict culturing conference presentation _presented 2013_12_07
 
Data Mining: Concepts and Techniques_ Chapter 6: Mining Frequent Patterns, ...
Data Mining:  Concepts and Techniques_ Chapter 6: Mining Frequent Patterns, ...Data Mining:  Concepts and Techniques_ Chapter 6: Mining Frequent Patterns, ...
Data Mining: Concepts and Techniques_ Chapter 6: Mining Frequent Patterns, ...
 
Data mining :Concepts and Techniques Chapter 2, data
Data mining :Concepts and Techniques Chapter 2, dataData mining :Concepts and Techniques Chapter 2, data
Data mining :Concepts and Techniques Chapter 2, data
 
Data Mining:Concepts and Techniques, Chapter 8. Classification: Basic Concepts
Data Mining:Concepts and Techniques, Chapter 8. Classification: Basic ConceptsData Mining:Concepts and Techniques, Chapter 8. Classification: Basic Concepts
Data Mining:Concepts and Techniques, Chapter 8. Classification: Basic Concepts
 
Data mining: Concepts and Techniques, Chapter12 outlier Analysis
Data mining: Concepts and Techniques, Chapter12 outlier Analysis Data mining: Concepts and Techniques, Chapter12 outlier Analysis
Data mining: Concepts and Techniques, Chapter12 outlier Analysis
 
Data Mining: Concepts and techniques classification _chapter 9 :advanced methods
Data Mining: Concepts and techniques classification _chapter 9 :advanced methodsData Mining: Concepts and techniques classification _chapter 9 :advanced methods
Data Mining: Concepts and techniques classification _chapter 9 :advanced methods
 
Data Mining: Concepts and techniques: Chapter 13 trend
Data Mining: Concepts and techniques: Chapter 13 trendData Mining: Concepts and techniques: Chapter 13 trend
Data Mining: Concepts and techniques: Chapter 13 trend
 
Data Mining: Concepts and techniques: Chapter 11,Review: Basic Cluster Analys...
Data Mining: Concepts and techniques: Chapter 11,Review: Basic Cluster Analys...Data Mining: Concepts and techniques: Chapter 11,Review: Basic Cluster Analys...
Data Mining: Concepts and techniques: Chapter 11,Review: Basic Cluster Analys...
 
Data Mining Concepts and Techniques, Chapter 10. Cluster Analysis: Basic Conc...
Data Mining Concepts and Techniques, Chapter 10. Cluster Analysis: Basic Conc...Data Mining Concepts and Techniques, Chapter 10. Cluster Analysis: Basic Conc...
Data Mining Concepts and Techniques, Chapter 10. Cluster Analysis: Basic Conc...
 
Data Mining: Concepts and Techniques chapter 07 : Advanced Frequent Pattern M...
Data Mining: Concepts and Techniques chapter 07 : Advanced Frequent Pattern M...Data Mining: Concepts and Techniques chapter 07 : Advanced Frequent Pattern M...
Data Mining: Concepts and Techniques chapter 07 : Advanced Frequent Pattern M...
 
Data Mining: Concepts and Techniques (3rd ed.) — Chapter 5
Data Mining:  Concepts and Techniques (3rd ed.)— Chapter 5 Data Mining:  Concepts and Techniques (3rd ed.)— Chapter 5
Data Mining: Concepts and Techniques (3rd ed.) — Chapter 5
 
Data Mining: Concepts and Techniques (3rd ed.) — Chapter _04 olap
Data Mining:  Concepts and Techniques (3rd ed.)— Chapter _04 olapData Mining:  Concepts and Techniques (3rd ed.)— Chapter _04 olap
Data Mining: Concepts and Techniques (3rd ed.) — Chapter _04 olap
 

Recently uploaded

The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 

Recently uploaded (20)

The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 

protothread and its usage in contiki OS

  • 1. 1 Protothreads – Simplifying Programming of Memory-Constrain ed Embedded Systems By Salah Adopted from Adam Dunkels et. al., http://dunkels.com/adam/pt/expansion.html
  • 2. 2 Protothreads uProtothreads are a extremely lightweight, stackless type of threads that provides a blocking context on top of an event-driven system, without the overhead of per-thread stacks. uThe purpose of Protothreads is to implement sequential flow of control without complex state machines or full multi-threading. u Protothreads provides conditional blocking inside C functions..
  • 3. 3 Why not just use multithreading? Multithreading the basis of (almost) all embedded OS/RTOSes! ◦ WSN community: Mantis, BTNut (based on multithreading); Contiki (multithr eading on a per-application basis) Nothing wrong with multithreading ◦ Multiple stacks require more memory ◦ Networked = more concurrency than traditional embedded ◦ Can lead to more expensive hardware ◦ Preemption ◦ Threads: explicit locking; Protothreads: implicit locking Protothreads are a new point in the design space ◦ Between event-driven and multithreaded
  • 4. 4 Threads require per-thread stack memory Four threads, each with its own stack Thread 1 Thread 2 Thread 3 Thread 4
  • 5. 5 Events require one stack Four threads, each with its own stack Thread 1 Thread 2 Thread 3 Thread 4 Eventhandler 1Eventhandler 2Eventhandler 3 Stack is reused for every event handler Threads require per-thread stack memory ● Four event handlers, one stack Eventhandler 4
  • 6. 6 Protothreads require one stack Four threads, each with its own stack Thread 1 Thread 2 Thread 3 Thread 4 Threads require per-thread stack memory ● Four protothreads, one stack Events require one stack ● Four event handlers, one stack Protothread 1Protothread 2Protothread 3Protothread 4 Just like events
  • 7. 7 Main features u No machine specific code since the protothreads library is pure C u Does not use error-prone functions such as longjmp() u Very small RAM overhead - only two bytes per protothread u Can be used with or without an OS u Provides blocking wait without full multi-threading or stack- switching
  • 8. 8 Examples applications: - Memory constrained systems - Event-driven protocol stacks - Deeply embedded systems - Sensor network nodes
  • 9. 9
  • 10. 10 Pt file structure Data Structures struct Pt Initialization #define PT_INIT(pt) Initialize a protothread. Declaration and definition #define PT_THREAD(name_args) Declaration of a protothread. #define PT_BEGIN(pt) Declare the start of a protothread inside the C functi on implementing the protothread. #define PT_END(pt) Declare the end of a protothread.
  • 11. 11 Pt file structure Blocked wait #define PT_WAIT_UNTIL(pt, condition) Block and wait until condition is true. #define PT_WAIT_WHILE(pt, cond) Block and wait while condition is true. Hierarchical protothreads #define PT_WAIT_THREAD(pt, thread) Block and wait until a child protothread completes. #define PT_SPAWN(pt, child, thread) Spawn a child protothread and wait until it exits.
  • 12. 12 Pt file structure Exiting and restarting #define PT_RESTART(pt) Restart the protothread. #define PT_EXIT(pt) Exit the protothread. Calling a protothread #define PT_SCHEDULE(f) Schedule a protothread.
  • 13. 13 Pt file structure Yielding from a protothread #define PT_YIELD(pt) Yield from the current protothread. #define PT_YIELD_UNTIL(pt, cond) Yield from the protothread until a condition occurs. Defines #define PT_WAITING 0 #define PT_YIELDED 1 #define PT_EXITED 2 #define PT_ENDED 3
  • 14. 14 Protothread scheduling A protothread runs in a C function We schedule a protothread by invoking its function We can invoke the protothread from an event handler ◦ Protothreads as blocking event handlers We can let the operating system invoke our protothreads ◦ Contiki Protothreads can invoke other protothreads ◦ Can wait until a child protothread completes ◦ Hierarchical protothreads
  • 15. 15 How do we implement protothreads?
  • 16. 16 Simple example program using protothread --waits for a counter to reach a certain threshold, -prints out a message, and resets the counter. - This is done in a while() loop that runs forever. - The counter is increased in the main() function.
  • 18. 18 C preprocessor expand the above code,
  • 21. 21 C-switch expansion-another example int a_protothread(struct pt *pt) { PT_BEGIN(pt); PT_WAIT_UNTIL(pt, condition1); if(something) { PT_WAIT_UNTIL(pt, condition2); } PT_END(pt); } int a_protothread(struct pt *pt) { switch(pt->lc) { case 0: pt->lc = 5; case 5: if(!condition1) return 0; if(something) { pt->lc = 10; case 10: if(!condition2) return 0; } } return 1; } Line numbers