Monday, October 4, 2010

UNIX/Linux test

So, I've been asked at a UNIX test about block and character devices. I've done a lot of things on Linux, BSD, but this question was a complete stranger for me. And, if this is a complete stranger for somebody else, here's the answer taken directly from Wikipedia:

Character devices
Character special files or character devices relate to devices through which the system transmits data one character at a time. These device nodes often serve for stream communication with devices such as mice, keyboards, virtual terminals, and serial modems, and usually do not support random access to data. In most implementations, character devices use unbuffered input and output routines. The system reads each character from the device immediately or writes each character to the device immediately.
Block devices
Block special files or block devices correspond to devices through which the system moves data in the form of blocks. These device nodes often represent addressable devices such as hard disks, CD-ROM drives, or memory-regions.
Block devices often support random access and seeking, and generally use buffered input and output routines. The operating system allocates a data buffer to hold a single block each for input and output. When a program sends a request to read data from or to write data to the device, the system stores each character of that data in the appropriate buffer. When the buffer fills up, the appropriate operation takes place (data transfer) and the system clears the buffer.

Very simple answer, isn't it?!

[update]: Damn, looks like I confused another answer too...about file descriptors. Again, taken from Wikipedia...
In POSIX, a file descriptor is an integer, specifically of the C type int. There are 3 standard POSIX file descriptors which presumably every process (save perhaps a daemon) should expect to have:
0 Standard Input (stdin)
1 Standard Output (stdout)
2 Standard Error (stderr)
Generally, a file descriptor is an index for an entry in a kernel-resident data structure containing the details of all open files. In POSIX this data structure is called a file descriptor table, and each process has its own file descriptor table. The user application passes the abstract key to the kernel through a system call, and the kernel will access the file on behalf of the application, based on the key. The application itself cannot read or write the file descriptor table directly. In Unix-like systems, file descriptors can refer to files, directories, block or character devices (also called "special files"), sockets, FIFOs (also called named pipes), or unnamed pipes.

This one was a bit complicated to explain, but...still, quite simple.

No comments:

Post a Comment