2012-09-05 22 views
1

我需要知道哪個perl腳本正在使用我的C CLI。有沒有辦法打印調用我的C二進制進程的PID

使用bash,我可以輕鬆打印使用「誰」跑的腳本:

CALLER=$(ps ax | grep "^ *$PPID" | awk '{print $NF}') 
echo $CALLER 

到現在我一直在用這個作爲一個包裝,但它的效果並不理想。有沒有辦法從C中獲取這些信息?

(我運行UNIX W/GCC)

+1

'getpid()'....? – JosephH

回答

3

的父進程ID你應該看看getpidgetppid功能從<unistd.h>

#include <stdio.h> 
#include <unistd.h> 
#include <sys/types.h> 

int 
main(void) 
{ 
    printf("%ld%ld", (long)getpid(), (long)getppid()); 
    return 0; 
} 
5

使用getppid。請參閱man 2 getppidhere's linux手冊頁。

getppid()返回調用進程

相關問題