对比windows和linux的对父进程的文件描述符继承的设置

本文探讨了在Windows和Linux中,如何处理子进程继承父进程的文件描述符(句柄)的问题。在Linux中,子进程默认继承文件描述符,而Windows则不然。在Linux中,可以通过glibc的fopen使用'e'标志来设置O_CLOEXEC,但在旧版本中可能需要结合open和fdopen。而在Windows中,需要在创建句柄时设置继承属性,并在创建子进程时允许继承。
摘要由CSDN通过智能技术生成

需要实现一个fopen中的子进程是否继承父进程的文件句柄的功能。

由于在多平台上,所以需要考虑windows和Linux及类Unix系统。

Linux实现的阻碍:

Linux中是默认子进程能够继承fd的。

1.由于需要在fopen函数中实现,但是看了下关于O_CLOEXEC属性(since Linux 2.6.23)是在open函数中的。

其含义就是在获取新的文件描述符时,使能close-on-exec flag,即使得在调用exec函数集时主动预先关闭父进程的文件描述符。

这在open中是一个Flag,也就是说是一个int型值。

2.正由于open中是一个int型值,而其他的文件属性相关的大部分在fopen都是rw等以char*形式提供的mode。所以出现了不好统一的的情况。

汇集一点:open中提供了对应的int型的flag,但是我们要打开的是文件需要调用fopen,fopen又没有提供int型的设置参数,只有字符串类型的mode作为设置参数。

解决办法:

还是查资料,发现了在glibc中,fopen已经提供了类似的参数在mode中,以字符'e'表示O_CLOEXEC flag。但是这里有一个限制,就是必须要是glibc 2.7版本以后才会有这种解析。如果是之前的版本,就只好先用open接口加上O_CLOEXEC的flag做为参数获取到fd,然后再利用fdopen,来进行mode的设置。


windows的实现阻碍:

windows中是默认子进程能够不能继承fd的,因为要提前设置好条件。

其实关键在于,这个实现的有没有意义。因为下面的资料上也有提及,如果要继承父进程的句柄,则需要有两步

1.在创建获取句柄(类似于文件描述符)的时候,就要明确在属性中设置是可以用于子进程继承的。

2.在创建子子进程时,是否允许继承的参数就要设置成true,否则仍然不能继承。


摘自:http://man7.org/linux/man-pages/man3/fopen.3.htm

NAME         top

       fopen, fdopen, freopen - stream open functions

SYNOPSIS         top

       #include <stdio.h>

       FILE *fopen(const char *path, const char *mode);

       FILE *fdopen(int fd, const char *mode);

       FILE *freopen(const char *path, const char *mode, FILE *stream);

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       fdopen(): _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE

DESCRIPTION         top

       The fopen() function opens the file whose name is the string pointed
       to by path and associates a stream with it.

       The argument mode points to a string beginning with one of the
       following sequences (possibly followed by additional characters, as
       described below):

       r      Open text file for reading.  The stream is positioned at the
              beginning of the file.

       r+     Open for reading and writing.  The stream is positioned at the
              beginning of the file.

       w      Truncate file to zero length or create text file for writing.
              The stream is positioned at the beginning of the file.

       w+     Open for reading and writing.  The file is created if it does
              not exist, otherwise it is truncated.  The stream is
              positioned at the beginning of the file.

       a      Open for appending (writing at end of file).  The file is
              created if it does not exist.  The stream is positioned at the
              end of the file.

       a+     Open for reading and appending (writing at end of file).  The
              file is created if it does not exist.  The initial file
              position for reading is at the beginning of the file, but
              output is always appended to the end of the file.

       The mode string can also include the letter 'b' either as a last
       character or as a character between the characters in any of the two-
       character strings described above.  This is strictly for
       compatibility with C89 and has no effect; the 'b' is ignored on all
       POSIX conforming systems, including Linux.  (Other systems may treat
       text files and binary files differently, and adding the 'b' may be a
       good idea if you do I/O to a binary file and expect that your program
       may be ported to non-UNIX environments.)

       See NOTES below for details of glibc extensions for mode.

       Any created files will have mode 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值