Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question already has an answer here:

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?

share|improve this question
add comment

marked as duplicate by Charles E. Grant, MichaelT, World Engineer Jun 8 '13 at 3:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers

up vote 6 down vote accepted

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.

share|improve this answer
add comment

interrupt from a outside clock with the timeout set at the start of a process' timeslot, which immediately goes back to the OS

share|improve this answer
add comment

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.
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.