Linux File Operations Summary

Source: Internet
Author: User
Tags bitwise fread function prototype

http://blog.163.com/he_junwei/blog/static/19793764620152592737741/

The IOCTL?? Lseek??

Files are an important concept in Linux. In Linux, everything (almost everything) is a file. Simply put, the basic printf () function in C, the scanf () function, actually belongs to the file operation.

For file operations, although they are implemented in the form of function calls, they can be divided into two categories: System invocation and library function.

This article will first introduce the concept of files in Linux, system calls and library functions, and then specifically discuss the two ways of file operations.

The main contents of the blog are as follows:

    1. Files in Linux
    2. File Access-Library functions
    3. File access-system calls
    4. Library functions
    5. Standard I/O Library
    6. /proc File System

1 files in Linux

1.1 Concepts

According to the normal definition, the file is just a bunch of data, in the down, is the memory of 0101 ... And the document we are discussing here has a broader definition. For files in Linux, my understanding is:

Linux files have the characteristics of: can be used by the operating system or programs to provide information, but also internal input information, can be created, deleted.

In Linux, files are of particular importance, and they provide a simple and unified interface for operating systems and devices. In Linux, almost everything can be seen as a file.

This means that normal programs can use disk files, serial ports, printers, and other devices exactly as they do with files (normal definitions).

Hardware devices are also represented as files in the Linux operating system. For example, you can mount a CD-ROM drive as a file with the following command,

#mount-T Iso9660/dev/hdc/mnt/cdrom

#cd/mnt/rom

You can then roam the CD-ROM directory as you would access a normal file.

1.2 Operation

As in general, the operation of files in Linux requires only five basic functions:

Open, close, read, write, and IOCTL

By invoking these functions, the files in Linux can be read, written, and so on. However, this operation is divided into system calls and library function calls. Simply put, the system call is the most direct way,

Library function calls are eventually implemented through system calls. Library function calls can be thought of as optimizations made to the system for efficiency considerations.

Differences and linkages between library function calls and system calls see: Differences between Linux system calls and library function calls

We can access and control files and devices with very few functions. These functions are called system calls, which are provided directly by the operating system, and they are interfaces to the operating system itself.

The core part of the operating system, the kernel, is actually a set of device drivers. Here are some of the interfaces that control the hardware.

2 File access-system calls

Access to files via system transfer is the most straightforward way. The system call function directly acts on the device driver of the operating system kernel for file access.

2.1 File Descriptor

Files that need to be processed in the system (read and write operations) require an identity so that the file can be identified elsewhere, resulting in a file descriptor. The file descriptor is a small integer, which simply means that

A file ID is used to uniquely identify the file in the system. The total number of file descriptors is the maximum number of files that can be opened by the system, depending on the configuration of the system.

When you start running a program, that is, when the system starts running, it typically has three file descriptors that are already open. They are:

    • 0: Standard input
    • 1: Standard output
    • 2: Standard error

The file descriptor for the other file, which is returned when the calling file opens the function open. This means that each device corresponds to a file descriptor. The file descriptor is assigned by the operating system and is assigned the smallest each time.

2.2 Write system call

Write, which writes the buffer data to the file. Note that the files here are widely meaningful files, such as writing to disk, writing to a printer, and so on.

function prototypes for write () in Linux:

size_t write (int fildes, const void *buf, size_t nbytes);

Parameter description:

Fildes: A file descriptor that identifies the target file to write to. For example: The value of Fildes is 1, just like standard output write data, that is, display data on the display, if 2, you want to label the wrong write data.

*BUF: The file to be written is a string pointer.

Nbytes: The number of characters to write.

function return Value: size_t Returns the number of characters that were successfully written to the file. It should be noted that write may report that he writes fewer bytes than you require. This is not necessarily a mistake. In the program, you need to check

Error has been found and then called write again to write the remaining data.

Take a look at the following example:

Operation Result:

This program only displays a message on the standard output.

Read system call

The system call read reads the data from the file. The file to be read is identified with a file descriptor, and the data is read into a pre-defined buffer. He returns the number of bytes actually read in.

function prototypes for read in Linux:

size_t read (int fildes, void *buf, size_t nbytes);

Parameter description:

Fildes: File descriptor that identifies the file to be read. If 0, the data is read from the standard input. Similar to the scanf () feature.

*BUF: Buffer that is used to store read-in data.

Nbytes: The number of characters to read.

Return value: size_t Returns the number of characters successfully read, which may be less than the number of bytes requested.

Operation Result:

Open System call

The function of the system call to open opens a file and returns a descriptor for the file.

