Joh*_*ith 0 c fork process exec
我必须使用EXEC系列来查找所拥有的进程(用户)并将其写入文件中.
码:
sprintf(buffer, "%d", getpid());
if ((pid = fork()) == -1)
exit(EXIT_FAILURE);
else if (pid == 0)
{
dup2(fd,STDOUT_FILENO);
dup2(fd,STDERR_FILENO);
// function that Write in the file
fdprintf(fd, "\n%s %s%d secondes %s\n", adornment, toShow, sec, adornment);
if (execlp("ps", "u", buffer, NULL) == -1)
show_err("Exelp Error\n");
}
else
{
wait(NULL);
fdprintf(fd, "%s %s%d secondes %s\n", adornment, toShow, sec, adornment);
close(fd);
}
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏!谢谢