Re: [問題] 可以幫我在linux跑跑看嗎= =
※ 引述《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
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):