To put it simply, open establishes a path to a file or device's access. If the operation succeeds, it returns a file descriptor that is used by system calls such as read and write to use the file descriptor on the file or

Device to operate. This file descriptor is unique and will not be shared with any other running process. If two programs open a file at the same time, you get two different ask price descriptors. If

At the same time, the operation of two files, their respective operations, complementary effects, each other overwrite (after writing the overwrite written first) in order to prevent the file read-write conflict, you can use the file lock function. It's not

This focus, introduced later.

There are two function prototypes for open in Linux:

int open (const char *path, int oflags);

int open (const char *path, int oflags, mode_t mode);

Parameter description.

Path: The file or device name you want to open.

Oflags: Indicates the access mode to open the file. The open call must specify one of the file access modes as follows:

The open call Ha can include a combination of the following optional modes (with a bitwise OR operation) in the Oflags parameter:

    • O_appedn: Appends the write data to the end of the file.
    • O_trunc: Set the file length to zero and discard the contents later.
    • O_creat: If necessary, create the file according to the access mode given in the parameter mode.
    • O_EXCL: Called with O_creat to ensure that the caller creates the file. Use this mode to prevent two programs from creating a file at the same time, and the open call will fail if the file already exists.

For other possible oflags values, see the Call Manual for Open.

Mode

When creating a file using the Oh, o_creat flag Open, we must use the open call in three parameter format. The third parameter mode is a number of flags that are obtained by bitwise OR. They are:

    • S_IRUSR: Read permission, file owner.
    • S_IWUSR: Write permission, file owner.
    • S_ ixusr: Execute permissions, file owner.
    • S_IRGRP: Read permissions, the group to which the file belongs.
    • S_IWGRP: Write permission, the file belongs to the group.

。。。。

Take a look at the following example:

