gpt4 book ai didi

c++ - 等待命令不会等待子进程完成 c cpp c++

转载 作者:搜寻专家 更新时间:2023-10-31 01:21:19 24 4
gpt4 key购买 nike

我正在尝试编写一个 C++ 程序来创建子进程、运行命令并将输出通过管道返回到父正在运行的命令的输入。

我让父级执行 wait(NULL) 或 wait((void*)pid) 命令,但它不等待。

代码如下:

#include <string.h>
#include <fstream>
#include <iostream>
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
using namespace std;

int main(int argc, char * argv[])
{
char* commands[strlen(argv[1])];
char *command = NULL;
command = strtok(argv[1],"|");
int i = 0;
while(command != NULL)
{
commands[i] = command;
i++;
command = strtok(NULL,"|");
}

int numberOfCommands = i;

pid_t pid;
int pfd[2];
char* prgname = NULL;
if(pipe(pfd) == -1)
{
perror("error on pipe call");
return(1);
}

for(int j = 0;j<numberOfCommands;j++)
{
cout<<commands[j]<<endl;
}

pid = fork();
if(pid == 0){//child process
printf("Child: My PID = %d\n", getpid());
printf("Child: Running...\n");
close(pfd[0]); //close read end of pipe
dup2(pfd[1],1);//connect the pipes
close(pfd[1]);//close extra file descriptors
prgname = commands[0];//first command
cout<<"child starting command: "<<prgname<<endl;
execlp(prgname, prgname, 0);//Load the program
**printf("Child: Done sleeping, returning.\n");**
}
else
{
printf("Parent: My PID = %d\n", getpid());
**wait((void*)pid); //also tried wait(NULL); same effect
printf("Parent: Running...\n");**
close(pfd[1]); //close the write end of the pipe
dup2(pfd[0],0);//connect the pipes
close(pfd[0]); //close extra file descriptor
prgname = commands[1];//now run the second command
cout<<"parent starting command: "<<prgname<<endl;
execlp(prgname, prgname, 0);//Load the programm
}
cout<<"all done"<<endl;
return 0;
}

不要去掉粗体线。我希望父进程在 wait() 命令处等待,子进程会打印出“Child done sleeping...”然后完成,然后父进程会打印出“Parent:running...”

我做错了什么!

谢谢!

更新:程序的完整输出是:

dmegs
more
Child: My PID = 30070
Child: Running...
Parent: My PID = 30066
Parent: Running...
parent starting command: more
child starting command: dmegs
Child: Done sleeping, returning.
all done

最佳答案

我看到四个问题:

1) execlp() 失败:execlp()(或任何 exec 函数系列)完全替换了当前正在运行的如果成功,则处理图像 - 除非出现问题,否则预计不会返回。但是您看到的是“ child :睡完了,回来了”消息,所以它不可能成功。 (在您的示例中,我猜想这可能是因为 dmegs 应该是 dmesg。)

2) printf()cout 输出缓冲意味着无法保证您会按照输出发生的顺序获得输出。如果你想通过打印输出来调试它,你最好打印到 stderr(例如使用 fprintf(stderr, ...))(默认情况下)无缓冲。

3) 正如其他人所指出的,wait((void *)pid) 是错误的。 wait(NULL)waitpid(pid, NULL, 0)

4) 这是否是一个问题取决于平台,但是...... execlp() 的终止空指针参数应该明确地写成 (char *) 0 而不仅仅是 0,以确保它作为指针而不是整数传递。通常在 C 中,指针上下文中的 0 定义为空指针,但是当将参数传递给参数数量可变的函数时,编译器没有足够的信息知道您正在尝试在指针上下文中使用它,因此除非您显式转换它,否则会将其作为整数传递。这可能会让您在指针和整数大小不同的平台上遇到麻烦。


所以我认为 wait() 正在工作,子进程实际上并没有运行您想要的命令,并且由于缓冲,父进程和子进程的输出混淆了。


这是您的代码的一个稍微修改过的版本,它不使用任何 C++,删除了命令处理内容,只是将 sleep 5 的输出通过管道传输到 cat(这是毫无意义的,因为 sleep 无论如何都不会生成任何输出,但是延迟对于查看发生了什么很有用):

#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(void)
{
pid_t pid;
int pfd[2];
if(pipe(pfd) == -1)
{
perror("error on pipe call");
return(1);
}

pid = fork();
if(pid == 0){//child process
fprintf(stderr, "Child: My PID = %d\n", getpid());
fprintf(stderr, "Child: Running...\n");
close(pfd[0]); //close read end of pipe
dup2(pfd[1],1);//connect the pipes
close(pfd[1]);//close extra file descriptors
fprintf(stderr, "child starting command: sleep 5\n");
execlp("sleep", "sleep", "5", (char *)0);//Load the program
fprintf(stderr, "child: execlp failed\n");
}
else
{
fprintf(stderr,"Parent: My PID = %d\n", getpid());
wait(NULL);
fprintf(stderr,"Parent: Running...\n");
close(pfd[1]); //close the write end of the pipe
dup2(pfd[0],0);//connect the pipes
close(pfd[0]); //close extra file descriptor
fprintf(stderr,"parent starting command: cat\n");
execlp("cat", "cat", (char *)0);//Load the programm
}
fprintf(stderr,"all done\n");
return 0;
}

输出:

$ gcc -Wall -o wait wait.c
$ ./wait
Child: My PID = 27846
Child: Running...
child starting command: sleep 5
Parent: My PID = 27845

(这里有5秒延迟)

Parent: Running...
parent starting command: cat
$

关于c++ - 等待命令不会等待子进程完成 c cpp c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3928612/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com