简体   繁体   English

在 fork 和 execvp 调用后循环不继续

[英]While loop not continuing after fork and execvp calls

After running my program for the first time around it runs correctly but the loop does not continue.第一次运行我的程序后,它运行正常,但循环没有继续。

I have tried adding more forks into my function but it seems to not work.我尝试在我的 function 中添加更多叉子,但它似乎不起作用。

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

 using namespace std; 

 int main(){
int pipefd[2];
int rs;
pid_t cpid;
char* args1[256];
char* args2[256];
char cmd1[256];
char cmd2[256];
char path1[10];
char path2[10];
//starts while loop 
while(true){
//creates pipe
rs = pipe(pipefd);
if (rs < 0){
    perror("pipe");
    exit(1);
}
//gets comands from user
cout << "Command 1";
cin.getline(cmd1,256);
cout << "command 2";
cin.getline(cmd2,256);
//checks id with commands are quit
if (strcmp(cmd1,"quit") == 0)
    break;
if (strcmp(cmd2,"quit") == 0)
    break;
char *token;
token = strtok(cmd1," ");
int i=0;
//splits char arrays up
while(token != NULL){
    args1[i] = token;
    token = strtok(NULL, " ");
    i++;
}
args1[i] = NULL;
token = strtok(cmd2," ");
i = 0;
while(token != NULL){
    args2[i] = token;
    token = strtok(NULL, " ");
    i++;
}
args2[i] = NULL;
strcpy(path1,args1[0]);//copis the command to the path file
strcpy(path2,args2[0]);
//forks and creates child process
rs = fork();
if (rs == 0){//child process
    close(pipefd[1]);//close write end of pipe
    close(0);//close standard input
    dup(pipefd[0]);//duplicate read end of pipe into standard 
  input
    close(pipefd[0]);//close read end of pipe
    rs = execvp(path2,args2);//runs program 2
    if (rs < 0){
        perror("execl");
        exit(1);
    }
}
else{//PARENT PROCESS
    close(pipefd[0]);//close read end of pipe
    close(1);//close standard input
    dup(pipefd[1]);//duplicate write end of pipe into standard 
   input
    close(pipefd[1]);//clsoe write end of pipe
    rs = execvp(path1,args1);//runs command 1
    if (rs < 0){
        perror("execl");
        exit(1);
    }
    }

}
return 0;
}

After going through the loop the first time the user should be asked for enter in two more commands or be able to quit out of the function第一次完成循环后,应要求用户再输入两个命令或能够退出 function

rs = execvp(path1,args1);//runs command 1 rs = execvp(path1,args1);//运行命令1

This line in the "parent process" replaces the current program. “父进程”中的这一行替换了当前程序。 There is no while loop after this succeeds anymore, only program 1 .成功后不再有 while 循环,只有program 1

Think of it this way.这样想吧。 When user inputs m pairs of commands to your program, how many processes do you expect to be spawned?当用户向您的程序输入m对命令时,您希望生成多少个进程? You expect a total of 2m processes each corresponding to a command but you only fork m times each corresponding to an iterations of the while loop in your current code.您预计总共有2m个进程,每个进程对应于一个命令,但您只分叉m次,每个进程对应于当前代码中 while 循环的迭代。

You should instead fork a different process for program 1 as well, similar to how you did it for program 2 .相反,您应该为program 1分叉一个不同的进程,类似于您对program 2的处理方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM