getppid(取得父進程的進程識別碼)
基本介紹
簡介,執行結果,
簡介
相關函式 fork,kill,getpid
表頭檔案 #include<unistd.h>
定義函式 pid_t getppid(void);
函式說明 getppid()用來取得目前進程的父進程識別碼。
返回值 目前進程的父進程識別碼。
範例1
#include<unistd.h>
main()
{
printf(“My parent ‘pid =%d\n”,getppid());
}
執行
My parent ‘pid =463
範例2
#include <sys/types.h>
#include <unistd.h>
int main(){
pid_t pid=fork();
if(pid==0){
printf("\ni am child\n");
printf("My process id is %d\n",getpid());
printf("My Parents's process id is %d\n",getppid());
}
else{
printf("i am parent\n");
printf("My process id is %d\n",getpid());
printf("My parents's process id is %d\n",getppid());
sleep(1);}}
執行結果
i am parent
My process id is 220
My parents's process id is 119
[root@linteng process]
#i am child
My process id is 221
My Parents's process id is220