当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 6 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : = 嘿嘿,我又来了 ^_^ =
帖子发表于 : 2007-04-10 17:42 

注册: 2007-01-25 13:04
帖子: 177
送出感谢: 0 次
接收感谢: 0 次
代码:
#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".


页首
 用户资料  
 
2 楼 
 文章标题 :
帖子发表于 : 2007-04-10 19:50 

注册: 2006-03-10 15:10
帖子: 3182
地址: xi'an China
送出感谢: 0 次
接收感谢: 2
注意一下fork的用法就好了


页首
 用户资料  
 
3 楼 
 文章标题 :
帖子发表于 : 2007-04-11 13:08 

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


???


页首
 用户资料  
 
4 楼 
 文章标题 :
帖子发表于 : 2007-04-11 13:24 
头像

注册: 2005-10-25 11:15
帖子: 1016
送出感谢: 0 次
接收感谢: 1
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


页首
 用户资料  
 
5 楼 
 文章标题 :
帖子发表于 : 2007-04-13 10:50 
头像

注册: 2006-08-13 16:06
帖子: 76
送出感谢: 0 次
接收感谢: 0 次
通过fork创建的新进程会继承父进程的所有变量,不过那是克隆出来的,其实是两组不同的变量,新进程修改变量不会影响父进程的变量,父进程修改变量同样也不会影响子进程的变量。

楼主的代码在我的机器上编译是只递归10次呀。


页首
 用户资料  
 
6 楼 
 文章标题 :
帖子发表于 : 2007-04-14 18:19 

注册: 2007-01-25 13:04
帖子: 177
送出感谢: 0 次
接收感谢: 0 次
哦.原来是这样.

谢谢大家的帮助 :o


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 6 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:没有注册用户 和 4 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译