Networking & IoT · Interview question

Why are blocking socket calls a problem on an MCU, and what are the alternatives?

A strong answer

By default socket calls block: recv sleeps until data arrives, connect until the handshake finishes or times out, accept until a client connects. On a desktop with an OS and threads, a blocked thread just yields the CPU to others, so it's fine. On a single-threaded bare-metal main loop, a blocking call freezes the entire program, no other tasks run, no sensors are serviced, and critically the watchdog may not get kicked, so the device resets. The alternatives: set sockets non-blocking (O_NONBLOCK), so calls return immediately with EWOULDBLOCK if they'd block and you poll them as part of your loop; use select/poll to wait on multiple sockets at once with a timeout and service whichever is ready, which scales to several connections without threads; or use the stack's event/callback API (lwIP's raw or netconn APIs) where the stack invokes your callback when data arrives or a connection event occurs, fitting an event-driven bare-metal or RTOS design with no blocking at all. Under an RTOS you can also dedicate a task to a blocking socket so only that task blocks while others run. The rule is to never let a blocking network call stall a real-time loop, choose the concurrency model (non-blocking + select, callbacks, or a dedicated task) that matches your scheduler.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

Sockets

The programming interface to TCP/UDP: the BSD socket calls (socket/connect/send/recv, bind/listen/accept), client and server flows, byte order, and the partial-I/O and blocking traps on an MCU.

More Sockets questions

Browse all 472 interview questions
Why are blocking socket calls a problem on an MCU, and what are the alternatives? | EmbeddedPrep.io