Learn from NetEase Cloud Classroom Monensin Teacher's "Linux kernel Analysis" in the Open class content
Directly on the function:
#include <stdio.h><sys/types.h><unistd.h>int main (void ) { pid_t id; = getpid (); printf ("TheID of this process is%ld\n", (long) ID); return 0 ;}
#include <stdio.h>#include<sys/types.h>#include<unistd.h>intMainvoid) {pid_t id; //id = getpid ();ASMvolatile( "mov $0,%%ebx\n\t" "mov $0x14,%%eax\n\t"//0x14 is the Getpid system call number, using the EAX register "int $0x80\n\t"Generate interrupts, from the user state into the kernel state, the int instruction before entering the kernel state will be the register to save work"mov%%eax,%0\n\t"//System call return value:"=m"(ID)); printf ("The ID of this process is%ld\n", (Long) (ID); return 0;}
The following is a system call that requires passing parameters, the mkdir function
#include <sys/stat.h><sys/types.h>int main (void) { const char"test"; if (0 = = mkdir (path, S_irwxu) )return0; Else return -1;}
#include <sys/stat.h>#include<sys/types.h>intMainvoid){ Const CharPath[] ="Test"; mode_t Mode=S_irwxu; intrst; ASMvolatile( "mov $0x27,%%eax\n\t" "int $0x80\n\t" "mov%%eax,%0\n\t" : "=m"(RST):"b"(path),"C"(mode)//pass the parameters by successively EBX and ecx two registers); if(0==rst)return 0; Else return-1;}
Linux system calls