作者NDHUIB (NDHUIB)
站内C_and_CPP
标题[问题] 可以帮我在linux跑跑看吗= =
时间Mon Apr 20 17:07:28 2009
如题...
恐龙书的作业有一题要用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;
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 61.231.192.22
1F:推 flamerecca:可以写成.c档让人下载吗 这样要剪剪贴贴有点麻烦XD 04/20 17:09
2F:→ netsphere:我还以为是第n个child 代表第n项~ 04/20 17:11
4F:→ NDHUIB:书上题目是说输入n 列出数列前n项~ 所以才这样啦~ 04/20 17:16
5F:→ netsphere:其实你可以奘 Pthread library 阿 一样可以在win跑 04/20 17:16
6F:推 FlyBrother:Parent ends(这是结果) 04/20 17:19
7F:推 sitos:fork 跟 pthread_create 不太一样吧? 04/20 17:19
8F:→ sitos:fork 会变成两个 process 但是 pthread 的 pid 是一样的? 04/20 17:19
9F:→ FlyBrother:用Linux跑的 04/20 17:19
10F:→ FlyBrother:我好像搞错了....^^", 可以编译过啦... 也可以跑 04/20 17:21
11F:→ NDHUIB:因为题目指定要用fork() 所以只好拜托大家帮忙啦~ 04/20 17:21
12F:→ NDHUIB:请问FlyBrother 输入5 会跑出什麽结果呢? 04/20 17:22
13F:推 FlyBrother:还是Parent ends 04/20 17:22
14F:→ weiyucsie:突然想到cygwin XD 04/20 17:24
15F:→ sitos:其实我觉得直接用 VirtualBox 装一个 linux 就好了 04/20 17:25
16F:→ sitos:以後用到的机会还很多 04/20 17:25
17F:→ NDHUIB:F大~~ 你是说输入数字完之後只会有一行"Parent ends"吗? 04/20 17:28
18F:→ NDHUIB:没有数列和其他字出来吗= =? 04/20 17:28
19F:推 FlyBrother:有一大堆数字耶. 04/20 17:34
20F:→ FlyBrother:我输入100000,5000,200,1000都是一样的结果 04/20 17:35
21F:→ NDHUIB:OK~~那就谢谢你啦! 04/20 17:35
22F:→ FlyBrother:一堆数字+Parent ends 04/20 17:35
23F:→ weiyucsie:因为他写六年前的笔电阿XD 04/20 18:03
24F:→ sitos:其实 VM 也不会太操阿,一次做一件事应该是还好 04/20 18:07
25F:→ james732:最近VM装了Fedora10 只有文字介面与开发环境 也挺省的 04/20 18:28