C Programming

Mknod Example in C

A named pipe sometimes referred to as a FIFO, is a way for inter-process interaction in computing. It is a Linux version of the classic pipe notion. A traditional pipe is “nameless” and only lasts about as long as the procedure does. On the other hand, a named pipe can last as long as the system is operational, even if the process is no longer active. If it is no longer required, it can be removed. A named pipe usually shows up as a file, and processes connect to it for inter-process interaction.

A FIFO file is a type of local storage file that allows two or more processes to interact with one another by reading and writing to and from it. The main drawback of pipes is that they can only be utilized by one process, which can have both readers and writers or processes that use the same file descriptor list; as a result, pipes have a major drawback: they can’t pass data across processes that aren’t related. This is as they do not share a file descriptor table. However, if the pipes are given names, they can be read and written just like any other file. The processes don’t even have to connect.

Named pipes are FIFO, i.e. First in, First Out pipes. The shell can be used to generate FIFOs. They can, however, be done with the C programming by using the mknod() system call. The mode option determines the permissions that will be used as well as the type of node that will be generated. It ought to be a bitwise OR conjunction of one of the file types given below and the new node’s privileges. The umask of the process modifies the permissions as usual: the permissions of the generated node. If mknod() succeeds, it returns zero; otherwise, it returns -1.

Syntax

int mknod(const char *nameofpath, mode_t mode, dev_t dev);

The pathname you are using for the file is the pathname. A set of bits that describe the file type and access privileges you intend to utilize is known as ‘mode”. S_IFDIR, which is used to construct a directory, and S_IFIFO, which is used to establish a FIFO, are the only permitted file types. Most of the time, dev is “Ignored.”

Example

After explaining the basic concept and syntax of the mknod() system call in C programming language, we have defined an example for our user’s understanding. We are implementing our example code in the Ubuntu 20.04 Linux operating system with a GCC compiler for code execution and compilation. We have used the nano command in the shell for the creation of the file. For executing an example in C language, we have used the “.c” file extension.

Let’s execute the listed below command in the console of the Ubuntu 20.04 Linux operating system for file creation.

$ nano w.c

In our case, the file name is “w.c”, you can modify it as well. Now the file will be created and will open in GNU editor. You have to write the script shown in the below-affixed image.

The script can be modified according to your work, but for this purpose, you have to fully understand this basic example. The default file descriptor for a program to write warning messages is Steer, often known as standard error. The error is printed to the output screen or window terminal using Steer. Steer has been one of the command outputs, similar to stdout, which is often logged elsewhere. S_IRUSR is a posix macro constant found in sys/stat. h. The user read permission bit is what it’s called. The prefix S_ RUSR’s could refer to the status, such as Read of User. S_IRUSR is a posix macro constant found in sys/stat. h. The user read access bit is what it’s called. The prefix S_ RUSR’s could refer to the status, such as Read of User. We want to provide the user read and write access in this situation. As a result, we set the mode to S IRUSR | S IWUSR| S IFIFO. Dev is verified if the file type is S_IFCHR or S_IFBLK; however, it is disregarded.

As a result, we used 0 as the parameter. The perror() method returns a standard error message detailing the most recent error encountered while calling a system or library function. After writing and understanding the script, you have to close the file and simply get back to the console. Let’s execute the below command in the Ubuntu 20.04 Linux operating system console for script compilation and execution.

$ gcc w.c

$ ./a.out

By looking at the above-attached image, you can easily identify that no error comes up upon compilation. The expected output can be seen on the screen as well.

Conclusion

This article was all about mknod() system call in C programming language. We have tried our best to enlighten you on the basic usage of the mknod() system call. The syntax of the mknod() system call has been explained for user understanding. The example used in this guide is very simple, and novice users can also understand in the first go. I hope this article will guide you whenever you try to use mknod() system call in C programming language.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content