看板 Grad-ProbAsk 關於我們 聯絡資訊
以下是程式碼 ------------------------------------------- #include<stdio.h> #include<stdlib.h> int value = 5; int main() { pid_t pid; pid = fork(); if(pid == 0){ value +=15; printf("A:value = %d\n",value); printf("I am child.my pid = %d\n",getpid()); printf("OB father pid is,getppid()); } else if(pid > 0){ printf("B:value = %d\n",value); printf("I am parent.my pid = %d\n,getpid()); } else printf("fork error\n"); } ------------------------------------------- 程式結果: B:value = 5 I am parent.my pid = 30537 A:value = 20 I am child.my pid = 30545 my father pid is 1 ------------------------------------------- 以上是我跑在ubuntu上結果 查了一下 getpid()取得該程式pid getppid()則是取得parent pid 可是為什麼print出來的結果 兒子那邊show出來的pid跟 爸爸那邊的pid 不一樣阿? 根據show出來的結果 難道getppid()是相對位置= = ? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.41.129.247
s90413k64:因為老爸死了 所以parent變init 你叫parent wait看看 11/11 21:33
SiriusCloud:感謝!! 已解決~ 11/11 22:18