Use the same system call using the Library function API and the embed assembly code in C code two ways

Source: Internet
Author: User

Lab 4: Use the same system call using the Library function API and the embed assembly code in C code two ways

Name: Li Donghui

Study No.: 20133201

Note: Original works reproduced please specify the source + "Linux kernel analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

system calls about content

1. What is a system call: The operating system provides a set of interfaces for user-state processes to interact with hardware devices.

2, the user state, the kernel state, the interrupt and the system call to deal with the way is through the library function, the library function to encapsulate the system call

3. How to distinguish the kernel state from the user state under Linux system

In the kernel state: Cs:eip can be any address, 4G of memory address space

In the user state: Cs:eip can only access the 0X00000000-0XBFFFFFFF address space

4. Three layers of system calls: XYZ, System_call, and SYS_XYZ

First layer: API

Second layer: The interrupt vector corresponds to the

Third layer: System calls have different kinds of service programs

5, the meaning of the system call:

(1) Freeing the user from the underlying hardware programming

(2) Greatly improve the security of the system

(3) Portability of the user program

API and system calls

The Application Programming Interface (application program interface, API) and system calls are different.

    • API is just a function definition
    • System call sends an explicit request to the kernel via a soft interrupt

Some APIs defined by the LIBC Library reference the encapsulation routines (wrapper routine, the only purpose is to publish system calls).

    • Typically each system call corresponds to an encapsulation routine
    • The library then uses these encapsulation routines to define the API for the user

Some APIs defined by the LIBC Library reference the encapsulation routines (wrapper routine, the only purpose is to publish system calls)

    • Typically each system call corresponds to an encapsulation routine
    • The library then uses these encapsulation routines to define the API for the user

Not every API corresponds to a specific system call.

    • API may directly provide user-configured services
    • A separate API may invoke several system calls
    • Different APIs may call the same system call

return value

    • Most encapsulation routines return an integer whose meaning depends on the corresponding system call
    • 1 in most cases the kernel does not meet the request of the process

relationships between applications, encapsulation routines, system call handlers, system invoke service routines

Use the same system call using the Library function API and the embed assembly code in C code two ways

Select System Call Sys_getppid () called 64 for the system call to return the process number of the parent process for the current process

The function getppid () is the glibc of the system call Sys_getppid, which is used to obtain the process number of the parent process of the current process. The SYS_GETPPID system call number is 64. In the user state, if the user calls the Getppid (), the system will generate an interrupt, into the kernel state execution sys_getppid. The function of Getppid () is to return the ID of the parent process of the current process, which itself cannot be completed, and must request the operating system service, SYS_GETPPID, to let the operating system tell Getppid () The ID of the current process.

Experiment:

Summarize:

How the system calls work:

1, the application in the user state call API.

2. The API function saves its corresponding system call number and the parameters obtained from the application to the register, and triggers a soft interrupt, which causes the application to fall into the kernel state.

3. The System_call () function looks for the corresponding kernel function in the system invocation list based on the incoming system call number,/3, calls the kernel function according to the parameters stored in the register.

4. After the kernel function is executed, the execution result is stored in the EAX register.

5. The interrupt handler is returned after execution, and the application is brought back to the user state.

6. The called API returns the value in EAX in the final stage and ends the API call.

Use the same system call using the Library function API and the embed assembly code in C code two ways

Related Article

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.