
Click the blue font above to follow us
In Linux, file sharing refers to the ability of multiple processes to access and manipulate the same file simultaneously.
File sharing is significant in multi-process or multi-threaded programming environments, especially in the following aspects:
-
Multi-threading Large Files: File sharing can be used to enable multiple threads to operate on the same large file simultaneously. By creating multiple different file descriptors, each thread can read or write to the file in parallel, reducing file read/write times and improving overall efficiency.
-
Increased Concurrency: File sharing provides a mechanism that allows multiple processes or threads to concurrently access the same file. This is beneficial for applications that frequently access files, enabling better utilization of system resources, and improving concurrency and responsiveness.
-
File Descriptor Duplication: By calling the open function multiple times or using the dup() and dup2() functions, multiple different file descriptors can be created that point to the same file. This provides independent file access channels for multiple concurrent operations, ensuring they do not interfere with each other.
-
Cooperative Operations: File sharing also involves mechanisms like file locking to ensure that operations on the file are conducted cooperatively during concurrent access. This helps avoid data inconsistencies and conflicts, improving program stability.
Below, we share three common methods of file sharing.
1
Calling the open function multiple times in the same process to open the same file
Calling the open function multiple times in the same process to open the same file will yield multiple different file descriptors (File Descriptor, abbreviated as FD). Each call to open returns a new file descriptor, which can be independently used for reading, writing, and other operations on the file.
The relationship between various data structures is illustrated in the diagram below:
Here is a simple example demonstrating opening the same file multiple times in the same process:
#include
#include
#include
int main(void) {
// Open the same file to get file descriptors
int fd1 = open("example.txt", O_RDONLY);
int fd2 = open("example.txt", O_RDONLY);
if (fd1 == -1 || fd2 == -1) {
perror("Error opening file");
return 1;
}
// Read file content
char buffer1[100];
char buffer2[100];
ssize_t bytesRead1 = read(fd1, buffer1, sizeof(buffer1));
ssize_t bytesRead2 = read(fd2, buffer2, sizeof(buffer2));
// Output the read content
printf("Content read from fd1: %.*s\n", (int)bytesRead1, buffer1);
printf("Content read from fd2: %.*s\n", (int)bytesRead2, buffer2);
// Close file descriptors
close(fd1);
close(fd2);
return 0;
}
In this example, the program opens the same file “example.txt” twice, obtaining two file descriptors fd1 and fd2. These file descriptors can perform file reading operations independently. It is important to note that the contents read in this case are the same, as they point to the same position in the same file.
2
Using the open function in different processes to open the same file
In Linux systems, different processes can use the open function to open the same file. When multiple processes open the same file, each process will receive a file descriptor (file descriptor), which is a unique integer used to identify that file’s open instance in that process.
The relationship between various data structures is illustrated in the diagram below:
Below is a simple example demonstrating two different processes using the open function to open the same file:
#include
#include
#include
#include
int main() {
// File path
const char *file_path = "example.txt";
// Open the file in the first process
int fd1 = open(file_path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (fd1 == -1) {
perror("Error opening file in process 1");
exit(EXIT_FAILURE);
}
// Write content to the file
const char *content1 = "Hello from process 1\n";
write(fd1, content1, strlen(content1));
// Close file descriptor
close(fd1);
// Open the file in the second process
int fd2 = open(file_path, O_WRONLY | O_APPEND);
if (fd2 == -1) {
perror("Error opening file in process 2");
exit(EXIT_FAILURE);
}
// Write content to the file
const char *content2 = "Hello from process 2\n";
write(fd2, content2, strlen(content2));
// Close file descriptor
close(fd2);
return 0;
}
In this example, two processes use the open function to open the same file example.txt. The first process opens the file in write mode, writes some content, and then closes the file. The second process opens the file in append mode, writes some content, and then closes the file. Since file descriptors are private to each process, they can independently access and manipulate the same file without interfering with each other.
3
Using the dup (dup2) function to duplicate file descriptors in the same process
In the same process, the dup function or dup2 function can be used to duplicate file descriptors. This allows two file descriptors within the process to point to the same opened file, permitting independent read and write operations on the same file.
The relationship between various data structures is illustrated in the diagram below:
Here is a simple example:
#include
#include
#include
#include
int main() {
// File path
const char *file_path = "example.txt";
// Open the file
int fd1 = open(file_path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (fd1 == -1) {
perror("Error opening file");
exit(EXIT_FAILURE);
}
// Write content to the file
const char *content = "Hello from process\n";
write(fd1, content, strlen(content));
// Duplicate file descriptor
int fd2 = dup(fd1);
if (fd2 == -1) {
perror("Error duplicating file descriptor");
close(fd1);
exit(EXIT_FAILURE);
}
// Use original file descriptor to write content
const char *content1 = "Original file descriptor\n";
write(fd1, content1, strlen(content1));
// Use duplicated file descriptor to write content
const char *content2 = "Duplicated file descriptor\n";
write(fd2, content2, strlen(content2));
// Close file descriptors
close(fd1);
close(fd2);
return 0;
}
In this example, the program first opens a file, then uses the dup function to duplicate the file descriptor. Thus, fd1 and fd2 both point to the same file. The program then writes some content using the original file descriptor fd1, and writes some other content using the duplicated file descriptor fd2. Since they point to the same file, both pieces of content will appear in the file.
It is important to note that the dup function returns a new file descriptor, which is the smallest available file descriptor among the current available file descriptors. The dup2 function allows specifying the value of the new file descriptor; if the specified file descriptor is already in use, dup2 will close that descriptor first, then redirect it to the specified file.

-
Explanation of UDS Diagnosis Time Parameters
-
Why Terminal Resistors are Needed in CAN Bus Networks?
-
Detailed Explanation of CAN Bus: Characteristics of High-Speed and Low-Speed CAN Buses
-
Industrial High-Precision Electromagnetic Flow Meter Solutions
-
LabVIEW Texture Analysis (Basics – Part 9)
