Search code examples
multithreadingparallel-processingoperating-systemsystemmulticore

What will be going on inside my computer if I run a python and matlab program at once?


Suppose I have a multi-core laptop.

I write some code in python, and run it; then while my python code is running, I open my matlab and run some other code.

What is going on underneath? Will this two process be processed in parallel using multi-core auomatically?

Or the computer waits for one to finish and then process the other?

Thank you !

P.S. The two programs I am referring to can be considered the simplest in nature, e.g. calculate 1+2+3.....+10000000


Solution

  • The answer is... it depends!

    Your operating system is constantly switching which processes are running. There are tons of processes always running in the background - refreshing the screen, posting sound to the speakers, checking for updates, polling the mouse, etc. - and those processes can only actually execute if they get some amount of processor time. If you have many cores, the OS will use some sort of heuristics to figure out which processes should get some time on the cores. You have the illusion that everything is running at the same time because (1) in some sense, things are running at the same time because you have multiple cores, and (2) the switching happens so fast that you can't notice it happen.

    The reason I'm bringing this up is that if you run both Python and MATLAB at the same time, while in principle they could easily run at the same time, it's not guaranteed that that happens because you may have a ton of other things going on as well. It might be that both Python and MATLAB run for a bit concurrently, then both temporarily get paused to allow some program that's playing music to load the next sound clip to be played into memory, then one pauses while the OS pages in some memory from disk and another takes over, etc.

    Can you assume that they'll run in parallel? Sure! Most reasonable OSes will figure that out and do it correct. Can you assume that they exclusively are running in parallel and nothing else is? Not necessarily.