/*
* function: 使用exec函数族中的函数,执行ps -ef命令,并将结果输出到文件中。
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
int fd = open("./a.txt", O_RDWR | O_CREAT, 0644); // 打开a.txt,如果没有就创建a.txt
if (fd == -1)
{
perror("open");
exit(1);
}
dup2(fd, STDOUT_FILENO); // 重定向
execlp("ps", "ps", "-e", "-f", NULL);
//close(fd);
return 0;
}
exec_ps.cpp
最新推荐文章于 2022-12-29 09:30:00 发布