FD_CLOEXEC标志作用

通过fcntl设置设置FD_CLOEXEC作用:

close on exec, not on-fork  意思是如果对描述符设置了FD_CLOEXEC,使用execl执行的程序里,此描述符被关闭,此描述符不能再被使用.  但是在使用fork产生的子进程中此描述符不被关闭,仍然可以使用。

例如:

cat   fd_cloexec_flag_test.c

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
    int fd,pid;
    char buffer[20];
    /** 最好用open打开文件就设置了O_CLOEXEC代替用fctnl设置FD_CLOEXEC */
    //fd=open("wo.txt",O_RDONLY|O_CLOEXEC);
    fd=open("wo.txt",O_RDONLY);
    printf("%d\n",fd);
    int val=fcntl(fd,F_GETFD);
    val|=FD_CLOEXEC;
    fcntl(fd,F_SETFD,val);
    pid=fork();
    if(pid==0){
#if 1
      char child_buf[2];
      memset(child_buf,0,sizeof(child_buf) );
      ssize_t bytes = read(fd,child_buf,sizeof(child_buf)-1 );
      printf("child, bytes:%d,%s\n\n",bytes,child_buf);
#endif  
      char fd_str[5];
      memset(fd_str,0,sizeof(fd_str));
      sprintf(fd_str," %d\n",fd);
      int ret = execl("./exe1","exe1",fd_str,NULL);
      if(-1 == ret)
        perror("execl fail:");
    }        
   
    waitpid(pid,NULL,0);
    memset(buffer,0,sizeof(buffer) );
    ssize_t bytes = read(fd,buffer,sizeof(buffer)-1 );
    printf("parent, bytes:%d,%s\n\n",bytes,buffer);
}

 

cat  exe1.c

#include <fcntl.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>

int main(int argc, char **args)
{
    char buffer[20];
    int fd = atoi(args[1]);
    memset(buffer,0,sizeof(buffer) );
    ssize_t bytes = read(fd,buffer,sizeof(buffer)-1);
    if(bytes < 0) {
        perror("exe1: read fail:");
        return -1;
    }else {
        printf("exe1: read %d,%s\n\n",bytes,buffer);
    }
    return 0;
}

cat  wo.txt

this is a test

输出结果:

3
child, bytes:1,t

exe1: read fail:: Bad file descriptor
parent, bytes:14,his is a test

 

转载于:https://my.oschina.net/u/3725204/blog/1588593

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值