int mkfifo (const char *pathname, mode_t mode);
We are going to perform this example on Ubuntu 20.04. First, we install the GCC compiler and then open the text editor of Ubuntu 20.04 and put some code. We will create two different files here and then read/write data to these files using the “mkfifo ()” function in C programming. For creating a C code, we must put the header files. The “stdio. h” header file is here, which is the standard input/output that includes the input/output information. We have a “string. h” header file, and this header file is used here for the string functions. Then we have the “unistd. h” header file, which is utilized for accessing API. The “fcntl. h” is used for the file controlling, and “sys/stat.h” includes building blocks that make it easier to learn about a file’s properties.
Also, we include the “sys/types.h” header file. We utilize the “main ()” function after these header files, and this function declares the data type as “int”. Below this, we declare the integer “int” and name it “fd”. After this, we declare and initialize the “char” named “myfifo” and initialize it with “/tmp/fifo”. Now, we have the “mkfifo ()” function where we pass two parameters. The first parameter is “myfifo,” and the second parameter is “0666” here. The first parameter is the pathname, and the second is the mode.
Then we declare two arrays of the name “arr1” and “arr2” of the size “80”. We utilize the “while ()” loop here and put “1” in it. Inside this while loop, we have different statements. We utilize the “open ()” for reading or writing to the file. We put the path of the file as the first parameter and then utilize the flag here as the second parameter, which is “O_WRONLY”. This “O_WRONLY” flag is utilized here, which means we open this file to write only. The “fgets ()” function is here for reading the characters.
Then we have the “write ()” function, which is used for writing data from the buffer that is declared by the user and then closing this file by utilizing the “close ()” function. After this, we again have the “open ()” function, but this time we utilize the “0_RDONLY” flag, which means this time the file is open for read-only. We also have the “read ()” which is utilized only for reading the data that was previously written to this file. Then we use the “printf” for rendering the data on the output screen and again close this file by utilizing the “close ()” function. We must close the file which we have opened. Here, we close the bracket of the while loop and place the “return 0” statement below this.
We are creating the second C code here by putting some code lines. We use the same header files in this code, which we have utilized in the above code. Then we have the “main ()” function, and inside this, we declare the “int” as “fd1” and “char” as “myfifo”. The “mkfifo ()” is also utilized here in the same way as we have used it in the above code. Here, we have two “char” arrays of the name “str1” and “str2” and assign the size as “80”. The “open ()” function is used, and the “0_RDONLY” flag is used to indicate that the file is open for read-only access this time. We have a function called “read ()” that is exclusively used to read data.
Then, we render the data on the output screen using “printf”, and we close this file by using the “close ()” function. The file’s path is the first parameter, and the second parameter is the flag, which in this case is “O_WRONLY.” We open this file to write only by using the “O_WRONLY” flag in this case. Here, the characters can be read using the “fgets ()” function. The “write ()” function is used to write data from the buffer that the user has declared before using the “close ()” function to close the file.
Now, we get the output by opening two terminals simultaneously and executing the command on both terminals at the same time. First, we compile both files on separate terminals. On the first terminal, we run the command which is given below and then move to the second terminal.
On the second terminal, we put this command for compiling the second C code file. After successful compilation of both commands on different terminals, we are going to execute both codes.
For execution, we put the given command on the first terminal; nothing will display here.
Now, on the second terminal, we must execute this code by writing this command which is shown here. Also, nothing is displayed here.
On the first terminal, write “hello”, press enter, and open the second terminal.
This “hello” is also displayed here on the second terminal, which we have written on the first terminal.
Here, you can see the outputs of both files in the images, which are given below.
We present this guide to show you how the “mkfifo ()” function works in C programming. We have explained this “mkfifo ()” function in detail here, and we also demonstrated an example here in which we have utilized this “mkfifo ()” function. We have provided the output here as well, so you will easily see how to use this “mkfifo ()” function in C. We hope that this guide will enhance your knowledge of the functions of C programming.