Linux Environment Programming Guide

Source: Internet
Author: User

hardware composition of computer system

Bus

A group of electronic pipelines throughout the system, called buses, are divided into:

In-Chip bus

System bus

Data Bus db

Address Bus AB

Control Bus CB

External bus

I/O devices

I/O devices are the channels through which the system contacts the outside world

The keyboard mouse is the input device, the explicit device is the output, the disk is both an input device and an output device, the input and output is relative to the memory.

Memory

Memory is an important part, it is a bridge to communicate with the CPU. It is used to store the program and the data The program is working on , and the programs on the disk are loaded into memory to run.

Processor

Central processing Unit (CPU), referred to as the processor.

CPU is mainly composed of arithmetic, controller, register

Take reference: PC, IR

Decoding: Instruction decoder, RISC, CISC

Execution: ALU

Write Back: Memory

Jump: JMP

System structure diagram

DMA transfer

DMA (direct-access memory) a technique for transferring data from one address space to another address space. When the CPU initializes this transfer action, the transfer action itself is performed and completed by the DMA controller , and the CPU can continue to do other work, the CPU only participates in the data Transmission control at the beginning and end of the transmission action . A typical example is moving a external memory chunk to the memory area inside the chip. Such operations do not delay the processor, but can allow the CPU to re-process other work. The DMA controller is in charge of the bus when implementing the DMA transfer.


Operating system

What is an operating system

The operating system is a collection of programs that "effectively manage the resources in a computer system, reasonably manage the workflow of a computer system, and facilitate user use."

Operating system three basic abstractions

The operating system has three basic abstraction concepts: processes, virtual storage, and files .

A file is an abstract representation of an I/O device, which is an abstract representation of main memory and disk I/O devices, and the process is an abstract representation of the processor, main memory, and I/O devices.


Process

A process is an abstraction of the operating system for a running program. A system can run multiple processes, and each process seems to be using the hardware exclusively.

Process Context Switch

Virtual memory

Virtual memory provides a large, consistent, private address space for each process;

It sees memory as the cache of the address space stored on disk, storing only the active area in main memory, and exchanging data back and forth in main memory and disk as needed.

Provides a consistent address space for each process, simplifies storage management, and protects the address space of each process from being broken by other processes.

Process Virtual address space


File

A file is a series of byte sequences that provide the application with a unified view of the various I/O devices in the system.

Virtual file System VFS

Virtual file system is a kernel implementation of a framework for user space to provide a unified file operation interface, that is, file system calls. It provides a consistent abstraction interface within the kernel for different real-world file systems.

The user interacts with the virtual file system in the kernel through system calls to manipulate the actual file system and device.


System Programming VS. Application programming

System programming

Use system calls and C libraries to access system resources on top of the operating system. Write service applications such as Apache, GCC, GDB, and more.

System resources include: processor, input/output, process management, memory, device, timer, interprocess communication, network

Application programming

Build applications on higher-level programming interfaces or libraries. such as Android program (Android SDK), iphone program (iphone SDK), QT program Design (QT), MFC program Design (MFC) and so on.

Where system calls are located in the system

All operating systems provide an entry point for multiple services, whereby the program requests services to the system. These entry points are called system calls,

C Library

Here we refer to the C library (LIBC), which refers to the set of C functions defined by standard C. such as standard input and output functions, string processing functions, dynamic storage allocation functions, date-time functions, mathematical functions and so on.

The GNU release of LIBC is called glibc.

System call and C library relationship

System call and C library from the formal view are C function;

However, some of the C library functions are implemented by invoking a system call, such as malloc, free calls to the write system call brk,printf, and some functions do not require any system calls, such as ABS (Fabs), strcpy, atoi, etc. Because it is not necessary to use the kernel service;

As a result, system calls typically provide a minimal interface, and C library functions typically provide more complex functionality.

How the kernel handles system calls

The kernel implements the system call through Soft interrupts , each system call is given a system call number, on the i386 platform, the execution of a system call through the INT 0x80 instruction to complete, from the user state switch to the kernel mentality.

Register EAX store system call number, register EBX, ECX, edx, ESI, EDI storage System call parameters, for system calls with more than 5 parameters, a register (such as EBX) to a cache of user space, which stores parameters (more than 5) for all system calls.

Error Handling

In system programming, an error is usually represented by a function return value (typically-1 means that the function performs an error) and is described by a special variable errno.

errno This global variable is declared in the Errno.h header file as follows: extern int errno;

Error handling function: Perror, strerror;

Example

int main () {    int ret = close (ten);    /*        if (ret = =-1)        {            perror ("Close error");        }    *    /if (ret = =-1)    {        fprintf (stderr, "Close error with message:%s, errno =%d\n",                strerror (errno), errno) ;    }    return 0;}

Common error Codes

E2big

Parameter list too long

Eaccess

Insufficient permissions

Eagain

Retry

EBADF

Bad file descriptor

Ebusy

Device or resource busy

Echild

No Child processes

EDOM

Mathematical parameters are not within the function domain

Eexist

File already exists

Efault

Address error

Efbig

File is too large

Eintr

System call is interrupted

Common error code Print program

int main () {for    (int i = 0; i < ++i)    {        errno = i;        cout << "errno" << I << ":" << strerror (errno) << Endl;    }    return 0;}

Linux Environment Programming Guide

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.