Special files

The files other than regular files, directories, and link files are special files. They are as follows:

  • The block device file
  • The character device file
  • The named pipe file
  • The socket file

The block device file

A block device file is a file that reads and writes data in block. Such files are useful when data needs to be written in bulk. Devices such as hard disk drive, USB drive, and CD-ROM are considered as block device files. Data is written asynchronously and, hence, other users are not blocked to perform the write operation at the same time.

To create a block device file, mknod is used with the option b along with providing a major and minor number. A major number selects which device driver is being called to perform the input and output operation. A minor number is used to identify subdevices:

$ sudo mknod  block_device b 0X7 0X6

Here, 0X7 is a major number and 0X6 is a minor number in hexadecimal format:

$ ls -l block_device
brw-r--r--. 1 root root 7, 6 Aug 24 12:21 block_device

The first character of the first column is b, which means it is a block device file.

The fifth column of the ls output is 7 and 6. Here, 7 is a major number and 6 is a minor number in decimal format.

A character device file is a file that reads and writes data in character-by-character fashion. Such devices are synchronous and only one user can do the write operation at a time. Devices such as keyboard, printer, and mouse are known as character device files.

Following command will create a character special file:

$ sudo  mknod  character_device  c 0X78 0X60

Here, 0X78 is a major number and 0X60 is a minor number that is in hexadecimal format.

$ ls -l character_device  # viewing attribute of  character_device file
crw-r--r--. 1 root root 120, 96 Aug 24 12:21 character_device

The first character of the first column is c, which means it is a character device file. The fifth column of the ls output is 120 and 96. Here, 120 is a major number and 96 is a minor number in decimal format.

Named pipe file

Named pipe files are used by different system processes to communicate with each other. Such communication is also known as interprocess communication.

To create such a file, we use the mkfifo command:

$ mkfifo pipe_file    # Pipe file created
$ ls pipe_file      # Viewing file content
prw-rw-r--. 1 foo foo 0 Aug 24 01:41 pipe_file

Here, the first character of the first column is 'p', which means it is a pipe file. There are a lot of pipe files available in the /dev directory.

We can also create a named pipe using the mknod command with the p option:

$ mknod   named_pipe_file p
$ ls -l  named_pipe_file
prw-rw-r--. 1 foo foo 0 Aug 24 12:33 named_pipe_file

The following shell script demonstrates a reading message from a named pipe. The send.sh script creates a named pipe called named_pipe, if it doesn't exist, and then sends a message on it:

#!/bin/bash# Filename: send.sh
# Description: Script which sends message over pipe

pipe=/tmp/named_pipe

if [[ ! -p $pipe ]]
then
  mkfifo $pipe
fi

echo "Hello message from Sender">$pipe

The receive.sh script checks whether a named pipe with the name named_pipe exists, reads a message from a pipe, and displays on stdout:

#!/bin/bash
#Filename: receive.sh
# Description: Script receiving message from sender from pipe file

pipe=/tmp/named_pipe

if [[ ! -p $pipe ]]
then
  echo "Reader is not running"
fi

while read line
do
  echo "Message from Sender:"
  echo $line
done < $pipe

To execute it, run send.sh in a terminal and receive.sh in another terminal:

$ sh send.sh  # In first terminal
$ sh receive.sh  # In second terminal
Message from Sender:
Hello message from Sender

Socket file

A socket file is used to pass information from one application to another. For example, if Common UNIX Printing System (CUPS) daemon is running and my printing application wants to communicate with it, then my printing application will write a request to a socket file where CUPS daemon is listening for upcoming requests. Once a request is written to a socket file, the daemon will serve the request:

$ ls -l /run/cups/cups.sock  # Viewing socket file attributes
srw-rw-rw-. 1 root root 0 Aug 23 15:39 /run/cups/cups.sock

The first character in the first column is s, which means it is a socket file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.239.100