Difference between processes and threads

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
now that we looked how to create threads i want to take a look briefly at the difference between threads and the processes because at first class they look much the same thing right we're just executing uh just some code at the same time right with threads we have a function we execute with with processes we just execute the the code from fork onwards by the way if you haven't watched the uh playlist about processes i do suggest you first go ahead and watch that i have a link up top and uh because this is gonna be sort of a continuation to this to that uh playlist anyway moving forward um so what is actually the difference now to illustrate the difference between the two uh concepts threads and processes i'm gonna have here two uh main files the left one is gonna be implemented using threads and right one we're gonna implement everything using processes and we're gonna see the difference between the two in here so first things first let's create two processes and two threads uh on each of the source files so that we have something to work on so to create two processes we can simply call here uh paint uh speed equals fork of course if p is negative one then we return an error that's simple as that and well if speed is not zero then we should wait of no for the child process to finish its execution so that's simple enough and if we print here something like i know hello uh from processes let's say and i launch this we should get two lines of text down here so that's straightforward i think that's pretty self-explanatory now uh let's do the same thing with threads so with threads we have to create a function we have to say void pointer this has to be avoid pointer and call it again routine that's simple enough and then i'm gonna i guess just say printf hello from threads this time and as we've seen in the previous video just say p thread d t one then we say p thread create uh the address of t one null and routine then note here as well as the arguments we don't need any arguments and p thread join p1 and no we don't care about the return values and of course i'm going to check for errors so i'm going to do something like if uh if this then return one and if this then return to let's say and if we try to launch this we only get uh one line of text on the terminal so we're going to have to call this one more time so i'm going to create another thread here so p thread create t2 and of course to define it here i'm gonna change these error code so that they are different copy the join t2 and that's about it so we're creating again two threads they both call routine and we should see this message printed twice on the terminal so now if i try to launch this you will notice we get this twice fair enough so that works now let's say i want to print first the process ids right what happens if i print the process ids in here let's say process id percent d and to get a process id you just call get feed and if i try to launch this we should get two different numbers right we got twenty five thousand nine hundred thirty two and twenty five thousand hundred thirty eight so we got two different process id so that's expected what happens if we do the same with threads so if instead say this we're gonna call say process id process id percent d and then say get b alright and launch this what's gonna happen well you will notice that you actually get two of the same ideas so what's going on here why are they not different that is because processes can contain multiple threads right so you can have multiple threads inside a single process but of course you cannot have multiple processes inside a single thread so sort of processes encompass the environment of threads and you can have multiple of them that is one difference now the second difference is regarding the address space remember whenever we work with processes i said okay when we're forking we are duplicating all the variables into the fork into a child process so we get two different variables basically one on each process and we can modify them individually but with threads there's a big difference where we have all the variables in the same place so all threads can access all variables and they all have a common very common set of variables common address space what we can do to illustrate this is first we can do a simple addition we can have here a variable in x equals 2 inside the processes source code and what we can say is if b is zero so if we are in the child process increment this x okay and i want to print this x value on the terminal but uh to make sure that we are actually incrementing it and then we are printing it on both processes i'm gonna first sleep for let's say two seconds and it's gonna that sleep is gonna occur for both processes and then we're gonna printf value of x by equation actually something like that and just x here and we no longer need this printf with the process id now if i try to execute it a an interesting thing will happen and it is well for one process value of x was 2 and for another value of x was 3 and since we waited we are sure that this operation occurred but as you can see uh only in one of the process the x variable got incremented on the other one it stayed the same uh as it was initialized now if we do the same with threads how can we do that now to check that we've tried is a bit more tricky than that so i'm going to create another function call it routine oops routine two and i'm gonna change the second thread to actually call this function so uh thread one is gonna call this and thread two is gonna call this and thread one what it's gonna do is well we have here a variable x equals to two as we said i'm gonna do x plus plus and i'm gonna sleep for two seconds and after it i'm just gonna print f well whatever i printed in here so i'm gonna write it again just that and i'm gonna do the same thing on the second thread except i'm not gonna increment it so we're just going to uh increment it in the first thread now let's see what actually happens if i try to launch this you will notice that the value of x is actually 3 on both trends right so even though we have only incremented it here in the thread one let's say thread two went along and slept for two seconds like this one so we made sure that this printf was executed after the x plus plus and we have in fact printed on the screen the exact value that was incremented by tread one so all in all threads are sharing memories this x if you add a number to it even if i add here x uh plus equals five anything if i do anything in one thread and execute it of course it's gonna be seen in the other thread as you can see here and that is the one one of the biggest differences between threads and processes so aside from those two uh differences are that well with threads all of the threads actually share the file handlers whereas with processes all the file handlers are duplicated uh all the really all the system resources are shared between all the threads on a process uh the security context is shared and so on and so forth but these are just details that we're gonna get into at some point the biggest difference is that they share memory but there comes a trade-off because since they share memory that means that there could be certain issues when it comes to certain threads modifying the same variable and that's what we're gonna get into at a later point but this is what i wanted to keep in mind for this video thank you so much for watching if you do have any questions please leave them down the comments below or on our discord server the source codes for both of these programs are gonna be on our website and leave a link straight to them on in the description below you can check it down there download it execute it whatever you want to do with it alright take care bye
Info
Channel: CodeVault
Views: 11,741
Rating: 4.9729729 out of 5
Keywords: threads, process, c (programming language), codevault
Id: IKG1P4rgm54
Channel Id: undefined
Length: 10min 30sec (630 seconds)
Published: Wed Dec 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.