2010-05-14 14 views
0

我成功地從程序集中調用了exit syscall,但我正努力調用_getpid系統調用並使用它的返回值。下面是我使用getpid從syscall與32位應用程序和雪豹上的內核

.text 
.globl _getpiddirect 

_getpiddirect: 
    pushl %ebp 
    movl %esp, %ebp 
    subl $8, %esp 
    movl $39, %eax 
    int $0x80 
    addl $8, %esp 
    popl %ebp 
    ret 

#include <stdio.h> 
#include <unistd.h> 

extern unsigned long getpiddirect(); 

int main(int argc, const char *argv[]) 
{ 
    printf("%lu\n", getpiddirect()); 
    printf("%lu\n", (unsigned long) getpid()); 
    return 0; 
} 

getpiddirect保持返回4056

回答

0

那是因爲39是getppid代碼的代碼 - 得到進程ID,這是你得到的是4056. getpid代碼是20,但請查看/usr/include/sys/syscall.h中SYS_getpid的值作爲系統上使用的精確常量。

另外,我不知道爲什麼你要在通過中斷調用getpid之前在堆棧上8個字節。它不影響任何東西,只是沒用,不是嗎?

+0

謝謝你的回答是正確的。我誤以爲getpid與getpid。 額外的8個字節是必要的,以保持堆棧對齊mac os ABI的要求。 – 2010-05-31 12:18:51