How to access system call

Source: Internet
Author: User

To access the system call, the C library function is generally used. The function enters the kernel state through the Soft Interrupt number 0x80, calls the corresponding interrupt handler, and runs the interrupt code in the kernel state through passing parameters.

Generally, system calls are supported by the C library. You can use the system call by adding a standard header file and linking it to the C library. However, if it is a system call implemented at the underlying layer, the C library may not support it. Fortunately, Linux itself provides a set of macros for direct access to system calls. It sets the registers and calls the commands. These macros are _ syscalln (), where N ranges from 0 to 6, representing the number of parameters to be passed to the system call, this is because the macro must know how many parameters are pushed into the register in what order. For example, the C library has implemented the OPEN function. Here we implement the open system call function.

The system call definition of the open function is long open (const char * filename, int flags, int mode );

We use _ syscalln () to implement the following:
# Define nr_open 5
_ Syscall3 (long, open, const char *, filename, Int, flags, Int, Mode)

Int main (){
Int FD = open ("FILENAME", flags, mode );
Close (FD );
Return 0;
}

Close is not implemented here, so the C library is called, but the self-implemented OPEN function is called.

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.