Hello and welcome.
In this video session we will discuss on the concepts of Fifo.
Fifo is nothing but first in, first out.
A Fifo is also similar to a pipe.
In the previous video session we saw the discussion discussion on pipe.
A pipe is also in fact a first in, first out.
Let us see.
What are the difference between a Fifo and a pipe?
A Fifo is also called as a named pipe.
And the main difference between pipe and Fifo is that a Fifo in fact has a name in the file system and
it is opened in the same way as we open a regular file, whereas a pipe does not have any name in the
file system.
This means that the Fifo has a persistent in the file system and even.
After the program stops running or it exits, the Fifo still remains in the filesystem as if it is a
file.
But a pipe will be deleted or a pipe will not exist as soon as the process which has created it terminates.
Also one of the main difference between a Fifo and a pipe is a Fifo can be used to send data.
Between two unrelated processes, but a pipe was used only for communication between a related process,
like a parent and a child.
A Fifo once it is open.
We can call all we can make use of all the system calls that we use on a files like open read write
close.
So in order to.
Operate on the Fifo.
We make use of the same system calls that we make use on the normal, normal or a regular file.
Well, Feefo also has a right end and a read end.
Similar to a pipe.
A right hand is is a is one in which the data is written onto the Fifo and read end is the one in which
the data is being read from the Fifo.
So a V4 is nothing but a first in first out.
The first written data will be.
Ridout first.
Let's see the syntax of creating a Fifo.
In order to create a Fifo, we need to include six slash stat dot h and make Fifo is the system call
used to create a Fifo and it takes two arguments.
The path along with the name as well as the mode.
We will see in details how to use make Fifo in the example.
Upon success it returns a 0 or -1 on error.
Now let's see some examples in order to understand the Fifo working of a Fifo.
Now let's open a simple program.
So here we need to include all the required header files, and here we have a main function.
And.
We are creating a Fifo.
So before creating a Fifo we have to specify the path in the file system where it is created.
So here we have chosen slash temp and the name of the Fifo will be my Fifo and this is a buffer used
for reading and writing the data onto the Fifo.
Now make Fifo is the system call to create a Fifo and the first argument is the.
CFO, but where we are about to create the CFO.
And so here we are specifying slash time slash my Fifo.
Right.
This is the mode in which we are going to create a Fifo.
So if we see this, this is similar to the file creation in which we are creating a file and we are
specifying the permission in which we have to create the file.
In the same way a Fifo will take the path name in which location it needs to be created along with the
name of the CFO and in which permissions it needs to be created.
Now, once it is created we have to open the Fifo in order to operate on the Fifo.
Now we are opening this my Fifo.
That we have.
Created.
And in this case, we are opening in a write only mode.
So here, if you see the permission, what we are creating is 660.
That means user can read.
Write.
Groups in red.
Right.
And others don't have any permission.
And now we have opened the fee for that we have created.
And in a while, one loop we are.
Getting the string from the user input and using the right system call.
And now if the refers to the Fifo and we are writing to the.
We are writing the data onto the file descriptor that we have opened and we are we are writing the data
that is present in the R2.
That is nothing but the buffer in which the string is stored and.
Finally we close the file.
So now let's compile this program.
Let's name this as a right.
Before executing this, let's go to the location of this slash temp.
So here we are in slash temp location and let's see the contents of this.
So in this, if you see, there is no.
My Fifo file which is present.
So my Fifo is the.
People, which is going to be created.
So if you see here, there is no Fifo by the name my Fifo now, now executing the right Fifo program,
let's consider the read Fifo program.
So in this program we are including the required header files and here we open the Myefo in the right
Fifo program.
We had specified the path of the Myefo, so now we are specifying the same path.
Now let's open the Myefo.
In read only mode.
So this operation is similar to opening a regular file.
So now, as we have mentioned in our.
Presentation that Fifo is also considered as the regular file operations.
So all the system calls of regular file can be used on my on the Fifo.
So now by opening we are open, we are opening the my CFO and we are specifying the read only mode and
in a while one loop we can read the Fifo by specifying the file descriptor.
So open returns the file descriptor.
Now if one represents the Fifo, which we have opened in reading mode and by calling a system call called
as read, we can read the contents of the Fifo onto the string one, which is nothing but a buffer here,
and the read string is printed onto the screen.
And finally, we can close the file descriptor.
Let's compile this program.
It's.
Name the executable as read here.
So now we have two executable files.
One is the reed, one is the right.
So right is the executable file created by the right program.
And read is the executable created by the read Fifo program.
So now.
Let's run the right Fifo program.
Now in the other tab, let's run the read Fifo program.
So now we have we are running two processes in parallel.
That is a write process.
Write, Write runs as a separate process.
And the read program also runs as a separate process.
So now we now we have two unrelated processes running in the memory.
Now let's enter some parameter.
So in the right.
Program we have entered.
Hello one.
And if you see here in the right program.
Running, we have received the data from the Fifo.
So.
From this process we are writing to the.
CFO that is present here, slash temp slash Fifo and the read program is also reading from the same
slash temp slash Myefo.
And the data that has been read is printed out to the user.
So now.
We can see that we are able to send the data from one process to the other process, and both the processes
are not in a not related process.
If you can see, we should have got this data that we have sent from the right program on to the right
program.
So this is the data that we have received in the other process.
Now let's see one more aspect of the Fifo.
No, this is the path in which we have created the program.
Let's open the right program as well.
So.
In this case.
We have the right program and the right program.
So both the program are.
Referring to the same C4 that is slash temp slash my C4.
Let's go to slash temp.
This is the path where the Fifo is created.
Now if we do LS minus L, we can see that my Fifo is the.
Seafood that is created and we can see that it is the type of the file is P here.
So this is this is nothing but the named pipe.
Now let's delete.
Let's remove this Fifo.
And let's check the contents.
So now my program is not there.
Now, as soon as we run this write program, we will be creating a Fifo.
For example, when I run the right program.
So.
We are calling a Mkfifo by calling Mkfifo.
We are creating the Fifo for the first time.
In the permission given file.
Now let's run this program.
Now the right program is run.
And now if we check the contents of.
Then we can see that we have created a Myefo.
This is nothing but a named pipe.