Linux system calls with me (1)

xiaoxiao2021-03-06  34

Technical Article: Linux System Call and I learn (1) Postter for LLC This article is the first article of Linux system call series articles, definition, basic principle, method, and precautions for Linux system, probably, In order to read the reader to establish a general impression on the Linux system. Author: This article taken from the town of Ray: IBM DW China September 23, 2002 the Japanese Linux system is the first chapter, the definition of Linux system calls, the basic principle, using methods and precautions to call a series of articles probably made a presentation, In order to read the reader to establish a general impression on the Linux system.

What is the system call?

A set of subroutines for implementing various system functions are set in the Linux kernel, called system calls. Users can call them in their own applications through the system call command. From a point of view, system calls and ordinary function calls are very similar. The difference is only that the system call is provided by the operating system core, running on the core state; and the normal function call is provided by the function library or the user itself, running on the user state. The two are similar in the way of use, which will be mentioned below. Some C language functions are also available with the core of the Linux. These libraries have made some packages and extensions on system calls because these library functions are very close to system calls, so they are used to call these functions as system calls.

How many system calls in Linux?

This problem can not answer it, even if the Linus Torvaldz does not see it, it can be clear. In the 2.4.4 ingredient, the narrow system calls have a total of 221, you can find them originally in the /includ., you can also find them in the original, or you can pass the command "man 2 syscalls" Look at their directory (MAN PAGES version is generally old, there may be many latest calls). The generalized system call is also those implemented in the form of library functions. They have never been counted. This is a good job, the new kernel is constantly being launched, each new core function The number of changes is not cared at all, at least the modifier of the kernel does not care, because they have never released such statements. There is a list of organizable lists, it is impossible to be very comprehensive, but the common system calls are basically included, and there is only a lot of parts that are usually available. This column will have a choice. They are introduced.

Why use system calls?

In fact, many of the C language standard functions we have learned, in the Linux platform, is done by the system call, so if you want to understand the principles of the system, master the various system calls are preliminary requirements. . Further, if you want to be a programs, it is the Hacker, which we often say, is also a thorough understanding of various system calls. Even if you remove the above reasons, you will also find that in many cases, the system call is a simple and effective way to achieve your thoughts, so it is possible to try to master some system calls, this will be The program design process brings unexpected help.

How is the system call work?

In general, the process cannot access the kernel. It cannot access the kernel function in the memory space, which cannot be accessed. The CPU hardware determines these (this is why it is called "protection mode"). System call is an exception to these rules. The principle is that the process fills the register with an appropriate value, then calls a special instruction, which will jump to a location in the previously defined kernel (of course, this location is readable but not writable). In the Intel CPU, this is implemented by interrupt 0x80. Hardware knows once you jump to this position, you are not running in restriction mode, but as the kernel of the operating system - so you can do it for what you want. The roof position that can jump to the kernel position called SYSEM_CALL. This process checks the system call number, this number tells the kernel process which service is requested. Then, it looks at the system call table (Sys_CALL_TABLE) to find the called kernel function entry address. Then, then call the function, wait after returning, do some system check, finally return to the process (or to other processes, if this process is exhausted). If you want to read this code, it is in the /kernel/entry.s ,entry (system_call)

Next line.

How to use system calls?

Let's take an example:

#include

/ * Define macro _syscall1 * /

#include

/ * Define type TIME_T * /

_syscall1 (time_t, time, time_t *, tloc) / * macro, get the prototype of the TIME () function after expanding * /

Main ()

