理解dup、dup和fcntl

本文详细介绍了C语言中dup和dup2函数的作用,通过实例展示了如何使用这些函数来改变文件句柄引用的目标文件,并讨论了在使用fcntl设置exec时close的属性时的细节。

/dup函数的作用:复制一个现有的句柄,产生一个与“源句柄特性”完全一样的新句柄(也即生成一个新的句柄号,并关联到同一个设备)
//dup2函数的作用:复制一个现有的句柄到另一个句柄上,目标句柄的特性与“源句柄特性”完全一样(也即首先关闭目标句柄,与设备断连,接着从源句柄完全拷贝复制到目标句柄)
//dup和dup2都是系统服务,window平台对应DuplicateHandle函数
/* DUP.C: This program uses the variable old to save
 * the original stdout. It then opens a new file named
 * new and forces stdout to refer to it. Finally, it
 * restores stdout to its original state.
 */

#i nclude <io.h>
#i nclude <stdlib.h>
#i nclude <stdio.h>

void main( void )
{
   int old;
   FILE *new;

   old = _dup( 1 );   /* "old" now refers to "stdout" */
                      /* Note:  file handle 1 == "stdout" */
   if( old == -1 )
   {
      perror( "_dup( 1 ) failure" );
      exit( 1 );
   }
   write( old, "This goes to stdout first/r/n", 27 );
   if( ( new = fopen( "data", "w" ) ) == NULL )
   {
      puts( "Can't open file 'data'/n" );
      exit( 1 );
   }

   /* stdout now refers to file "data" */
   if( -1 == _dup2( _fileno( new ), 1 ) )
   {
      perror( "Can't _dup2 stdout" );
      exit( 1 );
   }
   puts( "This goes to file 'data'/r/n" );

   /* Flush stdout stream buffer so it goes to correct file */
   fflush( stdout );
   fclose( new );

   /* Restore original stdout */
   _dup2( old, 1 );
   puts( "This goes to stdout/n" );
   puts( "The file 'data' contains:" );
   system( "type data" );
}

Output

This goes to stdout first
This goes to file 'data'

This goes to stdout

The file 'data' contains:

This goes to file 'data'

关于fcntl(fd, F_SETFD, FD_CLOEXEC)设置exec时close的属性

snd_ctl_hw_open
#define SNDRV_FILE_CONTROL    ALSA_DEVICE_DIRECTORY "controlC%i"
sprintf(filename, SNDRV_FILE_CONTROL, card); // 路径/dev/snd/controlC0
fd = snd_open_device(filename, fmode);
fcntl(fd, F_SETFD, FD_CLOEXEC); // 这里设置为FD_CLOEXEC表示当程序执行exec函数时本fd将被系统自动关闭,表示不传递给exec创建的新进程, 如果设置为fcntl(fd, F_SETFD, 0);那么本fd将保持打开状态复制到exec创建的新进程中[luther.gliethttp].
进入内核系统调用
sys_fcntl
do_fcntl
    case F_SETFD:
        err = 0;
        set_close_on_exec(fd, arg & FD_CLOEXEC);

void fastcall set_close_on_exec(unsigned int fd, int flag)
{
    struct files_struct *files = current->files;
    struct fdtable *fdt;
    spin_lock(&files->file_lock);
    fdt = files_fdtable(files);
    if (flag)
        FD_SET(fd, fdt->close_on_exec);
    else
        FD_CLR(fd, fdt->close_on_exec);
    spin_unlock(&files->file_lock);
}
下面是man fcntl看到的对FD_CLOEXEC解释

File descriptor flags
   The  following  commands manipulate the flags associated with a file descriptor.  Currently, only one such flag is
   defined: FD_CLOEXEC, the close-on-exec flag.  If the FD_CLOEXEC bit is 0, the file  descriptor  will  remain  open
   across an execve(2), otherwise it will be closed.

   F_GETFD (void)
          Read the file descriptor flags; arg is ignored.
   F_SETFD (long)
          Set the file descriptor flags to the value specified by arg.

int CWatchDogDaemon::Fork(void)

{

// an error detection pipe

int err, fd[2];

pipe(fd);

 

// fork child process

pid = fork();

if (pid == -1)

return pid;

 

if (pid == 0)

{

// close pipe if exec succ

close(fd[0]);

fcntl(fd[1], F_SETFD, FD_CLOEXEC);

 

Exec();

 

err = errno;

log_error("%s: exec(): %m", name);

write(fd[1], &err, sizeof(err));

exit(-1);

}

 

close(fd[1]);

if(read(fd[0], &err, sizeof(err))==sizeof(err))

{

errno = err;

return -1;

}

close(fd[0]);

 

AttachWatchDog();

return pid;

}

初一看会以为有问题,觉得由于read阻塞读写,父进程无论怎么样都会读到一个字节导致父进程会退出。

最后发现是下面这行代码在起作用:

fcntl(fd[1], F_SETFD, FD_CLOEXEC);

设置该标志会使得进程调用exec相关接口时如果成功会关闭管道,关闭管道时会向管理写一个字节的结束标志。这样read就会返回1导致条件不成立,反之则不关闭管道父进程就能正确得到子进程执行失败的消息。

dupdup2是UnixLinux系统中的系统调用函数,用于复制文件描述符。 dup函数的原型为: ```c int dup(int oldfd); ``` dup2函数的原型为: ```c int dup2(int oldfd, int newfd); ``` 这两个函数的作用是将一个已有的文件描述符复制到另一个文件描述符中。 - dup函数会返回一个新的文件描述符,该描述符与参数oldfd指向同一文件表项。这意味着对返回的文件描述符的读写操作会影响到原来的文件描述符。 - dup2函数也会返回一个新的文件描述符,但是它可以指定新的文件描述符newfd。如果newfd已经被使用,那么dup2函数会先将newfd关闭,然后再复制oldfd,使得新的文件描述符与oldfd指向同一文件表项。 这两个函数通常用于重定向标准输入、输出错误流。例如,可以使用dup2函数将标准输出重定向到一个文件中: ```c #include <unistd.h> #include <fcntl.h> int main() { int fd = open("output.txt", O_WRONLY | O_CREAT, 0644); if (fd == -1) { perror("open"); return 1; } // 将标准输出重定向到文件 if (dup2(fd, STDOUT_FILENO) == -1) { perror("dup2"); return 1; } // 打印到标准输出,实际上会写入文件 printf("Hello, world!\n"); close(fd); return 0; } ``` 在上面的示例中,dup2函数将文件描述符fd复制到标准输出描述符STDOUT_FILENO中,这样所有的printf函数调用都会将内容写入文件"output.txt"中。 希望这个解答能够帮到你!如果还有其他问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值