Implementation of multiple files simultaneously operating a file in Linux

  
 

How recently did the system handle this issue when a customer asked about how different users were working on the same file. This article will briefly introduce the process of multiple processes under Linux "and at the same time" operation of the same file. Suppose there are three processes A, B, and C that operate on the file a.txt file. A process should read the first to 100 characters of the file, process B should read the line containing the aa character in the file, and C should write to the a.txt file ”ABC”. The starting sequence of the three processes is A, B, and C.

[正文]

A hypothetical condition

1.1 Three processes
1, Suppose there are three processes A, B, C operate on the file a.txt file 2, A process to read the first 1 to 100 characters of the file; 3, process B to read the line containing the aa character in the file; 4, C to write to a.txt file & rdquo; ABC & rdquo;; H2>1.2 Process startup sequence
1. The startup sequence of the three processes is A, B, C;

Two processes

2.1 A process startup
The code execution steps of process A are as follows:

  1. Open file a.txt
  2. Read the contents of the file corresponding to the condition

    When process A starts, when executing the file a.txt” The management table (file_table[64]) associates the pointer table of the process A with the i-node of the a.txt file, and gives the process A the authority and capability to operate the file a.txt. When executing ” reading the file content of the corresponding condition, the system will load the file a.txt from the hard disk into the buffer. In this process, the kernel corresponding function will apply for a buffer space of "idle", and load the file a.txt from the hard disk into the buffer space. Since the loading takes time, the buffer space is locked. Process A must wait for the load to complete before reading the contents of the file. At this point, process A is suspended, set to ” uninterruptible wait state & rdquo;, the buffer process waits for the queue leader to be process A.

    2.2 B process startup
    Process B code execution steps are as follows:

    1. Open file a.txt
    2. Read the contents of the corresponding condition

      When B After the process starts, execute ” open the file a.txt”. Similarly, the file management table (file_table[64]) associates the pointer table of process B with the i-node of the a.txt file, giving the process B the authority and capability to manipulate the file a.txt. It is important to note that since different processes operate on files differently, this process must be done again. However, since the i node of the file a.txt has been found when the A process is executed, the process B does not need to perform the i node that traverses the i node to find a.txt. When executing ” reading the file content of the corresponding condition, the system finds that the data buffer to be loaded already exists through the hash table management structure, so the data is no longer loaded into the buffer from the hard disk. Instead, the number of references to the buffer is increased by one and the total is 2. (Note that because the three processes are ” at the same time & rdquo; operation on a file, the time is very short, at this time the file a.txt data is still in the process of loading the buffer from the hard disk) At this time, process B must also wait The contents of the file can only be read after the loading is completed. Thus, process B is also suspended, set to ” uninterruptible wait state, the buffer process waits for the queue head to be process B, followed by process A.

      2.3 C process startup
      Process C code execution steps are as follows:

      1. Open file a.txt
      2. Write the corresponding file contents

        When C After startup, execute ”Open the file a.txt”. Similarly, the file management table (file_table[64]) associates the pointer table of process C with the i-node of the a.txt file, giving the process C the authority and capability to manipulate the file a.txt. Like the B process, the C process goes through the same process and enters the "uninterruptible wait state", and is located at the head of the queue in this buffer queue, followed by processes B, A, respectively.

        2.4 Process wakeup
        At this time, processes A, B, and C are in a state of waiting for the buffer to be unlocked after the data is loaded, that is, the suspended state. After the data is loaded, the hard disk generation & rdquo; data transmission is completed & interrupt; the system informs that the system data has been loaded. After the system receives the interrupt, it unlocks the buffer and wakes up the processes in the queue. At this point, the system will wake up process C at the beginning of the team and set the C process to the ready state. Then start executing the "Write Data" code, start timing, and set the B process to the ready state. When the timing is over, the C process is completed and exits. The B process starts executing ” reads the data & rdquo; code, starts timing, and sets the A process to the ready state. When the timing is over, the B process is completed and exits. The A process starts executing ”read the code”, starts the model timing, and sets the queue to ”NULL”. When the timing is over, the A process is completed and exits. At this point, all three processes are completed.

        Three Summary
        From the above process, we can see that when three processes operate on one file at the same time, although the startup order of the process is A, B, C, but finally when the data is loaded into the buffer After the zone is completed, the execution order is based on the queue head of the waiting queue to the end of the team, namely C, B, and A. After the execution of the ABC three processes, the data modification is actually only in the buffer, and is not immediately written to the hard disk, but the buffer data is synchronized to the hard disk through the subsequent update process. Due to the limited space, we will not introduce them here. The readers are interested in researching on their own.

Copyright © Windows knowledge All Rights Reserved