方法:輸出結果的重定向
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int fd;
fd=open("ps.out",O_WRONLY|O_CREAT|O_TRUNC,0644);
if(fd<0)
{
perror("open ps.out error");
exit(1);
}
dup2(fd,STDOUT_FILENO);//將stdout的文件描述符(值爲1)指針指向fd,即實現輸出的重定向
execlp("ps","ps","ax",NULL);
return 0;
}
此時,系統的進程信息打印到ps.out文件中