How to implement file sharing for Linux common file I/O operations

Source: Internet
Author: User
Tags function prototype



1. Three ways to implement file sharing

1. What is file sharing:    

(1) file sharing is the same file (the same file refers to the same inode, the same pathname) by a plurality of independent read-write (almost understood as multiple file descriptors) to go at the same time (one open has not been closed while another to operate) operation.

(2) There are many meanings of file sharing: For example, we can use file sharing to achieve multi-threaded simultaneous operation of the same large file, to reduce file read and write time, improve efficiency.

2, the core of file sharing is how to get out multiple file descriptors point to the same file.

3, common three kinds of file sharing situation:

1. The same file is opened using the open function multiple times in the same process.

2, the different process to open the same file (because two FD is not in the consent process, so two FD may be the same or different).

3. The Linux provided DUP and dup2 two APIs to let the process assign a file descriptor.

4, we analyze the core focus of file sharing is: Write, read or continuous reading and writing

Add:

Re-discussion of file descriptors:

1, the essence of the file descriptor is a number, which is essentially a table of the file descriptor in the Process table item, the process through the file descriptor as index to look up the table to get the file table pointer, and then indirectly access the file table corresponding to it.

2, file descriptor This number is open system call internal by the operating system automatically assigned, operating system assigned this FD is not randomly allocated, but also according to certain rules.

3, operating system rules, FD starting from 0 an increase, FD also has the largest limit, In earlier versions of Linux, FD Max was only 20.linux in the File descriptor table is an array (not a linked list), so this file descriptor is actually an array, FD is index, the file table pointer is value

4. When we open, the kernel picks up a minimal unused number from the file descriptor table to return to us.

Copy of the file descriptor:

5, FD 0, 1, 2 has been used by the system by default, when we run a program to get a process, the internal default has been opened 3 files, the three files corresponding to the FD is 0, 1, 2. These three files are called stdin, stdout, stderr respectively. Standard input, standard output, standard error. So the minimum FD that the user process gets is 3.

6, the standard input is generally corresponding to the keyboard (can be understood as: 0 This FD corresponds to the keyboard device files), the standard output is generally LCD display (can be understood as: 1 for LCD device files)

7, the printf function is actually the default output to the standard output stdout. Stdio also has a function called fpirntf, which allows you to specify which file descriptor to output to.

2. How to implement file sharing

     1, Use the open function multiple times in the same process to open the same file . Open the same file at the same time is not difficult to understand, the key is to open the same file, we read to write the file, is to write or continue to write, a simple understanding is, covered with writing it? Or a finished, file pointer automatically moved to the back to write separately? We do not have to guess, to remember other people's theory, we directly use code testing, as long as the code is the correct situation test results are theory. Let's come down to the test code:

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include < sys/stat.h> #include <errno.h> #include <fcntl.h> #include <string.h>typedef int file _t;   #define  maxleng 1024int main (int argc,char *argv[]) {    file_t  fd1 = -1, fd2 = -1;     fd1 =  Open ("./1.txt", o_rdwr | o_trunc | o_creat,0644);    if (fd1 < 0)    {   perror ("FD1&NBSP;:OPEN&NBSP;FILE&NBSP;FAILED&NBSP;:");    _ Exit ( -1);    }   fprintf (stdout, "fd1 = %d\n", FD1);       //here in order to distinguish more clearly, fd2 open and judgment can be placed in the judgment of Fd1 to    fd2 = open ("./1.txt", O_ rdwr | o_trunc | o_creat,0644);    if (fd2 < 0)     {  &nbsP;perror ("FD2:OPEN&NBSP;FILE&NBSP;FIALED&NBSP;:");    _exit ( -1);    }    fprintf (stdout, "fd2 = %d\n", FD2);    while (1)    {      write (FD1, "AAAA", 4);    sleep (1);    write (FD2, "BBBB", 4);    }      return 0;}

When this code is finished, let's look at the results.

[Email protected]_k filetest]# Cat 1.txt bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb Bbbbbbbbbbbbbbbaaaa[[email Protected]_k filetest]#

So we can easily see that although we are writing with two file pointers, FD1 is only written in the last time, right? That's what we see, but why is that? Here we analyze: In fact, Fd1 did write operations, but we fd1 and fd2 two file pointers are different file pointers, when the FD1 is written, the file pointer moves backwards, but FD2 does not move, when the FD2 write will overwrite the fd1 write content, And here is about two times you open file properties, use

O_rdwr | O_trunc | O_creat Three properties Open, we can also test the results of other properties. Although the results of different properties may result in different results, we can be certain here that one file pointer moves without causing another file pointer to move .

