我对 Linux 和相关技术非常陌生,无法让脚本正常运行。我猜测程序在执行 tr 函数时被挂起了。
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
int pdesc[2];
pipe(pdesc);
int a = fork();
if (a == 0) // child
{
dup2(pdesc[1],1); // chaning std_out to pipes_out
execlp("ls", "ls", "-l", "-a", NULL);
}
else //parent
{
wait();
int file1 = open("file.txt", O_WRONLY|O_CREAT|O_TRUNC,0777);
dup2(pdesc[0], 0); // chaning std_in to pipes_in
dup2(file1, 1); // chaning std_out to file's stream
execlp("tr", "tr", "a-z", "A-Z", NULL);
}
return 0;
}
NULL
指针终止,并且由于这些是可变参数函数,因此此指针必须转换为(char *) NULL
。"? - Daniel Fischerumask
可能会解决这个问题)。 - Jonathan Leffler