A.k.
A.k.

Reputation: 302

How is multitasking possible even if it's just context switching and finish all processes simultaneously?

For what I have read multitasking is nothing but context switching happening at really fast speed and hence giving an illusion of parallelism.

But I am confused. Even with context switching whole data has to be processed, then how CPU achieves it at the same time?

For eg. I am listening to 5 minutes long song in VLC and listening another song of 5 minutes in KMP. So CPU has to process 10 min long data. But both songs finish at the same time. How is it possible? Please correct if I have misinterpreted some of the concepts.

Can you provide answers for both single and multicore processors?

Upvotes: 0

Views: 535

Answers (1)

lockcmpxchg8b
lockcmpxchg8b

Reputation: 2303

You are correct that multitasking on a single CPU core is just rapid context switching. This gets a little more complicated when you consider multi-core CPUs, but the idea is largely the same.

Audio devices are a particularly interesting example. They have buffers for playback from which they generate the sound on their own, without CPU intervention. How that works with multi-tasking is that, as the playback buffer gets low, the sound device will notify the OS (or a driver, or an application), and some CPU time will be spent filling the buffer back up. Fortunately, transferring data into the buffer is much faster than playback, so this leaves the CPUs free for all of the other things they have to do.

There's one more system involved in your example: Somewhere in your system, a 'mixer' is implemented. Sometimes this is on the sound-card, other times it's in the OS. The mixer is responsible for taking the sounds that all of the applications want to play, and combining them into a single playback stream that can be fed into the sound-card's buffer. Fortunately, mixing is also not overly complex, so it still doesn't take nearly as much time to mix 10 seconds of audio as it takes to play it back.

Upvotes: 1

Related Questions