I. Source of thought
In the read <<linux 0.12 full anatomy >> initialization section, the Init process is invoked through fork, where fork calls are very special, for various reasons, using inline assembly methods
#define _SYSCALL0 (type, name) \
type name (void) \
{\
long _res;
_asm_ volatile (\
"int $0x80\n\t" \
: "=a" (_res) \
: "0" (__nr_# #name); \
); \
if (_res >= 0) \ Return
(type) _res; \
errno = _res; \
return-1; \
}
Above See _nr_# #name, all of a sudden lost, what meaning this is.
two. Thinking results
1. Code Interpretation
Have to say, the above call fork way to write really let me shock, simple analysis
Prerequisite: Declare function static inline _syscall0 (int, fork)//Here the Syscall0 0 represents the meaning of no input parameters, of course, 1 represents an input parameter
Procedure: ① pre-compile phase://(just before the code has been compiled)
The static inline _syscall0 (int, fork) will be replaced with the following code
static inline int fork (void) \
{\
long _res;
_asm_ volatile (\
"int $0x80\n\t" \/// system call
: "=a" (_res) \/ /The result is stored in the register%eax and passed to the variable _res
: "0" (_n R_fork); \ //Here _nr_fork is #deine as 2 in Unstd.h, where _nr_fork is passed to the Register%eax for system call preparation
); \
if (_res >= 0) \ Return
(type) _res; \
errno = _res; \
return-1; \
}
② Run Phase:
After compiling, in fact, static _sys. That statement actually describes and defines the fork function, so it's OK to use fork () directly in the program.
Small examples of simulations:
#include <stdio.h>
#define _nr_hello
#define SUM (a) \
int a () \
{\ Return
_nr_# #a + 2; \
}
static SUM (hello);//The following precompiled programming code below
//int Hello ()
//{/return _nr_hello + 2;//due to _nr_ Hello is defined in front, so replace it directly with 10
//}
int main ()
{
int c;
C=hello (); A direct call to Hello on the program can be
printf ("[%d]\n", c);
return 0;
}
The following references: http://zgmgypb.blog.163.com/blog/static/9620281920129145154297/
2. #与 # #
In fact, in a word, is a declaration after the definition of a string, # #是告诉编译器, Precompiled will be # #前后定义的量和在一起
For example: #
#define MA_IF (exp) \
do{\
IF (exp) \
fprintf (stderr, "Warning:" #EXP "\ n"); \
} while (0)
In practice, the replacement procedure shown below will appear:
ma_if (divider = 0); is replaced by
do {
if (divider = 0)
fprintf (stderr, "Warning" "divider = = 0" "n");
while (0); is actually telling the compiler that EXP is a string
Example: # #
Here # # application can be written as a code generator, here is also inspired by the material on the web, the following example is about add, subtract instructions and corresponding processing functions of the association, the advantage is to improve the code density and
Easy to understand/due to my rookie, this experience is very awkward
#include <stdio.h>
struct arithmetic{
char *action_name;
Int (*func) (int a, int b);
int add_function (int a, int b)
{return
a+b;
}
int dev_function (int a, int b)
{return
a-b;
}
#define FUNC_REGISTER (name) {#name, name# #_function}//This must be noted that you cannot write {name, name# #_function}, which explains why ①
int main () c14/>{
int data1, data2;
struct arithmetic compute[2] = {
Func_register (add), //②
func_register (dev)
};
Data1 = Compute[0].func (1,2);
Data2 = Compute[1].func (2,1);
printf ("[%d][%d]\n", Data1, data2);
}
NOP: If this is written in ① this case, then the compilation will be an error, Add,dev is not defined, this is why?
Answer, no matter is #, # # etc, we will compile the macro replacement, for example, we #define SUN (a), a, if we are in the main program Sun (1)
will be replaced by 1, 1, there is nothing to say, because it is a constant, but if we write Sun (ABC), then the compiler is replaced by ABC, but the compiler does not know what ABC is, because you did not tell it
Similarly: here in the case of ①, in ② to replace the macro, replace the {ABC, name_function}, where ABC is not defined, the compiler does not know what, let alone do assignment, and name_function This is defined, is a function. So right so we have to precede the name plus # tell the compiler it's a string and it's OK
Then after the ② in the macro replacement, it becomes {"Add", name_function}