2022-06-04 13:00:04 +01:00
|
|
|
---
|
2022-09-06 13:26:44 +01:00
|
|
|
tags:
|
2024-06-16 18:15:03 +01:00
|
|
|
- computer-architecture
|
|
|
|
|
- Linux
|
2022-06-04 13:00:04 +01:00
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# Devices
|
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
Devices are hardware that require access to the CPU in order to function.
|
|
|
|
|
Devices can either be external and plugged-in or internal to the motherboard.
|
2025-12-19 18:38:12 +00:00
|
|
|
The most common type of device that you will work with are
|
|
|
|
|
[disks](./What_are_disks.md)
|
2022-06-04 13:00:04 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
Devices are files but they have some different capabilities than ordinaryq
|
|
|
|
|
files. There are two types: **block** and **stream**. Device files reside in the
|
|
|
|
|
`/dev/` directory.
|
2022-06-04 13:00:04 +01:00
|
|
|
|
|
|
|
|
Some of the most important device files are:
|
2022-09-06 13:26:44 +01:00
|
|
|
|
|
|
|
|
- `hda` : a harddisk on a port
|
|
|
|
|
- `sda` : a harddisk on another port
|
2022-06-04 13:00:04 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
> Device files are an interface to a driver which accesses the hardware. A
|
|
|
|
|
> driver is therefore part of the Linux kernel.
|
2022-06-04 13:00:04 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
So when you see `sda` listed for example, this isn't the harddisk itself, it is
|
|
|
|
|
a file that communicates with a driver that controls how the kernel can interact
|
|
|
|
|
with it. This is why when you add a new piece of hardware (such as a mouse for
|
|
|
|
|
example) you have to install drivers (typically provided by the manufacturer) so
|
|
|
|
|
that your kernel is able to interact with it and provide it with access to the
|
|
|
|
|
CPU.
|
2022-06-04 13:00:04 +01:00
|
|
|
|
2022-09-06 13:26:44 +01:00
|
|
|
## Listing devices
|
2022-06-04 13:30:04 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
The following `ls` within the `/dev/` directory shows my main harddrive
|
|
|
|
|
partitions:
|
2022-09-06 13:26:44 +01:00
|
|
|
|
2022-06-04 13:00:04 +01:00
|
|
|
```
|
|
|
|
|
brw-rw---- 1 root disk 259, 1 Jun 4 11:00 nvme0n1p1
|
|
|
|
|
brw-rw---- 1 root disk 259, 2 Jun 4 11:00 nvme0n1p2
|
|
|
|
|
brw-rw---- 1 root disk 259, 3 Jun 4 11:00 nvme0n1p3
|
2022-06-04 13:30:04 +01:00
|
|
|
```
|
2022-06-06 11:00:05 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
> Since device files are files we can interact with them using standard file
|
|
|
|
|
> programs like `ls` and `cat`.
|
2022-09-06 13:26:44 +01:00
|
|
|
|
2025-12-19 18:38:12 +00:00
|
|
|
The [mode](/zk/File_permissions_in_Linux.md) is different from ordinary files.
|
|
|
|
|
Each device file is prepended with `b, p, c, s` before the standard permissions.
|
|
|
|
|
These stand for the major types of devices: _block, character, pipe_ and
|
|
|
|
|
_socket_.
|
|
|
|
|
|
|
|
|
|
- block
|
|
|
|
|
- Block devices transfer data as large fixed-size blocks. These are the most
|
|
|
|
|
common device type and include harddrives and filesystems. As the data can
|
|
|
|
|
be split up into discrete blocks of data, this facilitates quick random
|
|
|
|
|
access from the kernel.
|
|
|
|
|
- character
|
|
|
|
|
- Character devices transfer data one character at a time.The data is not in
|
|
|
|
|
discrete chunks, it is a continuous stream of characters. An example of a
|
|
|
|
|
stream device file is a printer however many character devices (such as
|
|
|
|
|
`/dev/null`) are not physically connected to the machine.
|
|
|
|
|
- pipe
|
|
|
|
|
- Pipe devices allow two or more processes to communicate with each other.
|
|
|
|
|
They work in the same way as character devices (transferring data as a
|
|
|
|
|
stream) but instead of the output being sent to a device, it is sent to
|
|
|
|
|
another process.
|
|
|
|
|
- socket
|
|
|
|
|
- The same as pipe devices, facilitating communication between processes
|
|
|
|
|
however they can communicate with many processes at once, not just a single
|
|
|
|
|
process.
|
2022-06-04 13:30:04 +01:00
|
|
|
|
2022-06-06 20:00:04 +01:00
|
|
|
## /dev/null
|
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
`/dev/null` is a virtual device: it doesn't actually exist as a piece of
|
|
|
|
|
hardware on the system. It can be useful when
|
2025-12-19 18:38:12 +00:00
|
|
|
[bash scripting](Redirect_to_dev_null.md) as a place to direct output that you
|
|
|
|
|
don't care about, for example errors or verbose program read-outs.
|
2022-07-02 20:00:04 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
I like to think of `/dev/null` as being like earth in a circuit. It's an outlet
|
|
|
|
|
to safely dispose of things you are not interested in.
|