Day15 - User-Space Event Loop and poll()¶
Lab 1: Blocking read¶
Test¶
Observe¶
- Program blocks until event
- CPU usage is low
Lab 2: Non-blocking read¶
Python example¶
import os
fd = os.open("/dev/mygpio", os.O_RDONLY | os.O_NONBLOCK)
while True:
try:
data = os.read(fd, 128)
print(data)
except BlockingIOError:
pass
Observe¶
- CPU usage increases (busy loop)
Lab 3: poll()¶
import os
import select
fd = os.open("/dev/mygpio", os.O_RDONLY)
p = select.poll()
p.register(fd, select.POLLIN)
print("Waiting for events...")
while True:
events = p.poll()
Observe¶
- No CPU usage when idle
- Wake only when IRQ occurs
Lab 4: Multiple FDs (optional)¶
Add stdin:
→ understand multiplexing