An example of adding a system call in Linux

Source: Internet
Author: User
Add a system call example in Linux-Linux general technology-Linux programming and kernel information. The following is a detailed description. Author: PISA Park · Snail House

I. Basic Ideas:
Add the system multiplication sysMul (int a, int B) to the system call function to calculate the product of a and B. Encapsulate the system method as the multiplication party usrPow (int a, int n), and calculate the n power of.

Ii. preparations:
1. Environment: Fedora 6

2. The source code is not provided in my Fedora6, and? Why? How can I get lucky when I buy a dark neon flute? Cangjie ?. 6.18.1 kernel source code package; not detailed.

3. Detailed steps

1. Add the system calling and writing functions in kernel/sys. c:
Asmlinkage int sysMul (int a, int B)
{
Int c;
C = a * B;
Return c;
}

2. assign a call number to the new system call:
Add the following code in include/asm-i386/unistd. h:
# Define _ NR_sysAdd 264

3. Add the code in the system call table/arch/i386/kernel/entry. S to point to the newly written system call function:. long SYMBOL_NAME (sysMul)

4. Use a series of make statements to compile the kernel (this process is similar to that of running uClinux in skyeye, but it is not detailed) and put the generated kernel into the boot directory,

5. Modify grub and add a new boot item:
Add the following code to menu. lst:
Title testRoot
Root (hd0, 0)
Kernel/bzImage ro root =/dev/hda1

6. After the restart, the testRoot option is available in the startup directory. Now the system includes the system call sysMul () that we have written.

7. The uPow. c file is created under the root directory.

1) encapsulate the usrPow () function that has the taste of API called by the System

# Inculde
# Include
_ Syscall2 (int, sysMul, int, a, int, B)


Int usrPow (int m, int n)
{
Int I;
Int r = 1;
For (I = 0; I <n; I ++)
{
R = sysMul (r, m );
}
Return r;
}

2) write the main () of the usrPow () function using the user API ()

Main ()
{
Int bNum, pNum;
Int result;
Printf ("input: \ n ");
Scanf ("% d", & bNum, & pNum );
Result = usrPow (bNum, pNum );
Printf ("result =", result );
}

8. Compile
Gcc-o uPow. c

9. Run.

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.