Socket File and Character File

 

1. What is a Socket File?

✅ Definition:

A socket file is a special type of file used for inter-process communication (IPC) on the same machine (Unix domain sockets). It allows processes to exchange data, similar to network sockets, but without using the network stack.

📍Location & Format:

  • Usually found in /tmp, /var/run, etc.

  • Appears with s as the file type in ls -l output.

🔍 Example:

$ ls -l /var/run/docker.sock

srw-rw---- 1 root docker 0 Jun 23 09:12 /var/run/docker.sock


  • ssocket

  • Used by docker CLI to talk to the dockerd daemon.

🔧 Use Cases:

  • Communication between Nginx and PHP-FPM

  • Communication between system daemons and CLI tools

  • Docker: /var/run/docker.sock

📌 Create a Socket (manually):

# Python example using Unix domain socket

import socket

s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

s.bind("/tmp/mysocket")



🎛️ 2. What is a Character File (Character Device)?

✅ Definition:

A character file (or character device) represents a hardware device that can be accessed as a stream of characters (bytes)—one at a time.

It does not buffer large blocks of data like block devices (e.g., disks).

📍Location & Format:

  • Found in /dev/, like /dev/tty, /dev/null, /dev/zero

  • Appears with c as the file type in ls -l

🔍 Example:

$ ls -l /dev/tty

crw-rw-rw- 1 root root 5, 0 Jun 23 09:45 /dev/tty


  • ccharacter device

  • 5, 0 → major and minor numbers (used by kernel to identify driver)

🔧 Use Cases:

  • /dev/tty: Terminal

  • /dev/null: Discard all input

  • /dev/random, /dev/urandom: Random number generators


🆚 Socket File vs Character File – Quick Comparison

Feature

Socket File

Character File

File Type (ls -l)

s

c

Purpose

IPC (inter-process communication)

Interface to character devices

Communication

Between processes

Between user space and hardware

Examples

/var/run/docker.sock, /tmp/fpm.sock

/dev/null, /dev/tty, /dev/zero

Created by

Applications (via socket())

Kernel/device drivers


FAANG-Level Socket vs Character File Questions – Answer List

#

Question

Answer

1

What is the purpose of a Unix domain socket file?

To enable fast, local inter-process communication (IPC) between processes on the same machine using the file system.

2

How is a socket file different from a named pipe (FIFO)?

A socket file supports bi-directional communication, can handle multiple clients, and uses datagram/stream semantics. FIFO is one-way and simpler.

3

What is the output of ls -l for a socket file?

Begins with s, e.g., srw-rw---- 1 root docker ... /var/run/docker.sock.

4

What are real-world examples of applications using socket files?

docker (/var/run/docker.sock), Nginx + PHP-FPM, PostgreSQL, systemd services.

5

What does srw-rw---- mean for /var/run/docker.sock?

It’s a socket file (s) with read-write permissions for the owner (root) and group (docker); others have no access.

6

How do you check if a file is a socket file in a shell script?

Use: [[ -S /path/to/file ]] → returns true if it’s a socket.

7

What system call is used to create a socket file?

socket() to create, and bind() to bind it to a filesystem path.

8

Can a socket file be used for network communication across systems?

No. UNIX domain sockets are for local communication only. Use TCP/IP sockets for network communication.

9

What happens if you cat a character device like /dev/urandom?

It outputs a continuous stream of pseudo-random bytes. Might flood your terminal.

10

Why are character devices used for terminals and serial ports?

Because they transmit data one character at a time, matching how keyboards, serial lines, and screens work.

11

How does the kernel identify which device driver to use for a character file?

Using the major number (driver ID) and minor number (device ID) of the character device file.

12

Can character devices be memory-mapped like block devices?

Generally, no. Character devices don’t support memory-mapping unless the driver provides custom support.

13

What’s the difference between mknod and mkfifo?

mknod creates block/character/special files; mkfifo creates named pipes (FIFOs).

14

How do you simulate a character device in user space?

Use socat, FUSE, or pseudo-terminal interfaces like /dev/pts/*.

15

What are major and minor device numbers in /dev/?

Major: identifies the driver; Minor: identifies the specific device (e.g., crw-rw-rw- 1 root root 1, 3 /dev/null)

16

Is /dev/null a socket, pipe, or character file? Why?

It is a character file (c) that discards all input; it's a special device handled by the kernel.

17

Compare pipe(), fifo(), and socket() usage in IPC.

pipe() = unnamed one-way IPC; fifo() = named one-way IPC; socket() = full-duplex, allows complex and bidirectional communication (also over network).

18

What are risks of exposing /var/run/docker.sock to users?

Users can control Docker daemon—risking container escape, privilege escalation, or full root access.

19

Can sockets be accessed remotely like TCP ports?

Only network sockets can. Unix domain sockets (.sock files) are strictly local.

20

How are UNIX domain sockets more efficient than TCP sockets on localhost?

They avoid network stack overhead—faster data transfer, lower latency, and fewer context switches.




Distributed by Gooyaabi Templates | Designed by OddThemes