完成后叉子停止执行

时间:2015-09-28 20:35:52

标签: c linux unix fork

我正在尝试使用管道和分支在C中实现此命令。

    cat /proc/<pid>/limits | 
    grep "Max file size \|Max open files\|Max processes\|Max pending signals"

它处理整个命令。问题是,父案例导致执行在完成后停止。没有错误,没有警告。我猜这是一个管道/文件描述符问题。

代码:

char * command[3];
char str[25] = "";

strcat(str, "/proc/");
strcat(str, pid);
strcat(str, "/limits");

command[0] = "cat";
command[1] = str;
command[2] = NULL;

int fd2[2];
pipe(fd2);
pid_t pidlimits;

 switch (pidlimits = fork()) {
        case 0:
            //Execute cat /proc/$pid/limits here
            dup2(fd2[1], 1);
            close(fd2[0]);
            execvp(command[0], command);
            exit(1);

        default:
            dup2(fd2[0], 0);
            close(fd2[1]);

            //Build the grep command here
            char * command2[3];                
            char grepFilter[100] = "\"\\|Max file size\\|Max pending signals\\|Max processes\\|Max open files\\|\"";

            command2[0] = "grep";
            command2[1] = grepFilter;
            command2[2] = NULL;

            execvp(command2[0], command2);
            exit(1);
    }            
}

1 个答案:

答案 0 :(得分:-1)

看起来您可能有孩子(案例0 :)和父母(默认:)与您的意图相反。至少从示例here显示的内容 - child是管道读取器,parent是管道编写器。

相关问题