0

In Linux world, I learnt that there is corresponding file that user space program communicates with, in order to actually communicate with any hardware device , for example, below are the files for devices,

Hard disk: /dev/sd*

CD and DVD drives : /dev/sr*

PATA hard disks: /dev/hd*

Terminals: /dev/tt*, /dev/pts/* and /dev/tty

Serial ports: /dev/ttyS*

Parallel ports: /dev/lp0 and /dev/lp1

Audio devices: /dev/snd/*, /dev/dsp/, dev/audio and more


My understanding is,

In Linux world(unlike windows), apart from file storage, file system is also used for IO communication.

So, device driver for any corresponding hardware device actually populates the data in such files(say /dev/sd*) which is actually the data sent/received from/to device. This enables user space program to read/write from/to devices using these files. for example, below is the user space program using C,

int fd;
struct termios *oldser,*newser,*oldter,*newter;
oldser=(struct termios *)malloc(sizeof(struct termios));
newser=(struct termios *)malloc(sizeof(struct termios));
oldter=(struct termios *)malloc(sizeof(struct termios));
newter=(struct termios *)malloc(sizeof(struct termios));

fd=openSerial("/dev/ttyS1");
setSerial(fd,oldser,newser);
/* next stop echo and buffering for stdin */
tcgetattr(0,oldter);
tcgetattr(0,newter); /* get working stdtio */

Question:

When you say, Everything is file or process,

Does that mean my above understanding on performing IO using file in Linux kernel(or UNIX)? Please correct me.

closed as unclear what you're asking by Celada, jordanm, Michael Homer, jlliagre, JdeBP Feb 27 '17 at 0:00

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • I don't understand your question "Is this the usage of file for IO using Linux kernel", but I suspect that it's much too broad a question anyway, even if I did understand it. Accessing resources using a file-like API is just an interface, an API, used for some types of resources (and not others). What is it then that you want to know, exactly? – Celada Feb 26 '17 at 22:07
  • @Celada Query updated – overexchange Feb 26 '17 at 22:10
  • Thanks, but I'm afraid I still don't understand. It sounds like you might be asking "Can I/O performed using a file-like abstraction under Linux?" But that's not it since you obviously know this already: the whole lead-up to your question consists of several examples of exactly that. – Celada Feb 26 '17 at 22:14
  • @Celada My question is, how files are involved in IO? Am trying to confirm my understanding on that – overexchange Feb 26 '17 at 22:16
  • 1
    Is this the approach of performing IO using file in Linux kernel(or UNIX)?  can you explain what is this in your question? – saga Feb 26 '17 at 22:37
1

In Linux world, I learnt that there is corresponding file that user space program communicates with, in order to actually communicate with any hardware device

That's the Unix paradigm, but it is not always true in Linux (and while I lack familiarity with the original Unix I doubt it was true even there).

In Linux world(unlike windows), apart from file storage, file system is also used for IO communication.

Not really. For example, an anonymous pipe has no filesystem presence, and even with a named pipe the data itself does not pass through the filesystem.

So, device driver for any corresponding hardware device actually populates the data in such files(say /dev/sd*)

It doesn't populate the files, it just handles read/write/ioctl to the special file. The filesystem never sees these operations.

When you say, Everything is file or process, Is this the approach of performing IO using file in Linux kernel(or UNIX)?

Sort of. The paradigm is that instead of having separate functions to read and write from a disk, it should implement the file interface so that you can pass a disk into programs that can handle files. So, for example, you can hexdump the contents of a disk even though hexdump probably has no code specific to handling disks.

Not the answer you're looking for? Browse other questions tagged or ask your own question.