Chinaunix首页 | 论坛 | 博客
  • 博客访问: 69462
  • 博文数量: 24
  • 博客积分: 50
  • 博客等级: 民兵
  • 技术积分: 135
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-24 22:12
文章分类

全部博文(24)

文章存档

2013年(24)

我的朋友

分类: LINUX

2013-04-07 20:07:46


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <dirent.h>
  5. #include <fcntl.h>

  6. int main(void)
  7. {
  8.     DIR *dp;
  9.     struct dirent *dirp;
  10.     int fd;
  11.     int val;
  12.     pid_t pid;
  13.     char fdstr[10];
  14.     dp = opendir("/");
  15.     fd = dirfd(dp);  /*注意函数*/
  16.     val = fcntl(fd, F_GETFD, 0);
  17.     if (val)
  18.         printf("close-on-exec is on in test\n");
  19.     else
  20.         printf("close-on-exec is off in test\n");
  21.     sprintf(fdstr, "%d\0", fd);
  22.     if ((pid = fork()) < 0) {
  23.         printf("fork error\n");
  24.         exit(1);
  25.     } else if (pid == 0) {
  26.         execl("/home/shyan/study/test/child", "child", fdstr, (char *)0);
  27.         exit(0);
  28.     }
  29.     exit(0);
  30. }
child程序代码如下:

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>

  4. int main(int argc, char *argv[])
  5. {
  6.     int val;
  7.     int fd;
  8.     sscanf(argv[1], "%d", &fd);
  9.     if ((val = fcntl(fd, F_GETFD, 0)) < 0) {
  10.         perror("fcntl error");
  11.         exit(1);
  12.     }
  13.     if (val)
  14.         printf("close-on-exec is off\n");
  15.     else
  16.         printf("close-on-exec if off\n");
  17.     exit(0);
  18. }


阅读(1396) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~