I get how multiple processes can run on a single core by interleaving the processing. What I don't understand is how the operating system ensures that the program releases the processor after a certain amount of time. If only one program can run at a time, and the os allows a process to run, what's to prevent that process from never giving the processor back?
As @rachet_freak says, there are hardware timers that fire interrupts that can switch execution to the kernel when your processes' timeslice is up. In addition, whenever you call an OS service, say allocating memory, reading from the keyboard, writing to a disk, etc. the OS code can decide that your process needs to wait, and activate another process.
interrupt from a outside clock with the timeout set at the start of a process' timeslot, which immediately goes back to the OS
In general, the approaches can be classified (conceptually) into two types:
- Preemptive multi-tasking.
- Cooperative multi-tasking.
The details for both can be found in the question which Charles E. Grant has commented on your question.
There is a further classification under cooperative multi-tasking:
- The applications needs to periodically call back an OS function, to allow the OS to perform its work, without benefit to the application itself. Typically this is termed yielding.
- The application may request some services from the OS by making an OS call, such as opening/reading/writing a chunk of a file, or reading from a port or network. The OS could take this opportunity to perform additional work of its own.