[英]Function prototype or not?
我在原始的Linux內核代碼中找到了以下內容。 ( 鏈接 )
static inline _syscall0(int,fork)
static inline _syscall0(int,pause)
static inline _syscall0(int,setup)
static inline _syscall0(int,sync)
由於static inline
帶有前綴,因此不能是函數調用。 它不能是原型,因為函數不能在C
重載。 最終沒有分號。 這是什么?
另外,此評論之前(如果您也可以解釋)
/*
* we need this inline - forking from kernel space will result
* in NO COPY ON WRITE (!!!), until an execve is executed. This
* is no problem, but for the stack. This is handled by not letting
* main() use the stack at all after fork(). Thus, no function
* calls - which means inline code for fork too, as otherwise we
* would use the stack upon exit from 'fork()'.
*
* Actually only pause and fork are needed inline, so that there
* won't be any messing with the stack from main(), but we define
* some others too.
*/
因為_syscall0
是宏 (在unistd.h
):
#define _syscall0(type,name) \
type name(void) \
{ \
type __res; \
__asm__ volatile ("int $0x80" \
: "=a" (__res) \
: "0" (__NR_##name)); \
if (__res >= 0) \
return __res; \
errno = -__res; \
return -1; \
}
一個簡化的例子:
#define call(type,name) \
type name(void) \
{return 0;}
static inline call(int,func)
int main(void)
{
return func();
}
擴展為:
static inline int func(void) {return 0;}
int main(void)
{
return func();
}
評論試圖解釋的是,對於用戶空間中fork()的常規實現,使用了寫時復制技術。 父進程和子進程都據此共享父進程的地址空間。 在這種情況下,數據將被設置為只讀,一旦其中任何一個試圖將其寫入副本,便不再共享資源。
在內核空間中,存在一個主內核頁面表,該表在內核空間中的所有進程之間共享。 如果我們嘗試創建主內核頁表的副本,則可能會出現奇怪的錯誤,因為它在許多進程之間共享,並且在某些時候,許多用戶級進程也引用了它。 也可能是進程對主內核頁表所做的更改對其他進程可見。 也許這就是為什么此處未實現寫入副本的原因。
遇到execve時,將創建一個新進程,因此不會對主內核頁面表進行任何更改。 可能是在調用系統調用時(如果使用了函數),則它們可能會導致內核堆棧發生更改,從而防止使用內聯函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.