File programming in Linux (1)

Source: Internet
Author: User
Tags temporary file storage

Basic file programming knowledge in Linux

I used to read a friend's blog with a very sharp sentence: "All files under Linux". In just five words, I spoke about the importance of the file system in Linux. In the Linux operating system, all the resources are managed by the file.

I. Preface:

In the principle class, we know that one of the five major power supplies of the operating system is file management.

So why should we introduce the file management function? What is its main task?

Let's take a look at the background.

In modern operating systems, a large amount of programs and data need to be used. Due to the limited memory capacity and the inability to store the data for a long time, people have come up with the idea of storing the data in external storage as files, you can call them into the memory when necessary-the file system is available from now on. It manages stored files and provides users with access, sharing and protection measures. This facilitates users, ensures file security, and improves the utilization of system resources.

1. From the perspective of the system, the file system organizes and allocates the file storage space, stores and protects and searches stored files. From the user's perspective, the main purpose of the file system is to achieve access to files by name.

2. because a large number of files need to be stored, how can we effectively manage these files? ------> so people introduce directories to manage files.

---------------------------------------------------

Now let's take a look at how to implement the file system in Linux.

Ii. File System in Linux

A file system refers to the physical space of a file. in Linux, each partition is a file system with its own directory hierarchy. In Linux, the file systems that belong to different partitions form a general directory hierarchy of the system in a certain way.

In the principle course, we know that the system relies on FCB to manage files. In Linux, it relies on index node to manage files.

1. File System in Linux

1> Linux is a secure operating system designed based on files. From this point, we can see that "all files under Linux ". The file subsystem in Linux is mainly used to manage the allocation of file buckets, maintain file access permissions, and perform various file operations.

A. You can use shell commands to operate files, but the functions are limited.

B. programmers can operate files through system calls or C language library functions.

2> Linux The file mainly includes two aspects: first, the data contained in the file; second, the file attributes, also known as metadata, including the file access permission, owner, and file size, creation date.

3> A directory is also a file called a directory file. When the contents of a directory file, the directory items are related to the files and directories under the directory. When creating a new directory, the system automatically creates two directory items ..

We can use commands to experience its implementation:

Think @ Ubuntu :~ $ Mkdir Tiger
Think @ Ubuntu :~ $ LS-Al Tiger
Drwxr-XR-x 2 think 4096.
Drwxr-XR-x 51 think 4096 ..
Mkdir tiger is used to create a tiger directory. Then, run the LS-Al tiger command to view the details of the tiger directory. We can see that the system will automatically create two directory items ..

(If you are not familiar with the LS command, you can refer to my explanation of the Common commands of linxu)

4> Linux uses a standard directory structure-tree structure, regardless of how the operating system manages several disk partitions, such a directory tree only
There is one (the reason for this design is to: facilitate unified management of system files and different user files)

Tiger-John note:

We are familiar with Windows operating systems. It has several partitions and several tree directories.

For example, if we have three disks: C, D, E, and so on, we have three tree directories.

5> during Linux installation, the installer has already created a file system and a complete and fixed directory Composition Form for the user, specifying the role of each directory and the file type.

A. Try the following commands:

First enter CD/(switch to the root directory)

Then input ls to see some main directories in Linux)

Think @ Ubuntu :~ $ CD/
Think @ Ubuntu:/$ ls
Bin etc initrd. IMG. Old lost + found proc SRV USR
Boot home lib media root sys VaR
CDROM host lib32 MNT sbin think vmlinuz
Dev initrd. IMG lib64 opt SELinux TMP vmlinuz. Old

B. Check the functions of these directories.

/Bin binary executable command
/Dev special file
/ETC system management and configuration file
The base point of the/home user's home directory. For example, the home directory of the user is/home/user.
/Lib standard programming library, also known as dynamic link shared library.
/Sbin system management command, which stores the management program used by the system administrator.
/Tmp public temporary file storage point
/Root system administrator's home directory
/The directory where the MNT user temporarily installs other file systems.
/Proc virtual directory, which is the ing of system memory. You can directly access this directory to obtain system information.
/Var overflow of some large files, such as log files of various services
/Usr is the largest directory, and almost all the applications and files to be used are in this directory.

Tiger-John note:

ETC is for the system administrator.

Home is for our users.

Dev stores device information.

Proc stores kernel information

USR stores system calls.

2. file classification in Linux

1> common files

Files used by computer users and operating systems to store data, programs, and other information. It is generally stored in external storage (disks, tapes, etc.) for a long time. Regular files are generally divided into text files and binary files.

2> directory files

The Linux File System saves the node number and file name of the file index in the directory at the same time. Therefore, a directory file is a table that combines the file name with its index node number. The directory file can only be modified by the system. User processes can read directory files but cannot modify them.

3> Device Files

Linux treats all peripherals as files. Each type of I/O device corresponds to a device file and is stored in the/dev directory. For example, a row-type Printer corresponds to a/dev/LP file, and the first floppy disk drive corresponds to a/dev/fd0 file.

4> MPs queue files

It is mainly used to transmit data between processes. Pipelines are the "Media" for data transmission between processes ". Data of a process is written to one end of the pipeline, and data of another process is read from the other end of the pipeline. Linux performs the same operations on pipelines and files. It processes pipelines as files. MPs queue files are also known as FIFO files.

5> link file

It is also called a symbolic link file. It provides a way to share files. In a linked file, file sharing is not implemented through the file name, but by the pointer to the file contained in the linked file. You can use linked files to access regular files, directory files, and other files.

In Linux, how does one specify the type of a file?

To practice the following:

Enter the command LS-L on the terminal.

Bytes --------------------------------------------------------------------------------------------

Think @ Ubuntu :~ $ LS-l
Drwx ------ 19 think 4096 code

-Rwxr-XR-x 1 think 1152 deadlock. c

Lrwxrwxrwx 1 Root 33 2010-09-17 initrd. IMG. Old-> boot/initrd.

CrW-RW-1 Root 1, 5 zero

BRW-RW ---- 1 root disk 8, 0 2010-11-16 08:59 SDA

Bytes -----------------------------------------------------------------------------------

Tiger-John note:

The first letter indicates the file type:

D: indicates the directory.

-: Indicates a file.

L: indicates the link file.

B: indicates the block device file.

C: character Device File

P: indicates the MPs queue file.

For more information about the meanings of the following letter, see my other article file programming 2 in Linux.

 

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.