免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3810 | 回复: 17
打印 上一主题 下一主题

<<APUE>>--Exercises 3.2 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-05-18 20:40 |只看该作者 |正序浏览
3.2
Write your own function called dup2 that performs the same service as
the dup2 function we described in section 3.12,without calling the
fcntl function. Be sure to handle errors correctly.


  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <unistd.h>
  4. int
  5. my_dup2(int oldd,int newd)
  6. {
  7.     int fd;
  8.     if(oldd==newd){
  9.         return (-1==(fd=dup(oldd)))?-1:close(fd),oldd;
  10.     }else{
  11.         if( (-1==lseek(newd,0,SEEK_CUR)) && (errno==EBADF) ){
  12.             close(newd);
  13.         }
  14.         if(newd != (fd=dup(oldd))){
  15.             if(fd ==  -1){
  16.                 return -1;
  17.             }else{
  18.                 int fd1;
  19.                 if(newd != (fd1=my_dup2(oldd,newd))){
  20.                     close(fd1);
  21.                 }
  22.             }
  23.         }
  24.     }
  25.     return fd;
  26. }


  27. int
  28. main(void)
  29. {
  30.     int fd;
  31.     if(-1==(fd=my_dup2(1,5))){
  32.         perror("5555555");
  33.         exit(-1);
  34.     }
  35.     write(5,"hahahahahahahaha",17);

  36.     exit(0);
  37. }
复制代码




请问这样写的my_dup2有问题吗?

[ 本帖最后由 雨丝风片 于 2006-5-21 09:03 编辑 ]

论坛徽章:
0
18 [报告]
发表于 2006-05-20 14:09 |只看该作者
哦!我找找)

论坛徽章:
0
17 [报告]
发表于 2006-05-20 12:37 |只看该作者
原帖由 balabalacha 于 2006-5-20 11:46 发表
还没学到那呢,现在正在看第4章,谢谢各位的指导


结合《FreeBSD操作系统设计与实现》一起看。

论坛徽章:
0
16 [报告]
发表于 2006-05-20 11:46 |只看该作者
原帖由 gvim 于 2006-5-20 11:07 发表
看这个逻辑

fork()  --->  fdinit()  ---> newfd = malloc(......)  --->  初始化一些东西。

可以看出来每个进程自己分配的有空间存放这些东西。

当然描述符表还有这两个操作,fdcopy()/fdshar ...



还没学到那呢,现在正在看第4章,谢谢各位的指导

论坛徽章:
2
亥猪
日期:2014-03-19 16:36:35午马
日期:2014-11-23 23:48:46
15 [报告]
发表于 2006-05-20 11:07 |只看该作者
看这个逻辑

fork()  --->  fdinit()  ---> newfd = malloc(......)  --->  初始化一些东西。

可以看出来每个进程自己分配的有空间存放这些东西。

当然描述符表还有这两个操作,fdcopy()/fdshare(),分别对应拷贝,共享,上面那个比较简单,意思是关闭fd,fd table清0。

论坛徽章:
0
14 [报告]
发表于 2006-05-20 10:59 |只看该作者
真要命,赶紧弄懂

every process has an entry in the process table
在process table内的process table entry都有一个只属于自己的在自己进程打开的file descriptors的表,不管对这个表内的fd如何,并不影响process table中的其它的process table entry

是这样吗

论坛徽章:
2
亥猪
日期:2014-03-19 16:36:35午马
日期:2014-11-23 23:48:46
13 [报告]
发表于 2006-05-20 10:18 |只看该作者
就像num 1 文件符,你dup标准输出之后在自己的进程里关闭它,并不影响其他进程如父层的shell继续输出。

论坛徽章:
0
12 [报告]
发表于 2006-05-20 10:15 |只看该作者
原帖由 balabalacha 于 2006-5-20 09:58 发表
另外,我还想到:
以7楼的代码为例(我的是错的*^_^*),假设运气好,在这儿成功

  1. assert(fd == newfd); /* reasonable */
  2.                 return fd;
复制代码

而在此之前还经过了N次递归过程,现在由 ...


每个进程都有自己独立的文件描述符空间,互不影响。

论坛徽章:
0
11 [报告]
发表于 2006-05-20 09:58 |只看该作者
另外,我还想到:
以7楼的代码为例(我的是错的*^_^*),假设运气好,在这儿成功

  1. assert(fd == newfd); /* reasonable */
  2.                 return fd;
复制代码

而在此之前还经过了N次递归过程,现在由于分到了newfd,程序开始回归,假设回归过程时,另一个进程也使用了dup2来分配newfd,那会造成第一个进程分的newfd被释放(有可能,假设oldd!=newfd),但第一个进程并不知情,仍在回归,直到返回到调用进程,此时调用进程就会出问题

我说的对吗?

论坛徽章:
0
10 [报告]
发表于 2006-05-20 09:36 |只看该作者


我看了一下dup2.c是调用fcntl的,而fcntl是_fcntl的,
里面好似.........看木懂  --.--

  1. case F_DUPFD:
  2.                         /*
  3.                          * Get the file descriptor that the caller wants to
  4.                          * use:
  5.                          */
  6.                         oldfd = va_arg(ap, int);

  7.                         /* Initialise the file descriptor table entry: */
  8.                         if ((ret = __sys_fcntl(fd, cmd, oldfd)) < 0) {
  9.                         }
  10.                         /* Initialise the file descriptor table entry: */
  11.                         else if (_thread_fd_table_init(ret) != 0) {
  12.                                 /* Quietly close the file: */
  13.                                 __sys_close(ret);

  14.                                 /* Reset the file descriptor: */
  15.                                 ret = -1;
  16.                         } else {
  17.                                 /*
  18.                                  * Save the file open flags so that they can
  19.                                  * be checked later:
  20.                                  */
  21.                                 _thread_fd_setflags(ret,
  22.                                     _thread_fd_getflags(fd));
  23.                         }
  24.                         break;

复制代码
  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP