Introduction to FIFOs (aka named pipes) in C

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in the previous lesson we looked at pipes and a very nice example on how to use pipes but one thing was clear that pipes could only be used by processes that are in the same hierarchy right because here if I try to create a simple pipes I go OK in file descriptors of two and then just let's say pipe of FD equals negative 1 then with on one if that's the case well you see here I'm going to have F DS meaning file descriptors and these file descriptors are very special in the sense that if you do fork the process they are going to be inside the system the operating system itself they are going to be copied over from this process to the child process right so they can be used in that process and closed on their own their own and whatnot okay so these are sort of special entities now you can't really have a a pipe between two processes that aren't on the same hierarchy can you now there is another feature similar to pipes inside UNIX systems that features called feet balls and what fee Falls are are just basically a file type to which you can read or write from any process that you open ok so they are basically very similar to our file here we can start by creating that feasel in this C programs and an actually remove this pipe and create a FIFO file that is going to be used as a pipe really it's going to be a FIFO is really unnamed pipe so you're going to be you're going to see this FIFO file referred to as a named pipe in certain situations so they are the same thing not worry about it so let's let's start here first things first I have to a few files I'm gonna have here besides uni STD which is pretty normal to have we have here include C's C / if it was that dot H and I also want to include these slash types dot H and one more thing I have to include air now dot H which you might remember from other videos here what we have to do is first create the FIFO file so to create this FIFO file we just have to call in the function NK t fo from make FIFO and this guy takes in the path the path to a to the file that we want to create so here I'm just going to create it in the current directory I'm gonna name it just my FIFO I'll say one and the second parameter is the permission bits and they're basically a way of telling who can access what in a file in a UNIX system here I'm just going to say that in I'm gonna passing the value 777 in octal in base eight basically and this basically that is the operating system that this file is going to be able to be read from or wrote to by anybody in the system ok so I hope that's fine ok and then a semicolon so now if I try to run this and if I go to the Explorer of my IDE or in your own Explorer or in the terminal anywhere you want you're gonna notice that we have created a file named my fee for one inside the current folder that we're working in now there's a few more important things we have to do here besides just calling MK FIFO and the first thing is to check the status code you know with most unique functions you're gonna actually get a status code returned to you and that could be 0 negative 1 or something meaningful other than those two in this case MV if you were just returns either 0 or negative 1 and negative 1 is if something bad happened and zero if it's fine so what I'm going to do here is say if mkp 4 is negative 1 then let's say printf could not create FIFO file backslash N and then just return 1 and if I run this now again you'll notice that actually I get this error message could not create FIFO file that's because the file already exists and because it already exists there's nothing else to be done but since we after the code here is executed we actually just expect the file to exist we don't care if we created it in this instance on or in the previous one what we can do is check using this ere know whether or not that error is just that it already exists what we can say is here if our know is not he exists so if it's not that that is that something else happened besides actually the file just already existing so after calling NK FIFO our know is e exists that just means that the the file the FIFO already exists and we don't really need to do anything because we're fine with that we can continue execution so if it's not that what I want is just print out an error and return out of the problem this is a nice way of doing things ok so now from here onwards we actually have the FIFO file in there right we can see it here it's created it's nice but what can we do with it well it's like any other file we can open it we can write to it we can read from it like we did with pipes more or less there's one more step before we actually get to the code it is very similar to what we did with pipes and it is to open the fever file just creating it is not enough we have to actually open it and for it to open we have to call the open function which is actually in another header file and it read here F C NT l dot H we can call open and this open takes in again the file path so I can just type in here my P for one and then the open flag so for what purpose do we want to open this file well I want to open it to let's say write something into it so o underscore W are only uppercase so that would open the FIFO but this guy actually returns something very very very important and that is the file descriptor now this file descriptor you notice we work with when working with pipes but in that case there was no file you what you would just create the pipe and you straight up get the file descriptors but in this case we create a file and then we open it and that gets us the file descriptor so int FD and notice here we only get one because well we only opened it for writing so that's to be expected now we can actually use it just as we did with a pipe so we can say let's say we want to write weight I can just say right I want to write to FD notarize the value let's let's get here a value I say x equals let's say 97 why not and I'm going to actually type in the address of X and size of X or size of int whichever you prefer okay and I'm going to close this file descriptor so that we don't leave it open once we're done execution of course that's also error check this write code and see if right is it return negative one then and or something bad happens I'm just going to return the error code too but usually we shouldn't get into this so that's to be expected okay so now let's try to run this program right all we do is well create a FIFO file that well it's not going to create on this instance but just to make sure everything works collecting I'm just going to remove it so delete it from here and it doesn't exist it's gonna create it in this instance we're gonna open it I'm gonna try to write this 97 to it like any other file if I run if I launch it this you're gonna notice that the terminal just hangs it doesn't do anything we just wait there and nothing's happening what what gives let's stop the execution first and we can see inside the file explorer that the v4 got created so that's nice at the very least we don't have to worry about that too much the issue is why did it Hank well to find out the simplest way to actually find out if something just hangs print some messages so I can say here let's say printf opening up owning v I'm gonna type in P for every single time fancy opening then I'm gonna say here opened old fund and then I'm gonna say here after this is you know written and then also closed and I think I forgot the backs of hands I'm gonna actually add them one at a time here okay so we should get some messages on the screen so if I launch this again okay so we get the opening message but that's it so it just hangs there what what's the issue if I stop the execution we can notice that that means that the program hangs at open right so once it hits open just pops nothing's happening no error because otherwise the program would actually terminate with some something right but it just hangs now the reason behind that is a really special functionality for V Falls and that is something that's written in the Linux manual so if you go ahead and search on the internet for the open manual page and here we are here we have the open function and how what it does and you can't use for quite a while till you that information but if we go down it's somewhere out here and the first FIFA was the real so V Falls I think it's a bit too large make it to be smaller so what does this a 44 it says that opening the read or write and of a FIFA blocks until the other end is also opened by another process or thread what does that mean well that means that if you open a fever for writing the open call tanks or blocks as it says in the docs until another process comes along and opens the same FIFO for reading right and then when the other process actually opened it for reading the process that it opened for writing will continue is that clear I can actually show you guys what's up with that so here what I can do is let's first just remove the FIFA so that we have a clean slate doesn't really matter I don't think that just to be safe if I launch this I'm just gonna get a terminal that hangs at opening okay so I'm gonna put this on the side and I'm gonna open another terminal so on the Left I have the actual program that we wrote and here on the right I have my own terminal to the folder that we're working with so here if I have say LS we're going to get the main executable may not see my own fifo file and that's about it what I can do with this is actually read from these my few folks remember this guy will write a value a value two to that fever and now it's waiting with quite really just waiting for a process to read from that fever what I can do is just use the the program called cat to just read whatever is in this file and if I hit enter notice that this guy has finished its execution no longer hanged it actually continues its execution from that open call and it opened it or and it closed and everything went smoothly from then on so you see if he falls you always have to have both ends opened at the same time that's their special property if you don't do that they're the one one of the opens is just gonna hang there okay or all the opens if you have just if you open the same FIFO multiple times for writing all of those open calls are gonna hang because there's no process that opened it for reading okay so in this case what happened is we read from this and as you notice I have your a small a and this is not an error or anything this is actually the content of this my FIFO that's because I played the little trick on you guys and here I said right X right right whatever is on X and that X actually has the value 97 on it and 97 if you take a look at an ASCII table is actually the value a so I can say here instead of a let's say I know a percocet or yeah you can you can really do whatever you want but if I do this and art the program and get a terminal here and again say my fifo 1 and hit enter you're gonna notice I get an uppercase Z here so that is that is why you get an analog is a is why I get an uppercase Z in this case so that is what we actually wrote into that my FIFO file and we have to read it on that terminal on that process and only after that process started executing open the dead FIFO for reading this guy continued its execution now you notice here there's no communication really between processes what we have here is just a simple problem just one program one process everything is just one single line of execution that's because the other process that was reading well it wasn't wrote whereas right it wasn't created by us it's just a process a program that already exists but before actually getting into communication between processes using this FIFO or C it's gonna be very simple I just wanted to get this out of the way because it is very important to understand from the get-go otherwise you could be very confused about why and how certain things are synchronized properly this special property of ethos is one of the main reasons we actually use it for process communication and not just plain old text file you might have realized if we actually open the FIFO for reading we aren't having anybody write to it's also gonna hang so here if I actually run that CAD program on my FIFO without doing anything you'll notice it just stops it doesn't do anything it I can type in kill stop but doesn't do anything unless I just terminate it forcefully so it also waits for its right end to open it for writing do not hear that if you have to change this from just write only - oh it's rd w power that's for both read and write if you do that actually if you launch the program it's going to terminate because you have it open for reading and writing and well you wrote to it and you also open it for reading so it does have to wait for anything to open again because it is already open for reading so it's gonna finish its execution okay so then in that case it's not gonna wait now one more remark here is that you don't have to actually call MK FIFO from AC file you can first create the FIFO file yourself using the MK FIFO program so I can see here so here I have my FIFO once you can say m MK v all my three fold to let's say and if hit enter you'll notice it created my FIFO to here as well so in that case you wouldn't have to actually add this code but I added it for completion sake and it's usually is very nice for the program that is writing to that fee for to create its own FIFO and also check if it already exists if we take this it shouldn't really need to edit out or anything like that and one more important step before we finish this video is you should actually check what open returns so if this FD is actually negative 1 FD is negative 1 then you should actually return an error code that means that something that happened and it couldn't open the file doesn't exist or something else not that it hangs but because of other issues now that's all there is to this video in the next video is going take a look at how to communicate between processes using this FIFO concept and well I hope you got something out of this video if you do have any questions please do leave them down comments below or on our desk or server thank you so much for watching take care bye
Info
Channel: CodeVault
Views: 19,532
Rating: 4.9635701 out of 5
Keywords: fifo, pipe, named pipe, open, write, read, process, linux, c (programming language), codevault
Id: 2hba3etpoJg
Channel Id: undefined
Length: 18min 49sec (1129 seconds)
Published: Tue May 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.