2 . The second method opens the same file using the Open function in different processes (because two FD is not in the consent process at this time, so two FD may be the same or different). The fork function is not summarized here to produce a new process. So do not summarize, Bo master behind add up.


3. Use the DUP provided by Linux and the Dup2 two API to let the process assign a value to the file descriptor.

1. using DUP for file descriptor replication

(1) The DUP system invokes a copy of FD, which returns a new file descriptor (for example, the original FD is 3 and returns 4)

(2) The DUP system call has a feature, is that it can not specify the number of FD obtained after replication, but is automatically assigned by the internal operating system, the principle of allocation of the principle of FD to abide by the principles.

(3) The FD returned by the DUP and the original OLDFD all point to OLDFD open the dynamic file, the operation of the two FD actually operation is OLDFD open the file. Actually constitutes a file share.

(4) When the FD returned by the DUP and the original OLDFD are written to a file at the same time, will the result be written separately or consecutively?

Defect analysis using the DUP

(1) DUP does not specify the number of new file descriptors assigned, DUP2 system call fixed this flaw, so the actual use of the project according to the specific circumstances to determine the use of DUP or dup2.

2. Using dup2 for file descriptor copying

(1) The role of Dup2 and DUP is the same, all copying a new file descriptor. However, DUP2 allows the user to specify a number for the new file descriptor.

(2) Use the method to see the man manual function prototype can be.

Test shared file operations using DUP and DUP2:

1, the implementation of the DUP shared file

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include < sys/stat.h> #include <fcntl.h> #include <string.h> #include <errno.h>typedef int file_t; #define MAXSIZE 1024int Main (int Argc,char *argv[]) {file_t fd1 =-1, fd2 = -1;FD1 = Open ("./3.txt", O_RDWR | O_creat | o_trunc,0644); if (Fd1 < 0) {perror ("Open Fiel Error:"); _exit (-1);} fprintf (stdout, "FD1 =%d\n", fd1), fd2 = DUP (FD1), if ( -1 = = fd2) {perror ("DUP error:"); _exit (-1);} fprintf (stdout, "FD2 =%d\n", fd2), while (1) {Write (fd1, "AAAA", 4), sleep (1); Write (FD2, "BBBB", 4);} return 0;}

We continue to look at the results:

[Email protected]_k filetest]# Cat 3.txt Aaaabbbbaaaabbbbaaaabbbbaaaabbbbaaaabbbbaaaabbbbaaaabbbbaaaabbbbaaaabbbbaaaabbbbaaaabbbbaaaabbbbaaaabbbbaaaabbbbaaaabbbba Aaabbbbaaaa[[email Protected]_k filetest]#

From the results we can find that A and B are alternating, which is expected, although the DUP produced two file descriptors, but shared the same file pointer, when a write, the file pointer has moved to the back, nature will not be overwritten.


2. dup2 shared file cross-write test

1, for Dup2 and DUP write a file, the difference is that dup2 can specify the file descriptor itself, so here does not give dup2 to write an instance of the file.

2, when the cross-write, the result is the continuation of writing

3. Fcntl function

1, fcntl function is a multi-function file Management toolbox, can accept 2 parameters + 1 variables, the first parameter is fd means yo ah operation that file, the second parameter is a cmd means to do the command operation. Arguments are used to pass parameters, to be used with the cmd command

2, the cmd looks similar to the f_xxx, the different cmd has the different function. When learning, there is no need to get all the meaning of the cmd clear, just need to make a clear on the line, the other man manual can be checked.

3, Common fcntl of CMD   

1, F_DUPFD the function of this cmd is to copy the file descriptor (function similar to DUP and dup2), the function of this command is to find a list of available FD numbers than arg large or as large as arg as a copy of the OLDFD FD, and dup2 a bit like but different. DUP2 returns the NEWFD that we specified, otherwise it will go wrong, but the F_DUPFD command returns the smallest number of >=arg.

4. Standard I/O Library

1. What is the difference between standard io and file IO

1, the biggest difference: the standard I/O library is the C library function, and the file I/O is the Linux system call API

2, the C language library function is encapsulated by the API, library functions are also called by the API to complete the operation, but the library function more than a layer of encapsulation, so more useful than the API.

3, the library function than the API has an advantage, the API in different operating system interface is not common, but the C library function is almost the same on different operating systems, so C library functions are portable and the API is not portable.

2, Common standard IO function introduction:

Common standard IO Library functions: fopen fclose fwrite fread ffulsh, fseek

The above documents, I learn to refer to different teachers of the documents themselves summed up, the code is I personally test. Please correct me if there is a problem with that piece of documentation.

This article is from the "12162969" blog, please be sure to keep this source http://12172969.blog.51cto.com/12162969/1959060

How to implement file sharing for Linux common file I/O operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.