Android system Development (6)--linux bottom input and output

Source: Internet
Author: User

First, the operating system architecture of the computer is composed of a bunch of hardware, the operating system is to effectively control these hardware resources of the software. In addition to effectively controlling the allocation of these hardware resources and providing the functionality required for the computer to run, the operating system provides an entire set of system invocation interfaces in order to provide an environment in which programmers can develop software more easily.
As shown, the most intermediate is the hardware, the operating system is composed of kernel and system call interface, the kernel is the direct operation of the hardware, the kernel provides the control of these hardware resources and process management, the system call interface provides a unified calling interface to facilitate the developer call. The top layer is the application, which enables the operation of the hardware by invoking the system interface in the application. Second, Linux I/O system
such as the Linux I/O system structure diagram, the kernel layer has a virtual file system, is to use the standard C class library encapsulated API, so we need to manipulate the file system just call the API here.
Three, I/O operation Process 1, open the file an application by requiring the kernel to open the appropriate file, declaring that he would like to access an I/O device, the kernel returns a non-negative integer, called the Descriptor (descriptor) "File uniquely identify" 2, read and write file reads: Copy from the file n> 0 bytes to memory (memory) write: Copy n>0 bytes from memory (memory) to file 3, change file location 4, close file for the kernel, all open files are referenced by a file descriptor. The file descriptor is a non-negative integer. When an existing file is opened or a new file is created, the kernel returns a file descriptor to the process. When reading and writing a file, the file descriptor FD returned with open or create identifies the file and transmits it as a parameter to read or write. The stream (for example: fopen) Returns a file structure pointer, which contains the document descriptor, The file structure function can be thought of as encapsulation of the system call to the FD direct operation, which has the advantage of having an I/O cache

Linux supports a wide variety of file system formats, such as Ext2, Ext3, ReiserFS, FAT, NTFS, iso9660, and so on, with different file system formats for different disk partitions, discs, or other storage devices, but these file systems can Mount to a directory, so that we see a unified directory tree, the various file system directories and files we use the LS command to look the same, read and write operations are the same, how is this done? The Linux kernel makes an abstraction layer on a variety of file system formats, making the concepts of files, directories, read-write access, and so on, an abstraction layer, so that the various file systems look the same, and this abstraction layer is called the virtual file system (vfs,virtual Filesystem)


Linux bottom input and output we can go to GNU download libc source code and help documentation (source: http://ftp.gnu.org/gnu/glibc/) to open and close the file stream
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h>int Main (int argc, char *argv[]) {        //open file        if (argc<2) {                printf ("Please input Filename\n ");                Exit (1);        } else{                int fd;                umask (0000);                FD = open (argv[1], o_rdwr| O_creat, 0666);                if (FD <-1) {                        printf ("error\n");                        Exit (1);                } else{                        printf ("success=%d\n", FD);                        Close (FD);                        printf ("closed\n");                }        }        return 0;}
Read the file (the process of writing a file is similar to reading a file)
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include                <stdlib.h> #include <string.h>int main (int argc, char *argv[]) {//open file if (argc<2) {                printf ("Please input filename\n");        Exit (1);                }else{int fd;                umask (0000); FD = open (argv[1], o_rdwr|                O_creat, 0666);                        if (FD <-1) {printf ("error\n");                Exit (1);                        }else{printf ("success=%d\n", FD);                        Char buf[1024];                        memset (buf, 0, 1024);                        int returnum = Read (FD, buf, 1024);                        if (returnum! =-1) {printf ("buf=%s\n", buf);                                }else{printf ("Read error\n");                        Exit (1);       }                 Close (FD);                printf ("closed\n"); }} return 0;}


Android system Development (6)--linux bottom input and output

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.