- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我对昨天提出的现有问题感到困惑:
Recursive piping in Unix again .
我重新发布有问题的代码:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
void pipeline( char * ar[], int pos, int in_fd);
void error_exit(const char*);
static int child = 0; /* whether it is a child process relative to main() */
int main(int argc, char * argv[]) {
if(argc < 2){
printf("Usage: %s option (option) ...\n", argv[0]);
exit(1);
}
pipeline(argv, 1, STDIN_FILENO);
return 0;
}
void error_exit(const char *kom){
perror(kom);
(child ? _exit : exit)(EXIT_FAILURE);
}
void pipeline(char *ar[], int pos, int in_fd){
if(ar[pos+1] == NULL){ /*last command */
if(in_fd != STDIN_FILENO){
if(dup2(in_fd, STDIN_FILENO) != -1)
close(in_fd); /*successfully redirected*/
else error_exit("dup2");
}
execlp(ar[pos], ar[pos], NULL);
error_exit("execlp last");
}
else{
int fd[2];
pid_t childpid;
if ((pipe(fd) == -1) || ((childpid = fork()) == -1)) {
error_exit("Failed to setup pipeline");
}
if (childpid == 0){ /* child executes current command */
child = 1;
close(fd[0]);
if (dup2(in_fd, STDIN_FILENO) == -1) /*read from in_fd */
perror("Failed to redirect stdin");
if (dup2(fd[1], STDOUT_FILENO) == -1) /*write to fd[1]*/
perror("Failed to redirect stdout");
else if ((close(fd[1]) == -1) || (close(in_fd) == - 1))
perror("Failed to close extra pipe descriptors");
else {
execlp(ar[pos], ar[pos], NULL);
error_exit("Failed to execlp");
}
}
close(fd[1]); /* parent executes the rest of commands */
close(in_fd);
pipeline(ar, pos+1, fd[0]);
}
}
发生的错误是:
Example:
./prog ls uniq sort head
gives:
sort: stat failed: -: Bad file descriptor
建议的解决方案是,“不要关闭子进程中的文件描述符 fd[1] 和 in_fd,因为它们已在父进程中关闭。”
我的困惑:(对不起,我是 Linux 新手)
根据我的书“开始 Linux 编程”,当我们 fork() 一个进程时,文件描述符也会被复制。因此, parent 和 child 应该有不同的文件描述符。这与答案相矛盾。
我的尝试:
我尝试自己运行这段代码,发现只有在关闭两个进程(父进程和子进程)中的“in_fd”文件描述符时才会出现问题。它不依赖于 fd[1]。
另外,有趣的是,如果我尝试 ./prog ls sort head
它工作正常,但是当我尝试 ./prog ls sort head uniq
它在 上给出读取错误>头
。
我的想法:in_fd
文件描述符只是此函数的输入 int 变量。似乎即使在 fork 之后,也只剩下一个文件描述符被父子共享。但我无法理解如何。
最佳答案
when we fork() a process, then the file descriptors are also duplicated. Hence the parent and child should have different file descriptors
文件描述符是一个简单的整数
。所以当它被复制时,它具有相同的值,所以它们指向同一个文件。
所以你可以在父级打开一个文件,并从子级访问它。唯一可能发生的问题是,如果同时从父文件和子文件访问文件,在这种情况下,无法保证将从文件的哪个位置访问。为避免这种情况,建议关闭 child 中的 fd 并重新打开。
正如您所说的尝试,我对上述问题做了同样的事情,发现第 4 个命令总是出现这种情况。此外,dup2()
关闭它正在复制的文件。在问题中,fd[1]
和 in_fd
被复制到 child 的 stdin
和 stdout
。 fd[1]
和 in_fd
在那一刻被关闭了。无需再次关闭它们。
关闭一个已经关闭的描述符会导致错误。
并且由于您不知道是父文件还是子文件先执行,如果您从子文件中关闭一个文件,然后再次从父文件中关闭,可能会导致问题,并且这种行为是不可预测的。
关于c - Pipe() with fork() with recursion : File Descriptors handling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21332970/
我有一个用于查找存储设备序列号的内核驱动程序,但该驱动程序存在问题。Descriptor->SerialNumberOffset 为 103但是 (LPCSTR)(UINT_PTR)Descripto
在我的程序中,每当我用导致无法检测到 ORB 功能的东西覆盖相机时,程序就会崩溃并出现错误: OpenCV Error: Assertion failed (type == src2.type() &
定义 通常,一个 descriptor 是具有“绑定行为”的对象属性。所绑定行为可通过 descriptor 协议被自定义的 __get__() , __set__() 和 __delete__(
如 normaluser : $ ulimit -n 4096 -bash: ulimit: open files: cannot modify limit: Operation not permit
我正在尝试在elasticsearch中安装ik分析,ik源来自以下位置: GitHub 我的步骤来自自述文件和来自互联网的一些资料 cd elasticsearch-analysis-ik mvn
我有以下代码: int fds[2]; if (pipe(fds) < 0) { fprintf(stderr, "ERROR, unable to open pipe: %s\n", str
我在 C 中有一个简单的生产者消费者程序,尝试用 fork 解决它当生产者试图在管道上写入时,我得到了错误:我用相同的逻辑编写了另一个程序,但这个程序没有给我任何线索,让我知道为什么? 生产者无法在管
我很难理解 FREAK 描述符中的参数 orientationNormalized 和 scaleNormalized。知道它们的意思或作用吗? OpenCV FREAK 文档:http://docs
我在做的事情是否符合通用设计模式?如果有,名字是什么? 我有一个复杂对象,它具有“简单”字段,例如字符串和字符串列表,以及其他复杂对象。我想将此对象的实例添加到 JMS 消息队列中,这意味着它们需要是
在例子中: event.events = EPOLLIN; event.data.fd = fd; int ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, event
最近,我的 Crashlytics 和 Apple 崩溃日志收到了许多崩溃信息 -[CTTelephonyNetworkInfo updateRat:descriptor:]在没有太多其他信息的情况下
是否有可能将N个文件描述符作为一个文件描述符显示给程序,以便在N个文件描述符(即从N个套接字)中接收的数据将被转发回单个文件描述符上的调用API,从而隐藏它实际上可能来自不同的文件描述符的事实吗?是否
网络编程菜鸟在这里, 我对accept和connect套接字函数的行为感到困惑。在大多数编程语言中,这些函数的包装返回不同类型的值:accept返回可用于发送/接收数据的新描述符,但是connect不
我正在尝试启动resque-web,但是会发生此错误: [Sun Mar 06 05:27:48 +0000 2011]启动“resque-web” ... [Sun Mar 06 05:27:48
我有一个项目,其中有几个为程序集插件编写的自定义描述符。有没有办法一次只运行其中一个描述符而不是整个描述符?我尝试使用文档中的描述符开关 here ,传递到我想要运行的一个描述符的完整路径,但它正在运
我正在尝试学习 POSIX 中的基本 IO 函数,我编写了以下代码,但它不起作用,并且在我尝试执行代码时返回“Bad file descriptor”错误: #include #include #
编辑:最小、完整和可验证的示例位于下面的注释中,代码实际上有效,问题出在不同的区域。抱歉,发帖错误,我现在无法删除它。 我知道,已经有一些关于此的页面,但我确实尝试了所有方法,但没有任何效果。我一直遇
#define MAX 2 int main(){ int mutex = semget(ftok("/usr",'P'),1,IPC_CREAT|0666); int wrt = s
我正在尝试实现一个 simpel c shell,它将通过管道传输任意数量的命令。这是相关的 for 循环: int status; int i,j,inputFile,outputFile,pid;
简介 我典型的swig接口(interface)文件类似如下: %{ //COPY THIS BLOCK AS IS #include static CppClass* get_Cp
我是一名优秀的程序员,十分优秀!