How does task scheduling work in an RTOS, and what are the differences between cooperative and preemptive scheduling?