Open ("MyFile", O_creat, s_irusr| S_ixoth;

His role is to create a file named MyFile, where the file owner has read permissions, and other users have execute permissions, and only those permissions.

Operation Result:

The program creates a file named MyFile, which has read permissions for the master, and other users have execute permissions, and only those permissions.

Close System call

The close system call is used to "close" a file, and the close call terminates a file descriptor Fildes with the association between its files. The file descriptor is freed and can be reused.

Close successfully returned 1, error returned-1.

#Include <unistd.h>

int close (int fildes);

IOCTL system call

The IOCTL provides an interface for controlling the behavior of devices and their descriptors and for configuring the underlying services. Terminals, file descriptors, and even tape drives can also define the IOCTL for them, specifically

For details, refer to the user's manual for the specific device.

Below is the function prototype of the IOCTL

#include <unistd.h>

int ioctl (int fildes, int cmd,,,,,,);

The IOCTL executes the action given in the CMD parameter on the specified object of the descriptor Fildes.

Other system calls related to file management

There are many other system calls that can manipulate files.

Several commonly used such as: Lseek () Set the file descriptor Fildes read-write pointer to the specified file, that is, it can set the next read and write location of the file.

Fstat,stat,lstat is a function operation associated with a file descriptor and is not described here.

DUP,DUP2 system calls. The DUP provides a way to copy file descriptors so that we can access the same file with two or more different file descriptors. This can be used to

Read and write data to and from different locations in the file.

4 Library functions

In input and output operations, direct use of system calls is very inefficient. There are two specific reasons:

    • System calls can affect system performance. System calls are expensive compared to function calls. Because in the execution of the system call, you switch to the kernel code area to execute, and then return the user code. This is bound to take a lot of time to spend. One solution is to minimize the number of system calls and make every system call complete as many tasks as possible. For example, each system call writes a large number of characters rather than a single character.
    • The hardware restricts the system's call to a data block that can be read and written once. For example, a tape drive typically has a write data block length of 10k, and if the abbreviated data is not an integer multiple of 10k, the tape drive will still wrap around the tape in 10k, leaving a gap on the tape.

To improve the efficiency of file access operations and to make file operations easier, Linux distributions provide a range of standard libraries of functions. They are a collection of functions that you can use in your own program,

To manipulate the file. This is an example of a standard I/O library that provides output buffering capabilities. You can write data blocks of any length efficiently, and the library function arranges the underlying function calls (system calls) when needed

In other words, the library function adds an intermediate layer between the user and the system. As shown in the following:

Library functions are packaged system calls based on actual needs, and users can easily use library functions in the program, such as the standard I o Library (later)

5 standard I/O Library

The standard I/O library and its header files <stdio.h> provides a common interface for the underlying I/O system calls. This library has now become part of ANSI Standard C, but the previous system call is not.

The standard I/O library provides a number of complex functions for formatting output and scanning input, and it is also responsible for meeting the device's buffering needs.

In many ways, using a standard I/O library is similar to using the underlying file descriptor. You need to open a file, a file access path has been established (that is, the file descriptor in the system call)

In the standard I/O library, the stream, which corresponds to the file descriptor, is implemented as a pointer to the structure file.

When you start the program, there are three file streams that are automatically opened. They are:

    • STDIN: Standard input
    • STDOUT: Standard output
    • STDERR: standard error output

Some common I/O library functions are described below:

5.1 fopen function

The fopen function is similar to the open function in a system call. As with open, it returns the identifier of the file, except that it is called a stream, which is implemented as a pointer to a file in the library function.

If you need explicit control over the behavior of your device, it is best to use the underlying system call, as this avoids some unintended side effects, such as input/output buffering, that can be caused by using library functions.

Function Prototypes:

#include <stdio.h>

FILE *fopen (const char *filename, const char *mode);

Parameter description:

*filename: Open file's filename

*mode: Open the way

R opens the file as read-only, and the file must exist.
r+ Open the file as read-write, the file must be present.
rb+ Read and write open a binary file that allows read data.
rw+ Read and write open a text file that allows reading and writing.
W Open Write-only file, if the file exists then the file length is clear to 0, that is, the contents of the file will disappear. If the file does not exist, create the file
w+ Open a read-write file, if the file exists then the file length is clear to zero, that is, the contents of the file will disappear. If the file does not exist, the file is created.

Fopen in success is to return a non-empty file * pointer. Failed to return null

5.2 Fread/fwrite function

The Fread function reads data from the file stream and corresponds to the Read;fwrite function in the system call to write data from the file stream, corresponding to the write in the system call

Function Prototypes:

#include <stdio.h>

size_t fread (void *ptr, size_t size, size_t nitems, FILE *stream);

Parameter description:

*ptr the buffer where the data is to be read, that is, where the data is to be read.

Size: Specifies the length of each data record.

Nitems: Counts, giving the number of records to transfer.

Return value: The number of records successfully read to the data buffer, and when the end of the file is reached, his return value may be consumed with nitems, or even 0

size_t fwrite (const coid *ptr, size_t size, size_t nitimes, FILE *stream);

He writes data from the specified data buffer ptr to the file stream, returning the number of records that were successfully written.

5.3 fclose function

The Fclose function closes the specified file stream stream, which causes all the data that is not written to be written out. Because the Stdio library function buffers The data, it is important that all calls to the Fclose function.

The fclose function should be called if the program needs to ensure that the data has been completely written out. The Fclose function is automatically called when the program ends normally, but it is not possible to detect errors caused by calling fclose.

The function prototypes are as follows:

#include <stdio,h>

int fclose (FILE *stream);

5.4 Fflush function

The purpose of the Fflush function is to write out all the data that is not written in the file stream. In terms of efficiency, data buffers are used when using library functions, and write operations occur when the buffer is full. Using the Fflush function

You can write out all the data in the buffer without worrying about whether the buffer is full. The execution of fclose implies that the Fflush function is called, so fflush is not called before fclose execution.

Function Prototypes:

#include <stdio.h>

int fflush (FILE *stream);

6/proc File System

Linux sees everything as files, and hardware devices have entries in the file system as well. Files in the/dev directory use the underlying system call to access the hardware in such a special way.

The/proc file system can be seen as a special file system in which each file corresponds to a separate hardware, so the user can access the hardware device like a file through the proc file system.

The file system typically behaves as a/proc directory. This directory contains a number of special files to allow high-level access to driver and kernel information.

If you want to know CPU information, kernel version information, etc., you can go through the proc file system.

Files in the/proc directory vary depending on the system. The files in/proc on my computer are as follows:

In most cases, you can obtain status information by reading these files directly.

6.1 Accessing device information

For example, get information about the CPU:

Memory usage information (show only local ~):

Each time the contents of these files are read, the information they provide will be updated in a timely manner. So read the Meminfo file again to get different results.

More information given by specific kernel functions can be found in subdirectories of the proc directory.

6.2 View the information given by the kernel function

For example: View usage statistics for network sockets:

6.3 Viewing process information through proc

With the PS command to get the currently running process, each process has a corresponding information file in proc, by viewing this file, you can know the process-related information:

The current working directory for Process 2754 is:/hme/yyl

The program/bin/su is running, and other information is not described here;

Modify the contents of the proc file system

For example, the total number of files that are running on the system at the same time is a parameter to the Linux kernel.

If we want to increase the value of this song, it can be done by writing the same file.

Note: The write operation to the proc should pay attention to the permission problem, should be careful when modifying, the inappropriate value may affect the system to run.

Linux File Operations Summary

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.