Master Linux Basic I/O: Files, Descriptors, System Calls and Libraries
This guide explains Linux file I/O fundamentals, covering file concepts, standard streams, system calls like open and dup2, file descriptor allocation, redirection, the FILE structure, inode layout, hard and soft links, and the creation and usage of static and dynamic libraries, all illustrated with code snippets and diagrams.
File concepts and basic I/O
In Linux, everything is treated as a file, including disks, keyboards, and network interfaces. A file consists of metadata (inode) and content. Even an empty 0 KB file occupies disk space. File operations are essentially operations on the file’s content or its attributes.
Standard streams
Every C program starts with three standard streams: stdin (keyboard, read), stdout (display, write) and stderr (error output, write). These are file descriptors 0, 1, 2 and are opened by the OS for each process.
Common I/O functions
int fputs(const char *s, FILE *stream);– writes a null‑terminated string to the given stream. char *fgets(char *str, int num, FILE *stream); – reads up to num‑1 characters or until a newline/EOT, stores them in str, and appends a terminating NUL.
size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream);– writes size*count bytes from ptr to stream and returns the number of items written.
System calls and file descriptors
The low‑level interface uses system calls such as open, read, write, close, and lseek. The open prototype is:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);Typical flags include O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_APPEND. On success open returns a new file descriptor (the smallest unused integer in the process’s files_struct array).
File descriptor allocation
The kernel maintains an array of pointers to struct file objects; the index in this array is the file descriptor. Descriptors 0, 1, 2 are pre‑allocated for the standard streams.
Redirection and dup2
Redirection changes which file descriptor a stream points to. The dup2(oldfd, newfd) system call copies the entry of oldfd to newfd in the descriptor table.
#include <unistd.h>
int dup2(int oldfd, int newfd);The FILE abstraction
The C library wraps a file descriptor inside a FILE structure (defined in stdio.h). Important members include _fileno (the underlying descriptor) and various buffer pointers. Functions such as fopen, fread, fwrite, fclose operate on this structure, ultimately invoking the corresponding system calls.
Linux file system layout
A Linux file system is organized into block groups. Each group contains a superblock, a group‑descriptor table, block bitmap, inode bitmap, inode table, and data blocks. An inode stores all attributes of a file (size, timestamps, permissions) but not its name; the directory maps names to inode numbers.
Inode and timestamps (ACM)
Access (A) – last read time.
Modify (M) – last content change.
Change (C) – last metadata change.
Hard and soft links
Hard links create additional directory entries that point to the same inode; they cannot cross file‑system boundaries. Soft (symbolic) links are independent files containing a pathname to the target; they can span file systems but become dangling if the target is removed.
Static and dynamic libraries
Static libraries ( .a) are archives of object files linked into the executable at compile time. Dynamic libraries ( .so) are linked at runtime, allowing multiple programs to share the same code in memory.
Creating a static library
# gcc -c add.c -o add.o
# gcc -c sub.c -o sub.o
# ar -rc libmymath.a add.o sub.o
# gcc main.c -L. -lmymath -o mainOptions: -I adds header search paths, -L adds library search paths, -l specifies the library name without the lib prefix.
Creating a shared library
# gcc -fPIC -c foo.c -o foo.o
# gcc -shared -o libfoo.so foo.oShared libraries follow the naming convention lib<name>.so. They must be placed in a directory listed in /etc/ld.so.conf or referenced via LD_LIBRARY_PATH, then refreshed with ldconfig.
Running a program with a shared library
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/lib
ldconfig
./myprogramSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
