Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do dev files work?

Tags:

c

file

linux

udev

How guys from linux make /dev files. You can write to them and immediately they're erased. I can imagine some program which constantly read some dev file:

FILE *fp;
char buffer[255];
int result;
fp = fopen(fileName, "r");
if (!fp) {
    printf("Open file error");
    return;
}
while (1)
{

    result = fscanf(fp, "%254c", buffer);
    printf("%s", buffer);
    memset(buffer, 0, 255);
    fflush(stdout);
    sleep(1);
}
fclose(fp);

But how to delete content in there? Closing a file and opening them once again in "w" mode is not the way how they done it, because you can do i.e. cat > /dev/tty

like image 612
badeleux Avatar asked Dec 09 '22 11:12

badeleux


2 Answers

What are files? Files are names in a directory structure which denote objects. When you open a file like /home/joe/foo.txt, the operating system creates an object in memory representing that file (or finds an existing one, if the file is already open), binds a descriptor to it which is returned and then operations on that file descriptor (like read and write) are directed, through the object, into file system code which manipulates the file's representation on disk.

Device entries are also names in the directory structure. When you open some /dev/foo, the operating system creates an in-memory object representing the device, or finds an existing one (in which case there may be an error if the device does not support multiple opens!). If successful, it binds a new file descriptor to the device obejct and returns that descriptor to your program. The object is configured in such a way that the operations like read and write on the descriptor are directed to call into the specific device driver for device foo, and correspond to doing some kind of I/O with that device.

Such entries in /dev/ are not files; a better name for them is "device nodes" (a justification for which is the name of the mknod command). Only when programmers and sysadmins are speaking very loosely do they call them "device files".

When you do cat > /dev/tty, there isn't anything which is "erasing" data "on the other end". Well, not exactly. Basically, cat is calling write on a descriptor, and this results in a chain of function calls which ends up somewhere in the kernel's tty subsystem. The data is handed off to a tty driver which will send the data into a serial port, or socket, or into a console device which paints characters on the screen or whatever. Virtual terminals like xterm use a pair of devices: a master and slave pseudo-tty. If a tty is connected to a pseudo-tty device, then cat > /dev/tty writes go through a kind of "trombone": they bubble up on the master side of the pseudo-tty, where in fact there is a while (1) loop in some user-space C program receiving the bytes, like from a pipe. That program is xterm (or whatever); it removes the data and draws the characters in its window, scrolls the window, etc.

like image 70
Kaz Avatar answered Dec 10 '22 23:12

Kaz


Unix is designed so that devices (tty, printer, etc) are accessed like everything else (as a file) so the files in /dev are special pseudo files that represent the device within the file-system.

You don't want to delete the contents of such a device file, and honestly it could be dangerous for your system if you write to them willy-nilly without understanding exactly what you are doing.

like image 39
X Tian Avatar answered Dec 10 '22 23:12

X Tian