Easy explanation of . I/O system calls & File Descriptor in C language

Suhulla
5 min readJul 24, 2024

--

Part 1:

The first thing that you need to know about system calls I/O (I = input / O = output) is that there are several system calls in C. I will name them then give you the explanation.

What its a I/O System Calls.

I will give you two explanations: one for the geeks and one for beginners like you and me.

Geek explanation:

System calls are mechanisms that enable a program to interact with the operating system kernel to access system resources that are not directly accessible to the program. These system calls allow the program to request services from the kernel, such as access to input/output devices (I/O) like monitors and keyboards. Programming languages like C provide specific functions for I/O system calls, such as create, open, read, write, etc., which enable the program to communicate with the kernel to access these resources. 🥱😮‍💨🤕🤒

Easy Suhulla explanation:

Ok, imagine that you want to create a program that writes “the geek explanation is useless” in another file that you created and called “myfile.txt”. So your program can’t just write that all at once into that specific file. It needs specific tools to help it; otherwise, it can’t. This is where I/O system calls come in.

System calls are like requests that your program makes to the operating system(Windows, macOS, Linux) to do something it can’t do alone.

So basically this is the process:

etc…

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Part 2:

Now we understand that if our program wants to do a specific task like write, read, or close, it needs to call some friends to help it. But how do the system calls work? Do it need help too? What are the tools that it use?

Calm down, all your questions will be answered.

One of the things that call system like write, read, and close need is the file descriptor (FD). I haven’t mentioned open because the file descriptor (FD) is something provided by open for the process to work. Don’t worry, you’ll understand everything.

Let me give you the geek explanation and the Suhulla explanation:

Geek explanation:
A file descriptor (FD) is an integer value that uniquely identifies an open file within a process. The file descriptor table is a data structure that maps these integer values to file table entries, which are in-memory representations of open files. Each process has its own file descriptor table, which is used to manage the files that are currently open within that process. The file table entries contain meta data about the open file, such as the current file position, and are used to facilitate I/O operations. 🤢😵🫣

Easy Suhulla explanation:
Imagine that you created 100 files and now you want to write in a specific file that you named “my_file.txt”. Like we explained earlier, you will need a “system call” like the function write to do this. Here is the prototype of this function write:

Don’t focus on “const void buf[.count]” & “size_t count” because these are variables that are specific to the function write and this is not our subject in this article. If you want to know more about each function like write, read, close, open, go read the articles I wrote on them. But here we will only focus on the explanation of system calls, so I want us to concentrate on “int fd” (file descriptor (FD)) because it’s an important variable that you will find in system calls:

So as we were saying, if you want to write in one of those 100 files that you created, you will need something to identify that file, and this is where the fd comes in because the fd is the identification number of that file, allowing us to identify it more easily. Each file has an fd that is generated when we call the open() function.

As they say, a picture is worth a thousand words, so here’s how it works:

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Conclusion:

You now have a clear idea of what system I/O calls and file descriptors are in C. I hope this article has helped you understand these concepts in a simple and easy-to-grasp way.

If you have any questions, don’t hesitate to write them in the comments or send them to me by email: suhulla@proton.me , and I’ll be happy to respond. Remember, if you ask a question, you might look silly for a moment, but if you don’t ask, you’ll remain silly for life!

If you want to understand each I/O call system go read mes autres articles:

--

--

Suhulla
0 Followers

Our explanations adapt to your understanding.