我正在尝试使用管道和分支在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);
}
}