a day ago
- Cooperative multitasking lets tasks yield control voluntarily, while preemptive multitasking forcibly takes control after a time slice.
- Function pointers in C store the address of a function and allow indirect calls, with a syntax like `int (*fp)(const char*)`.
- Function pointers are used in standard library functions like `qsort()` and `bsearch()` to pass custom comparison functions.
- A simple cooperative multitasking system can be implemented using an array of function pointers, where each task is a function that runs briefly and returns.
- The example defines three tasks (manatee, goat, bat) and dispatches them in a round-robin loop, demonstrating basic cooperative multitasking.
- Potential improvements include passing data to tasks, using different scheduling algorithms, and dynamic task creation/destruction.