我对 execlp 有疑问。当我不知道如何将命令从指针数组正确重定向到 execlp 时。例如我想使用
ls -l | sort -n
我的程序只需要“ls”和“sort”
int pfds[2];
pipe(pfds);
child_pid = fork();
if(child_pid==0)
{
close(1);
dup(pfds[1]);
close(pfds[0]);
execlp(*arg1, NULL);
}
else
{
wait(&child_status);
close(0);
dup(pfds[0]);
close(pfds[1]);
execlp(*arg2, NULL);
}
所有命令都在指针数组中,其中:ls -l
在第一个表中,sort -n
在第二个表中