I.inode, soft/hard link
(i), 1, Inode
The file is stored on the hard disk, and the minimum storage unit of the hard disk is called "Sector". Each sector is stored 512 bytes (equivalent to 0.5KB).
When the operating system reads the hard disk "does not read each sector, so inefficient", but one-time continuous reading of multiple sectors, that is, one-time read a "block." This "block", composed of multiple sectors, is the smallest unit of file access.
The file data is stored in "blocks". Information about the creator of the file, the date the file was created, the size of the file, and so on, requires a region to store the file information. This area of stored file meta information is called Inode, and the Chinese name is "Index node".
2. Inode Content
The inode contains the meta-information for the file, which reads "All file information except the filename":
* Number of bytes in the file
* User ID of the owner of the file
* The group ID of the file
* file read, write, execute permissions
* File timestamp "CTime refers to the time when the inode was last changed; Mtime refers to the time when the contents of the file were last changed; Atime refers to the time the file was last opened"
* Number of links "How many filenames point to this inode"
* Location of File data block
4. Inode number
Each inode has a number, and the operating system uses Inode numbers to identify different files.
The Unix/linux system does not use file names, and inode numbers are used to identify files. For the system, the file name is just an alias or nickname for the inode number to easily identify. On the surface, the user opens the file by file name.
In fact, this process inside the system is divided into three steps: First, the system finds the inode number corresponding to the file name, and secondly, obtains the inode information through the inode number, and finally, according to the Inode information, finds the block of the file data and reads the data.
5. Catalog Files
In a unix/linux system, directory is also a file. Opening the directory is essentially opening the directory file.
The structure of the catalog file is very simple, which is a list of a series of catalog items (dirent). Each directory entry consists of two parts: the file name of the included file, and the inode number that corresponds to the file name.
The LS command lists only all file names in the directory file: ls/etc
The ls-i command lists the entire directory file, that is, the file name and inode number: Ls-i/etc
If you want to view the details of a file, you must access the Inode node based on the inode number and read the information.
ls-l command to list file details: ls-l/etc
(ii) soft/hard Links
I. Hard Links :
In Linux, multiple filenames point to the same index node , which is usually a hard connection .
The function of a hard link is to allow a file to have more than one valid pathname so that the user can establish a hard connection to the important file to prevent "accidental deletion" of the function.
Hard Link (an alias of the actual file), its role is to prevent the real file is mistakenly manipulated , after a hard link to a file, they alias each other , delete any of them, only delete the alias, the actual file will not be deleted. Because just aliases do not have any additional information, they do not occupy the original file size of disk space.
"Hard links" can access the same content with different filenames, and changes to the contents of the file affect all file names, but deleting a file name does not affect access to another file name.
After running, the source file is the same as the inode number of the destination file, pointing to the same inode.
In the inode information, there is a "link count" that records the total number of filenames pointing to the inode (if the file is deleted, its value is reduced by one). When this value is reduced to 0, it indicates that no file name points to the Inode, at which point the inode number and its corresponding block region are reclaimed by the system.
hard links are exactly the same size and creation date as the real file, similar to copy, which synchronizes updates.
II. Soft Link :
Soft Links The file is a bit like a shortcut to Windows. It is actually a kind of special file. In a symbolic connection , a file is actually a text file that contains location information for another file. Soft link is another file, function can be understood as a pointer, action on this file operation in addition to the deletion are directly to the actual point to the file, because it is a real file, so occupy disk space.
Soft links similar to Windows shortcuts,-> represents the address of a real file (the source file is accessed, synchronized with the source file)
Iii. Differences and linkages:
(1) soft links can cross file system, hard link can not .
(2) Inode. Hard Links No matter how many, they point to the same Inode , it will increase the number of inode links, as long as the inode link number is not 0, the file exists . When you modify the source file or any one of the linked files, the other files are modified synchronously. Because the hard links are created, the files are aliases, and they do not occupy disk space.
Soft Links instead of using the inode number directly as a file pointer, use the File path name as a pointer (the soft link is not the same as the inode of the source file). So deleting a linked file has no effect on the source file, but deleting the source file will not find the file you want to point to. The soft Link has its own inode, and there is a small space on the disk to store the path name .
(3) A soft link can be linked to a nonexistent file name .
(4) soft links can be linked to the directory .
1): hard link and copy :
The copy is different from the date the real file was created.
2): Synchronous update of hard links
A soft connection is similar to a shortcut, which accesses the source file, and all must be synchronized with the source file.
A hard link is similar to a copy, but it has the ability to synchronize updates.
"The hard link file and the source file I node number is the same, and the kernel is based on the file's I node to identify the file, the two files of the I node is the same, all is considered a file, all simultaneously write and modify data".
3): impact on hard links and soft connections after deleting source files
A soft connection is similar to a shortcut , and it is not valid when the source file does not exist.
The hard link belongs to the copy , and still exists.
4): requirements for hard links
Hard links cannot span file systems, that is, cannot span partitions, similar to the hard links in windows that do not place the C drive on the D drive. And a soft link can be placed on any file system.
Second,Fork () and vfork () -----"Create process"
Vfork (): Child process shared data segment with parent process (the child process does not call Exec and exit before it is equivalent to thread concept, at which time the parent process blocks waiting)
Vfork (): Guarantees that the child process runs first and is shared with the parent process data before calling exec or exit, and that the parent process may be scheduled to run after it calls exec or exit, after which the order is unrestricted. If the child process relies on further actions of the parent process before invoking these two functions, it causes a deadlock.
Vfork (): No need to create virtual address space
[Copy-on-write technology: The kernel creates virtual space for newly generated child processes that replicate in the virtual space of the parent process, but does not allocate physical space for those segments, they share the physical space of the parent process, and when the parent-child process has write memory, the corresponding segment of the child process is allocated physical space. ]
4. The "Same" is called once, but returns two times. The only difference between the two returns is that the return value of the child process is 0, and the return value of the parent process is the process ID of the new child process.
Return value: Child process--0, parent process--Child process ID, error---1
1). Exit is used to end the entire running program, return parameters (the general return value is 0 for normal exit, not 0 for abnormal exit), give control to the operating system;
Return is to exit the current function, return the function value, and give control to the calling function.
2). Exit is the system call level, which represents the end of a process;
Return is the language level that represents the return of the call stack.
3). At the end of the main function, the Exit function is called implicitly, so the general program executes to the end of main () to end the main process.
Exit deletes the memory space used by the process and returns the error message to the parent process.
4). Calling return and exit in a non-main function is obvious, but in the main function it is consistent to call return and exit in most cases.
This article is from the "Flower Open Shore" blog, please be sure to keep this source http://zxtong.blog.51cto.com/10697148/1761249
Inode, Soft (hard) links and fork and vfork