File descriptors

What are File descriptors?

UNIX, the ancestor of Linux, championed a file-centric approach( If everything behaves like a file, you can use the same set of operations — read, write, close — on a wide range of resources, be it an actual file, a network socket, or even hardware devices), treating everything as a file for uniformity. File descriptors, basic non-negative integers, served as handles for diverse resources.

Example:

Let's illustrate this concept with an example. Suppose you want to read a file named "coolStuff.txt." The basic steps would involve:

Open the file: This action provides you with a file descriptor (let's say fd = 3).

Read or Write: Utilize the file descriptor (fd) to perform read or write operations on the data.

Close: Finally, close the file using the file descriptor (fd).

The Purpose: Why Do We Need File Descriptors?

File descriptors serve as this communication channel between the user-space applications and the kernel-space system calls for I/O operations. They create a neat, orderly way to deal with the chaos of data streams.

System calls:?

System call is a way of asking the kernel to do some work for us. For example, these are the four basic system calls that help to work with files in Linux

  • open — Hey kernel, could you open a file for me? So that the process could do something with it.
  • read — Hey kernel, could you read this file saved in ~/new/stuff/file_descriptors.js?
  • write — Hey kernel, could you help me write about "File descriptors" to this file?
  • close — Hey kernel, I am done, please close the file.


Delving into the world of file descriptors sparks curiosity about the intricate process through which programs use them to interact with files. This intriguing exploration merits further discussion in an upcoming post!

Brajinder Deep

General Electric (GE) | Software Engineer

12 个月

Yes, this is something! Knowledgeable

要查看或添加评论,请登录

社区洞察

其他会员也浏览了