Experiment-using the same system call (Li Pengju) using the Library function API and the embed assembly code in C code two ways

Source: Internet
Author: User

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

Grasping Writer: Li Pengju No.: 20132201

(* Original works reproduced please specify the source *)

(Study course: "Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000)

Experimental requirements:

    1. Select a system call (except for number 13th system call time), see the System invocation list HTTP://CODELAB.SHIYANLOU.COM/XREF/LINUX-3.18.6/ARCH/X86/SYSCALLS/SYSCALL_32.TBL

    2. Refer to the video in the way the Library function API and C code embedded in the assembly code two ways to use the same system call, recommended in the experimental Building Linux virtual machine environment to complete the experiment.

Experimental process:

First, select the system call to use: I chose number 64th, Getppid.

Find out about the use and description of the function on the Internet: getppid (get the process identifier of the parent process)

Table header file #include <unistd.h>

Define function pid_t getppid (void);

The function Description Getppid () is used to obtain the parent process ID of the current process.

Returns the value of the current process's parent process identification code.

#include <unistd.h>

Main ()

{

printf ("My parent ' pid =%d\n", Getppid ());

}

Perform

My Parent ' PID =463

Then, go into the lab building, build the file, and use the above example to write the C language code

Save and compile with GCC./Run the file to see the results of the run:

Run successfully, then modify the file, and then replace the embedded Assembly statement:

Experiment Summary:

The complete code is attached first:

C Language code:

#include <unistd.h>=%d\n ", Getppid ());}

Embedded assembly code:

#include <unistd.h>intMain () {intA=0; ASMvolatile(          "mov $0,%%ebx\n\t"          "mov $0x40,%%eax\n\t"          "int $0x80\n\t"          "mov%%eax,%0\n\t"          :"=m"(a)); //a = Getppid ();printf"The number of PID is:%d", t);}

This assembly code allows you to see the process in which the program executes, where the system call makes an explicit request to the kernel via a soft interrupt. At least I can see that int $0x80 is the interrupt instruction for the/T syntax, and through the interrupt call, the call number will be loaded into the EAX register implementation, and then the corresponding value of the change process. The value is given by EAX to a.

However, I doubt that I do not have a problem, the results of the Assembly and the C language out of the results are different, but the operating system is uncertain, and two times the way the printout is different, it may affect the output of the results, of course, it is possible that I write the statement of what is wrong, I will tell the teacher, After the students check and then make changes, perfect.

Experiment-using the same system call (Li Pengju) using the Library function API and the embed assembly code in C code two ways

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.