= 嘿嘿,我又来了 ^_^ =

C、C++和Java语言
回复
DRIFT
帖子: 177
注册时间: 2007-01-25 13:04
送出感谢: 0
接收感谢: 0

= 嘿嘿,我又来了 ^_^ =

#1

帖子 DRIFT » 2007-04-10 17:42

代码: 全选

#include<stdio.h>
#include<time.h>

/* 自定义函数 echo() */
int
echo(int num)
{	int a,b;
	if(num==0)
		return 0;
	if(fork()==0)
	{	a=time((time_t*)NULL);
		exit(0);
	}
	b=time((time_t*)NULL);
	wait(NULL);
	printf("Chile's return is %d\tParent's return is %d\n",a,b);
	num-=1;
	echo(num);
}

/* 主函数 */
int
main()
{	echo(10);
	return 0;
}
执行结果为
Chile's return is 0 Parent's return is 1176198212
Chile's return is 0 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 0 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 0 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 0 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 0 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 0 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 0 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 0 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 0 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212
Chile's return is 15956120 Parent's return is 1176198212


问题:
1- 我只递归调用了10次echo(),为什么会返回56次.

2- 为什么有时候返回的时间为"0".
dbzhang800
帖子: 3182
注册时间: 2006-03-10 15:10
来自: xi'an China
送出感谢: 0
接收感谢: 2 次
联系:

#2

帖子 dbzhang800 » 2007-04-10 19:50

注意一下fork的用法就好了
DRIFT
帖子: 177
注册时间: 2007-01-25 13:04
送出感谢: 0
接收感谢: 0

#3

帖子 DRIFT » 2007-04-11 13:08

dbzhang800 写了:注意一下fork的用法就好了
???
头像
laborer
帖子: 1016
注册时间: 2005-10-25 11:15
送出感谢: 0
接收感谢: 1 次
联系:

#4

帖子 laborer » 2007-04-11 13:24

fork用的没问题。不过你在父进程里面是不能直接访问子进程的变量a的。
hreiser@oakland:~$ killall -9 wife
police@oakland:~$ sudo find / -user hreiser
court@oakland:~$ sudo mv /home/hreiser /jail/
court@oakland:~$ sudo usermod -d /jail/hreiser -s "/usr/sbin/chroot /jail/" hreiser
头像
arthur
帖子: 76
注册时间: 2006-08-13 16:06
送出感谢: 0
接收感谢: 0

#5

帖子 arthur » 2007-04-13 10:50

通过fork创建的新进程会继承父进程的所有变量,不过那是克隆出来的,其实是两组不同的变量,新进程修改变量不会影响父进程的变量,父进程修改变量同样也不会影响子进程的变量。

楼主的代码在我的机器上编译是只递归10次呀。
DRIFT
帖子: 177
注册时间: 2007-01-25 13:04
送出感谢: 0
接收感谢: 0

#6

帖子 DRIFT » 2007-04-14 18:19

哦.原来是这样.

谢谢大家的帮助 :o
回复

回到 “C/C++/Java”