2015-02-10 87 views
0
#include <stdio.h> 
#include <unistd.h> 

int main() 
{ 
    int id; 
    printf("here comes the date.\n"); 
    if (id = fork() == 0) { 
     printf(「%d」, id); 
     printf ("PID is %d and ID is %d\n", getpid(),id); 
     execl ("/bin/date", "date", 0); 
    } 
    printf ("that was the date.\n"); 
} 

OUTPUT:爲什麼id變量1的輸出?

here comes the date. 
that was the date. 
PID is 1414 and ID is 1 
Tue Feb 10 14:03:02 PST 2015 
+0

這是因爲[運算符優先級](http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm):'='的優先級低於'==' – whoan 2015-02-10 22:34:53

回答

0

由於要設置它等於fork() == 0其結果是邏輯測試。

分叉將在分叉線程內成功(返回零)。外螺紋將具有PID。

+0

謝謝,這是有道理的。我認爲它應該是零,因爲這是fork()在孩子中返回的內容。感謝你的幫助! – 2015-02-10 22:39:06

+0

任何時間。 :)請確認後請接受答案。 – 2015-02-10 22:41:10