How to use FIFO files to communicate between processes in C

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
i do know how to actually create fifos and how they work behind the scenes let's actually create a sort of useful little program or two programs to be specific one that's simply gonna generate some numbers and the other one is going to read those numbers from the product generated them and print out a sum simple as that how do we do this using fifos well first things first i have here like two files all right so we have made one main two this is where we're gonna add the code but before we get started we should create the v4 itself i'm gonna actually create it from c this time i'm actually gonna create it from the terminal so if i stay if i say here let's go to the proper folder and as you can see i don't have a fifo here if i say make fifo let's see let's call it sum and in that case we're gonna get a sum that is a fifo nice now all we need to do is just open that we don't need to care about creating it so first let's simply generate let's say an array of some numbers so that would be pretty easy just let's define here the array and then so something like this i have an array of five numbers here and uh we're just generating numbers up to 99 inclusive and i also added the time.h header file and as you might notice i have added all the other header files that we need to actually use fifos so make sure you include this in your program if you're trying this out okay so that was simple next up what we have to do is open the fifo so here i'm gonna actually scroll a bit down and let's let's see first things part we have to call that open functions i'm gonna say in fd equals open the file is called sum and it's in the current working directory so that's fine and we're going to open it for writing and again make sure you check if your file descriptor is not a positive number because that means that an error occurred and this is a good idea if you in the future will professionally work with these types of things um it can get very messy if you forget to actually check it's very important so this will save a lot of hours of debugging to find out like what's wrong okay now that we opened it as we know from the previous video uh if we get here with the execution that means that we have also opened the file the fifo for reading from another process so this is amazing that means that we can already write to it well how do we do that well let's just start a simple loop here and in here what i'm going to do is actually call right fd and we want array of y and we're of course writing just an integer but i also want to check if this right call returned negative one because again if it did well we're just going to return an error code and we know that something bad happened and uh yeah just stop execution doesn't matter okay now that we wrote all this i also want to print out on the screen the fact that we wrote that so let's say printf wrote percent d backslash n and then array of i okay so we know like what's happening behind the scenes and lastly we want to close the file descriptor nice so we have generated the array we have sent it over through the fifo and we also have closed it at the end here now in the other program what do we have to do here well you see i have the same template file nothing different just a library included up here what we have to do is first define a place where we can store this uh array so let's say here array of five like that and we don't have to initialize it we're just going to start by first reading so in fd equals open so we want to open the sum for o underscore read only so we want to open it for reading and again this check so ft is negative one return something and just return one okay we want to read so we want to read we want to read inside a for loops and i'll just say in i for i equals zero i less than five i plus plus of course read this time not right inside the address of array of i and we want to read just an integral just a simple integral and if this actually returns negative one we know something bad happens so we just kind of written an error code we don't really care much about it what i want to do is also print out the status so here i'm going to say printf received percent d backstage and array of i so that we know what's happening it's kind of difficult to sometimes wrap your head around what's happening so it's very useful to actually have these messages around when you're trying to debug because really debugging multiple programs can get kind of messy okay so we have now the simple message and what we can do here is just take a break and try to execute these two as they are not before forgetting to close the file descriptor now this time around to actually execute two programs at once what i'm gonna do is actually execute them from the terminal all right so i'm not gonna use the id just i'm to use the id just for building so remember to build the program before you're actually uh launching it right so i'm going to just hit ctrl shift b and it should build the active file and same thing for our other program um you can do this of course from the terminal if you want i just prefer it this way it's much faster for me right now and here i have the two terminals so let's launch first main one and as you can see it's gonna block as we know that's what fifos do when you try to open them and then if you try to open the second one as you noticed we got uh all the numbers generated here so we wrote 8 18 10 89 24 and in the same order we got them received on the other program so we know that this works this is very nice we know that inside that uh array here inside main two inside each array we have the elements stored all we have to do now is just sum sum them all up and we're done so just something like this i have here a simple sum variable just a for loop that sums them all up and a simple printf so if i save and compile don't forget to compile if you're running them uh from the terminal and now if i try to launch this so main one okay so that doesn't do anything that's great and main two you're gonna notice that it says that the result is 316 and i'm gonna i'm gonna trust that that's the correct result it does it does seem like it it's like a hundred a hundred and uh let's say 200 and this is about 300. uh yeah so now that this already works now one homework i actually have for you guys uh is to try to take this sum so we have here in the main two we have this sum and i want you to instead of just printing out on the screen to send it back to the other program right so uh similarly to how we did here but just write the sum first and then read it on this other side right so we're gonna have to open the fifo again but for reading for the for main one and for writing for main two right i hope this is not too complicated and it's gonna get you actually to understand some things there's one optimization that i want to do here and that is regarding this uh reading and writing pattern here i am doing it fairly inefficiently right i have what do i have here with it what do i want to write i want to write an array right an array is just a set of bytes that are or a set of integers really that are one after the other physically in memory well if they are like that can they just write them all at once with just one single right column yes indeed i can and instead of having a for loop so i can remove this for loop and i guess we can comment this all for now right at the array so this array is going to evaluate to the address of the first element in the array and we want to not only just write an integral we want to write five integrals okay and then if i actually get the top back here and i guess we can move the printf to the generation part so you can say generated instead okay and this will simply write five integers one after the other in one single call and it's going to be much faster than doing it like we did it before now in uh main two well we can actually leave it as is because if you do five reads of one integral or one read of five integrals technically it's going to be the same thing it's gonna do the same exact thing so in this case if we actually try to launch these programs first build them so if i launched the main one here as you can see we generated them we did not actually write anything yet and then if we launch the second one we actually receive all the numbers one by one in that for loop even though we sent them all at once that's because well uh in the fifo there's a buffer right and there's a buffer and we have five integers in the buffer but whenever we actually just call this uh read here there's just one element taken from that buffer right we can take it five times until we just run out of things to take but of course it's going to be much faster if we simply read all five elements at once so we can do that instead so this and then we're gonna tab it back read array again array is just gonna evaluate to the address of the first element in the array and then size of int we want to again read five integrals if we compile this and we try to run it so let's say here main one that generated the numbers and main two the result is 270 and actually to make sure that we have the proper calculation here i can limit the the number sizes so we can say here in mid one instead of percent 100 for the array you can say percentage 10 and if i build this and 90 piano so as you can see we generated just single digit numbers and if we run main two here we can get 28 that's because 7 plus 4 is 11 this is 20 and 20 plus 8 is 28 so that is the correct answer as you can see we sent these numbers that were randomly generated to this other uh program through a fifo that we have created and the main takeaway from this is that you can send batches of data so if you have an array you can send it like this just tell it how many integers is in that array and just give it the address of the start of the array right and then on the reading end you can do either this or you can have a for loop doesn't really matter for the program itself it's uh really the same thing because the fifo itself has a buffer in there so you can write 100 integers but only read like 20 or maybe 20 every 5 seconds or something like that you can do all sorts of things but i hope you got something out of this video remember try that a little homework that i gave you to actually implement i think it's a very nice exercise to get you started with this and if you do have any questions do leave them down the comments below or on our discord server take care bye you
Info
Channel: CodeVault
Views: 12,626
Rating: 4.9881306 out of 5
Keywords: fifo, fork, process, c (programming language), codevault
Id: dniz3nEOEjU
Channel Id: undefined
Length: 13min 36sec (816 seconds)
Published: Wed May 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.