Re: [問題] 可以幫我在linux跑跑看嗎= =

看板C_and_CPP作者 (妳曾經是我的女孩...)時間15年前 (2009/04/20 17:39), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《NDHUIB (NDHUIB)》之銘言: : 如題... : 恐龍書的作業有一題要用fork()來做費伯納西數列 : 可是我電腦只有XP= = (已經是六年前的筆電了... 不太適合再裝OS操它= =) : 可以請哪位版友幫我跑跑看這個program能不能用嗎? : #include <sys/types.h> : #include <stdio.h> : #include <unistd.h> : int main() : { : int a=0, b=1, n=a+b,i,ii; : pid_t pid; : printf("Enter the number of a Fibonacci Sequence:\n"); : scanf("%d", &ii); : if (ii < 0) : printf("Please enter a non-negative integer!\n"); : else : { : pid = fork(); : if (pid == 0) : { : printf("Child is producing the Fibonacci Sequence...\n"); : printf("%d %d",a,b); : for (i=0;i<ii;i++) : { : n=a+b; : printf("%d ", n); : a=b; : b=n; : } : printf("Child ends\n"); : } : else : { : printf("Parent is waiting for child to complete...\n"); : wait(NULL); : printf("Parent ends\n"); : } : } : return 0; : } 很抱歉剛剛我的推文說錯了. 結果是: [root@hep-fc8 test]# ./test.exe Enter the number of a Fibonacci Sequence: 5 Child is producing the Fibonacci Sequence... 0 11 2 3 5 8 Child ends Parent is waiting for child to complete... Parent ends 所以還是有 Child ends, 最後才有Parent ends -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.109.103.226

04/20 17:48, , 1F
嗯~~這樣就沒錯了~ 非常謝謝你<(_ _)>
04/20 17:48, 1F
文章代碼(AID): #19x4BZON (C_and_CPP)
文章代碼(AID): #19x4BZON (C_and_CPP)