{

Time_t the_time;

THE_TIME = TIME ((Time_t *) 0); / * Call TIME System Call * /

Printf ("Time IS% LD

", the_time);

}

The system call time returns from GM Ni Time to 0:00 on January 1, 1970 to the current number of seconds. This is the most standard system call form, macro _syscall1 () expands to get a function prototype, I will explain it later. But in fact, if the program is changed to the following, the program can also run the same result.

#include

Main ()

{

Time_t the_time;

THE_TIME = TIME ((Time_t *) 0); / * Call TIME System Call * /

Printf ("Time IS% LD

", the_time);

}

This is because the system calls have been implemented in the form of the library function in Time.h, which saves us the step of the function prototype for us to the call _syscall1 macro. Most system calls are implemented in various C language functions, so in general, we can call system calls like calling ordinary library functions, only in extreme cases, we have the opportunity to use To _syscall * () these macros.

_syscall * () What is it?

7 macros are defined in Unistd.h, respectively

_syscall0 (Type, Name)

_syscall1 (Type, Name, Type1, Arg1)

_sysCall2 (Type, Name, Type1, Arg1, Type2, Arg2)

_SysCall3 (Type, Name, Type1, Arg1, Type2, Arg2, Type3, Arg3)

_SysCall4 (Type, Name, Type1, Arg1, Type2, Arg2, Type3, Arg3, Type4, Arg4)

_SysCall5 (Type, Name, Type1, Arg1, Type2, Arg2, Type3, Arg3, Type4, Arg4, Type5, Arg5)

_SysCall6 (Type, Name, Type1, Arg1, Type2, Arg2, Type3, Arg3, Type4, Arg4, Type5, Arg5, Type6, Arg6) They seem to be inextristic, but their substance and

#define maxSize 100

There is no difference in MaxSize inside. Their role is to form the corresponding system call function prototype for our calls in the program. We can easily find the rules, _syscall, and the number of Typen, the number of Typen, Argn. In fact, the numbers behind _syscall indicate the number of parameters that form the function after the expansion, let us see an instance, just used the TIME system call:

_syscall1 (Time_T, Time, Time_t *, Tloc)

The situation after the start is like this:

Time_t Time (Time_t * Tloc)

{

Long __res;

__asm__volatile ("int $ 0x80": "= a" (__res): "0" (13), "b" ((tloc));

Do {

IF ((unsigned long) (unsigned long) (- 125)) {

Errno = - (__ rES);

__RES = -1;

}

Return (TIME_T) (__RES);

} while (0);

}

It can be seen that _sysCall1 (time_t, time, time_t *, tloc) expands into a function called Time, the original parameter Time_t is the return type of the function, the original parameter TIME_T * and TLOC constitute the parameters of the new function. In fact, the prototype of the TIME function used in the program is it.

What is errno?

To prevent and normal return values, the system call does not return the error code directly, but put the error code in a global variable called errno. If a system call fails, you can read the value of Errno to determine the problem. Errno represents the error message represented by the different values ​​in errno.h, you can also look at them through the command "Man 3 Errno". It should be noted that the value of Errno is only set when the function is wrong. If the function does not have an error, the value of errno is not defined, and it will not be set to 0. In addition, it is best to store it in another variable before processing errno, as in the error handling process, even if the function like Printf () is wrong, Errno is also changed.

Is the system call compatibility?

Unfortunately, the answer is - not good. But this will never mean that your program will cause the system to crash for three days, because the system call is provided by Linux kernel, so they work very stable, there is no need to doubt it, in most cases, The system call is more efficient than the code you have written in your own. However, between the various versions of Linux, the compatibility of the system call is not as good as imagining, which is determined by the nature of Linux itself. Linux is a group of programming masters developed by spare time. Most people in the middle have not regarded Linux as a serious commercial software, and now some of them, with Linux business companies and people who have linux Growth, many people's brains have changed.) The result is that if new programs have contradictory in efficiency and compatibility, they tend to discard compatibility and pursue efficiency, so if they think that a system call is implemented. It's a bad, they will do not hesitate to make changes, sometimes even the connection is changed together, more terrible, many times, they don't play their own modifications, and I will find it in any document. Less than a reminder about the modification. This way, whenever the new kernel is launched, it is likely to update some system calls, and the user prepared by the user will follow an error. Speaking of this, do you feel dim for your future? Oh, don't be too nervous, as mentioned earlier, with more and more people regard Linux as their own rice bowl, incompatible situation is increasingly rare. From the 2.2 version, the Linux kernel has been very stable, but despite this, you still need to compatibility with your application after each new kernel is launched to prevent accidents. How to learn to use Linux system calls?

You can use the "MAN 2 System Call Name" command to view the introduction of each system call, but this first requires you to have a good English foundation, followed by a certain programming and system programming, MAN PAGES is not It will involve too much application details because it is just a manual rather than the tutorial. If the thing provided by Man Pages can't make you feel very satisfied, come with me, this column will show you the endless charm of the Linux system call programming. Two small requirements for readers: 1) The reader must have a certain C language programming experience; 2) The reader must have a certain experience in Linux. If you can completely understand this article from the beginning to what you said here, you are qualified. Pack the bag, ready to start! "From IBM DW China"

转载请注明原文地址:https://www.9cbs.com/read-64429.html

New Post(0)