Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
shell关闭文件描述符num 19_Shell_Unix_Exec_Fork_File Descriptor - Fatal编程技术网

shell关闭文件描述符num 19

shell关闭文件描述符num 19,shell,unix,exec,fork,file-descriptor,Shell,Unix,Exec,Fork,File Descriptor,调试我的应用程序时,我发现shell解释器有奇怪的行为(Solaris上为/bin/sh,Debian中为/bin/dash)。而shell文件描述符中按数字19(dec)进行的fork()调用被shell关闭。在我的例子中,它导致进程之间的通信套接字对关闭 查看shell源代码,我发现了以下内容: 为简洁起见: /* used for input and output of shell */ #define INIO 19 及 因此,netto只是结束FD编号INIO(

调试我的应用程序时,我发现shell解释器有奇怪的行为(Solaris上为/bin/sh,Debian中为/bin/dash)。而shell文件描述符中按数字19(dec)进行的fork()调用被shell关闭。在我的例子中,它导致进程之间的通信套接字对关闭

查看shell源代码,我发现了以下内容:

为简洁起见:

/* used for input and output of shell */
#define     INIO        19

因此,netto只是结束FD编号INIO(19)

复制的简单测试:

$ exec 19>&1
$ echo aaa >&19
aaa
$ bash -c 'echo aaa >&19'
aaa
$ dash -c 'echo aaa >&19'
dash: 1: Syntax error: Bad fd number
$ ksh -c 'echo aaa >&19'
aaa
问题是:

  • 这种奇怪行为的原因是什么
  • 文件描述符19有什么问题
19是特殊的,因为(很久以前),打开的文件的最大数量是20个,例如

#define _NFILE  20
stdio.h

在POSIX中,您可以通过界面看到其他符号,如
OPEN_MAX

  • 文件描述符从0开始计数,并且
  • 通常按递增顺序分配
  • 所以“最后可能”的文件描述符应该是19
  • 如果有一个未使用的文件描述符,使它最后一个将“起作用”

Solaris sh(特别是通过Solaris 10)和可追溯到很久以前,您注意到的细节可能没有破坏任何重要的遗留shell脚本。

作为一个简单的实验,您是否尝试过将“19”更改为其他(“22”)?这至少可以检测出19是不是特别的。在solaris中(如代码所示),唯一的魔力是19。在Debian dash中,任何文件描述符GT19(20也适用)。
#define _NFILE  20