I am trying to execute LP to print a PDF document and wait for it to exit. After it exists i am trying to delete the file with unlink();
However the wait finishes even before execv execute LP. I am not quite sure how to handle this and why the wait isn't waiting till execv finishes.
Is there any other way to accomplish this?
if(fork())
{
fprintf(stderr, "Executing command %s %s", "/usr/bin/lp", homedir);
char *const parmList[] = {"/usr/bin/lp", homedir, (char *)0};
execv("/usr/bin/lp", parmList );
}else
{
int pid, status;
fprintf(stderr, "Wait\n");
pid = wait(&status);
fprintf(stderr, "Finished waiting.\n");
unlink(homedir);
}
When executing the above code the ouput would look like this:
Wait
Finished waiting.
Executing command /usr/bin/lp /home/user/Docs/test.pdf
/usr/bin/lp: Error - unable to access "/home/user/Docs/test.pdf" - No such file or directory
fork()
在子进程中返回零,在父进程中返回正值(假设fork成功),而wait(...)
仅在父进程中有意义,因此您需要交换if
的内容else
块。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.