Multitasking vs Multithreading vs Multiprocessing

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
multitasking multi processing and multi-threading now there's three interesting words for you all starting with the word multi well what do they all mean and what difference does it make well my name is Gary Sims and this is gary explains and if you want to find out more about those three words please let me explain okay first of all there can be some overlap in how people use these words and that's okay but if we're talking about a strict definition let's look at these three words in order and see what they mean to us so the first word is multitasking and we use that about people don't we you know he's very good at multitasking she's very good at multitasking and of course we're not actually very good at multitasking you can really only do one thing at once because we don't have one pair of hands but and of course it's the same with a computer it's got if it's a single core CPU it can only do one thing at a time however it gives the appearance that it's doing two things at once and the way it does that is it gives a little bit of time to each of the tasks it has to achieve so if you're running you know kind of like on your on your computer a single-core computer and we'll talk more about single core computers in a moment but if you're running kind of on a computer then you might be running kind of like Microsoft Word and you might also have the Chrome web browser open and kind of a new email comes in to Gmail and yet you can still carry on typing in Microsoft Word you get the appearance that you're doing two things at once and that's because the computer gives a slice of its time to each program in turn and the programs are demanding the most use from the CPU get more time which is why if you have a program that's doing something really really heavy work then the other programs appear to be quite slow when you try to click on them or try to use them and the way operating systems do that using a system called pre-emptive multitasking and that basically means that this the kernel is the very heart of the operating system or that's Windows with us Linux with us Mac OS it says okay now you're getting a slice of time then it says I don't care what you're doing now I'm gonna stop you from what you're doing I'm giving your size of time to somebody else and that's pre-emptive multitasking there is a different version called cooperative multitasking if you fancy a whole video on multitasking you know like cooperative multitasking or pre-emptive multitasking and kind of like real-time or French ISM do tell me in the comments below and maybe I'll think about doing a video just about the concepts of multitasking but basically the operating system divides up the time on each program and if it switches between them quick enough it looks like they're both working at the same time now in a little while we're going to go over to a couple of raspberry PI's and one of the raspberry PI's we'll be using is a raspberry pi zero and it's a single core ARM version six CPU which means it can only literally do one thing at a time it can do one instruction then the next instruction the next instruction and we'll see how multitasking comes into that when we start to run some test programs on that in a moment now related to multitasking is multi-threaded now multi-threading is what happens when you have parts inside the same program inside the same task that actually want to try to do things in parallel now again we're running this on a single core computer it can't do things in parallel it can only do things like the appearance of doing things in parallel so the scheduler gives one thread a bit of time then it gives another thread a bit of time and actually again it looks like that the program is achieving two things at the same time so a good example would be lets say something like Android if it was running on a single core a smartphone like it was way back before we had multi-core smartphones then the GUI the user interface is being updating in one thread and maybe you're downloading from the network with another thread and it looks like the GUI is still is kind of you know interactive and fluid and acting the Downloads happening but what's really happening is that the CPU is switching between the two threads but the point about thread is they're both inside the same task they're both inside the same process therefore they share memory they share the networking they share files and they all contain within that neat little process package that gives the appearance that the protis itself could do multiple things at once and again we're going to go to a Raspberry Pi I mean it will show you a multi-threaded program running on a multitasking operating system and then the final variant there is multi processing and multi processing it means you've got more than one CPU core so today when you're using a pea see might be using a thing called s MP symmetric multiprocessing which basically means a dual-core or a quad core or ever many calls you've got in your processor and actually those processors can do more than one thing at once so you can have a program like you know Microsoft Word that's running on one core and actually at the same physical time Chrome might be running on another core and it is actually doing two things at once and that's the difference between when you're running on a multi-core and a single core ok let's go over to the raspberry PI's and see what we can discover ok so here we are on a raspberry pi 0 it is a single core computer and on the left here we have a window running a program called top which shows you how much CPU is being used by which programs and here on the right I've got two windows where I can run some programs and let's run a program here where it tries to calculate 250,000 prime number using trial by division and it does it ten times sequentially one after the other so let's just run that and see what happens notice here on the left hand side that the program is using 90% class of the CPU and the CPU for the whole system is over 90% ok that took about eight point eight seconds to run now I have another program here which is is actually the same thing but it actually got 10 threads so rather than running the ten programs sequentially it actually runs ten threads and tries to calculate all these prime numbers now let's see what happens so again here on top we can see the program is using over 90% of the CPU the CPU the system is 98 96 % and you can see here that it again took roughly the same amount of time 8.8 seconds so on a single core machine we've seen now how we can run a multi-threaded program and actually it doesn't change anything it still takes exactly the same amount of time and there was no speed benefit no performance benefit to him now interestingly to show us what multitasking is like we can run both of those programs into twice so we're gonna run it in these two windows so to see if I can get them going as quick as we can there we go now notice here we've got both running and they're both using roughly 50% of the CPU because they're having to share the resources between them whereas the system CPU level is up in the high 90s it just went to a hundred percent they're a second so the overall system is still using the CPU as much they can and notice here it's finished now and both with them took over 17 seconds well of course it was eight point eight seconds for a single run of it so if we're running to lots of it where it takes twice as long and the poor old CPU just has to do what it can and this is multitasking because it's trying to run both of those programs at the same time and they're both multi-threaded programs so there was 20 threads running there in total and the CPU just ran as quickly as it can trying to give the appearance it with doing multiple things at once but of course it wasn't it was actually just slicing the time between each one okay now let's go over to a Raspberry Pi 3 because here I'm on a Raspberry Pi 3 and the Raspberry Pi 3 is different because it has four causes have got a quad-core processor now first of all I'm just going to run the program exactly the same program as we had running on the Raspberry Pi zero and I want us to see here actually it runs much much quicker it will come out at about 4.1 seconds there we go and that's because the core the core designs inside the Raspberry Pi 3 is better they're fast those clocks at a faster speed and they're a better design so actually which is quicker doing exactly the same program and it was only running on one core now we're going to run the same program again but I've upped the number of prime numbers now to a million so that we can get a bit of longer to talk about what happened so we run this program now and what do we notice here first of all that it's running at a hundred percent okay on one core again so the overall system a load that clearly 25% because there are four cores and it's doing a hundred percent on one core as it tries to run that non-threaded program to calculate those million numbers ten times in a row in a sequential in a sequential pattern so it is maxing out one core and the other three cores are not doing anything and that took about 29 seconds there we can see from that timing now we can now run the threaded version of the same program again now using a million numbers for to make it a bit longer and if we hit go the first thing we notice is look at this it's using now almost 400% and the reason why it's saying 400% is because it's a hundred percent of four cores and now you can see here this finish much quicker four times faster because it finished in 7.5 seconds because it was using all four cores while it was doing it so running a multi-threaded program on a multi-core pro Cee'd PU allows the program to run much much quicker in fact we can now do multi processing and multi-threading by running in both windows here these two programs let's run them as quick as we can there we go now first of all notice we've got two running okay and they're using about 200 percent of the CPU time each which means they're using two cores each but yet the overall system time is still up here at 100% because all four calls are being used and of course this will take about twice as long because of twice the work and there we have it 15 point six seconds fifteen point six seconds because the programs were doing it together and that's an example of multi processing of two programs running multi threads and the calls were divided up but as the best they could really two calls were maxed out one program and two calls were maxed out for the other program so there we go multi processing multi-threaded and multitasking all together and there you have it my explanation of multitasking multi threading and multi processing I really hope you enjoyed this video if you did please do give it a thumbs up please subscribe and please share this video on social media okay that's it I'll see you in the next one
Info
Channel: Gary Explains
Views: 78,423
Rating: undefined out of 5
Keywords: Gary Explains, Tech, Explanation, Tutorial, Multitasking, Multithreading, Multiprocessing, Operating Systems, Concurrency, parallelism, Linux, preemptive multitasking, symmetric multiprocessing, multi-threading, heterogeneous multiprocessing
Id: Tn0u-IIBmtc
Channel Id: undefined
Length: 10min 30sec (630 seconds)
Published: Thu Jul 26 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.