C++ C++;在microshell中,输入命令并使用fork()、dup()、pipe()将其传递到进程。只是我不';我得不到我想要的结果 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; //第一个要执行的命令 首先无效(int pipeA[],char*命令[]){ //将标准输出重定向到管道[1]并关闭我们不使用的管道[0] dup2(pipeA[1],1); 关闭(pipeA[0]); execvp(命令[0],命令); printf(“第一错误”); 出口(127); } 空秒(int pipeA[],char*command2[]{ //将STDIN重定向到管道[0]并关闭我们未使用的管道[1] dup2(pipeA[0],0); 关闭(pipeA[1]); //这有时似乎没有什么作用 execvp(command2[0],command2); perror(“第二个错误”); 出口(127); } 内部主(空) { char-buf[1024]; char*命令[1024];//这是第一个输入,通常为“cat file.txt” //只使用一个或另一个,排序永远不起作用,“grep U”有时起作用 char*command2[]={(char*)“sort”,(char*)NULL};//这是要对上面的“command[1024]”及其在第二个函数中的使用进行排序 //char*command2[]={(char*)“grep”,(char*)“U”,(char*)NULL};//这是要grep上面的“command[1024]”及其在第二个函数中的使用 //fork和wait的变量 pid_t pid; pid_t pid2; 智力状态; int状态2; //查看是否存在当前未使用的| | bool=false; //创建管道 int-pipeA[2]; 管道(pipeA); //第一行,请求输入, 可通过OP
解决 这是适用于microshell的代码 我最终在最初的父进程中创建了两个进程 在while循环中移动一些变量并重置它们以再次工作。每次代码运行时也创建管道并关闭管道 为两个进程而不是一个进程创建了waitpid()C++ C++;在microshell中,输入命令并使用fork()、dup()、pipe()将其传递到进程。只是我不';我得不到我想要的结果 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; //第一个要执行的命令 首先无效(int pipeA[],char*命令[]){ //将标准输出重定向到管道[1]并关闭我们不使用的管道[0] dup2(pipeA[1],1); 关闭(pipeA[0]); execvp(命令[0],命令); printf(“第一错误”); 出口(127); } 空秒(int pipeA[],char*command2[]{ //将STDIN重定向到管道[0]并关闭我们未使用的管道[1] dup2(pipeA[0],0); 关闭(pipeA[1]); //这有时似乎没有什么作用 execvp(command2[0],command2); perror(“第二个错误”); 出口(127); } 内部主(空) { char-buf[1024]; char*命令[1024];//这是第一个输入,通常为“cat file.txt” //只使用一个或另一个,排序永远不起作用,“grep U”有时起作用 char*command2[]={(char*)“sort”,(char*)NULL};//这是要对上面的“command[1024]”及其在第二个函数中的使用进行排序 //char*command2[]={(char*)“grep”,(char*)“U”,(char*)NULL};//这是要grep上面的“command[1024]”及其在第二个函数中的使用 //fork和wait的变量 pid_t pid; pid_t pid2; 智力状态; int状态2; //查看是否存在当前未使用的| | bool=false; //创建管道 int-pipeA[2]; 管道(pipeA); //第一行,请求输入, 可通过OP,c++,linux,shell,process,piping,C++,Linux,Shell,Process,Piping,解决 这是适用于microshell的代码 我最终在最初的父进程中创建了两个进程 在while循环中移动一些变量并重置它们以再次工作。每次代码运行时也创建管道并关闭管道 为两个进程而不是一个进程创建了waitpid() #include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <string.h> #include <iostream> #inclu
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <sys/utsname.h>
#include <unistd.h>
using namespace std;
//first comannd to execute
void first(int pipeA[], char * command[]){
//redirect STDOUT to pipe[1] and close the pipe[0] we are not using
dup2(pipeA[1], 1);
close(pipeA[0]);
execvp(command[0], command);
printf(" first error ");
exit(127);
}
void second(int pipeA[], char * command2[]){
//redirect STDIN to pipe[0] and close the pipe[1] that we are not using
dup2(pipeA[0], 0);
close(pipeA[1]);
//This doesnt seem to be doing anything at times
execvp(command2[0], command2);
perror(" second error ");
exit(127);
}
int main(void)
{
char buf[1024];
char * command[1024];// this one is the first input usually 'cat file.txt'
//Use only one or the other, sort never works and 'grep U' works sometimes
char * command2[] = {(char *)"sort", (char *) NULL};// this is wants to sort the above 'command[1024]' and its use in the second function
//char * command2[] = {(char *)"grep",(char *)"U",(char *) NULL};// this is wants to grep the above 'command[1024]' and its use in the second function
//variables for forks and waits
pid_t pid;
pid_t pid2;
int status;
int status2;
//see if || exists not in use currently
bool pipeExists = false;
//create pipe
int pipeA[2];
pipe(pipeA);
//first line and ask for input,
cout<< "command: ";
while (fgets(buf,1024,stdin) != NULL)
{
buf[strlen(buf) -1] = 0;
//Save input into buf and tokenized? it
//NOT YET CATCHING ||, im only debugging and usually use use 'cat file.txt'
int number =0;
char * ptr;
ptr = strtok(buf, " ");
while(ptr != NULL){
command[number] = ptr;
ptr = strtok(NULL, " ");
number++;
}
//***************************************************************
//1. do the pipes go here or after the children?
//They seem to be working here but im not really sure where they should be
close(pipeA[0]);
close(pipeA[1]);
//create first child
if ((pid = fork()) <0)
printf("fork error");
else if (pid == 0)
{ /* child */
//create second child INSIDE ORIGINAL CHILD
//2. Is this correct? or is there not supposed to be grandchildren?
if ((pid2 = fork()) <0)
printf("fork 2 error");
else if (pid == 0)
{ /* child */
second(pipeA, command2);
printf("couldn't execute: %s");
exit(127);
}
//first command from buf
first(pipeA, command);
printf("couldn't execute: %s");
exit(127);
//3. Do I wait for this child aswell?
if ( (pid2 = waitpid(pid2, &status2, 0)) < 0)
printf("waitpid error");
}
/* parent */
if ( (pid = waitpid(pid, &status, 0)) < 0)
printf("waitpid error");
printf("Command :");
//***************************************************************
//***************************************************************
//SECOND WAY OF DOING IT
// THIS WAY IT TRIGGERS WAITPID ERRORS.
/*
close(pipeA[0]);
close(pipeA[1]);
//create first child
if ((pid = fork()) <0)
printf("fork error");
else if (pid == 0)
{
first(pipeA, command);
printf("couldn't execute: %s");
exit(127);
}
//create second child INSIDE ORIGINAL CHILD
if ((pid2 = fork()) <0)
printf("fork 2 error");
else if (pid == 0)
{
second(pipeA, command2);
printf("couldn't execute: %s");
exit(127);
}
//3. Do I wait for this child aswell?
if ( (pid2 = waitpid(pid2, &status2, 0)) < 0)
printf("waitpid error");
if ( (pid = waitpid(pid, &status, 0)) < 0)
printf("waitpid error");
printf("Command :");
*/
//***************************************************************
}
exit(0);
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//此函数将执行用户的第一个命令。
//它在管道中使用命令数组和布尔值来检查管道
//如果管道存在,则布尔值为true,并将把STDOUT()复制到管道的写入部分
//我们关闭管道的不必要部分,并在命令数组中执行该命令
//如果命令不执行,将出现一些错误checkink
void first_命令(int-pipeA[],char*command[],bool-pipeExists){
如果(存在){
dup2(pipeA[1],1);
关闭(pipeA[0]);
}
execvp(命令[0],命令);
printf(“无法执行第一个命令。\n”);
出口(127);
}
//此函数仅在管道存在时在主管道中调用
//它接受一个管道和一个命令数组
//它从管道的读取端复制STDIN并关闭未指定端
//它将根据管道中提供的内容执行命令
无效第二个命令(int-pipeA[],char*command2[]{
dup2(pipeA[0],0);
关闭(pipeA[1]);
execvp(command2[0],command2);
printf(“无法执行第二个命令。\n”);
出口(127);
}
内部主(空)
{
//此变量将接收用户提交的输入行
char-buf[1024];
//两个子进程的PID
pid_t pid;
pid_t pid2;
//这些将用于检查父进程中每个子进程的状态
智力状态;
int状态2;
//初始化管道
int-pipeA[2];
//输出请求用户输入的第一行
可通过OP解决
这是适用于microshell的代码
我最终在最初的父进程中创建了两个进程
在while循环中移动一些变量并重置它们以再次工作。每次代码运行时也创建管道并关闭管道
为两个进程而不是一个进程创建了waitpid()
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <sys/utsname.h>
#include <unistd.h>
using namespace std;
//first comannd to execute
void first(int pipeA[], char * command[]){
//redirect STDOUT to pipe[1] and close the pipe[0] we are not using
dup2(pipeA[1], 1);
close(pipeA[0]);
execvp(command[0], command);
printf(" first error ");
exit(127);
}
void second(int pipeA[], char * command2[]){
//redirect STDIN to pipe[0] and close the pipe[1] that we are not using
dup2(pipeA[0], 0);
close(pipeA[1]);
//This doesnt seem to be doing anything at times
execvp(command2[0], command2);
perror(" second error ");
exit(127);
}
int main(void)
{
char buf[1024];
char * command[1024];// this one is the first input usually 'cat file.txt'
//Use only one or the other, sort never works and 'grep U' works sometimes
char * command2[] = {(char *)"sort", (char *) NULL};// this is wants to sort the above 'command[1024]' and its use in the second function
//char * command2[] = {(char *)"grep",(char *)"U",(char *) NULL};// this is wants to grep the above 'command[1024]' and its use in the second function
//variables for forks and waits
pid_t pid;
pid_t pid2;
int status;
int status2;
//see if || exists not in use currently
bool pipeExists = false;
//create pipe
int pipeA[2];
pipe(pipeA);
//first line and ask for input,
cout<< "command: ";
while (fgets(buf,1024,stdin) != NULL)
{
buf[strlen(buf) -1] = 0;
//Save input into buf and tokenized? it
//NOT YET CATCHING ||, im only debugging and usually use use 'cat file.txt'
int number =0;
char * ptr;
ptr = strtok(buf, " ");
while(ptr != NULL){
command[number] = ptr;
ptr = strtok(NULL, " ");
number++;
}
//***************************************************************
//1. do the pipes go here or after the children?
//They seem to be working here but im not really sure where they should be
close(pipeA[0]);
close(pipeA[1]);
//create first child
if ((pid = fork()) <0)
printf("fork error");
else if (pid == 0)
{ /* child */
//create second child INSIDE ORIGINAL CHILD
//2. Is this correct? or is there not supposed to be grandchildren?
if ((pid2 = fork()) <0)
printf("fork 2 error");
else if (pid == 0)
{ /* child */
second(pipeA, command2);
printf("couldn't execute: %s");
exit(127);
}
//first command from buf
first(pipeA, command);
printf("couldn't execute: %s");
exit(127);
//3. Do I wait for this child aswell?
if ( (pid2 = waitpid(pid2, &status2, 0)) < 0)
printf("waitpid error");
}
/* parent */
if ( (pid = waitpid(pid, &status, 0)) < 0)
printf("waitpid error");
printf("Command :");
//***************************************************************
//***************************************************************
//SECOND WAY OF DOING IT
// THIS WAY IT TRIGGERS WAITPID ERRORS.
/*
close(pipeA[0]);
close(pipeA[1]);
//create first child
if ((pid = fork()) <0)
printf("fork error");
else if (pid == 0)
{
first(pipeA, command);
printf("couldn't execute: %s");
exit(127);
}
//create second child INSIDE ORIGINAL CHILD
if ((pid2 = fork()) <0)
printf("fork 2 error");
else if (pid == 0)
{
second(pipeA, command2);
printf("couldn't execute: %s");
exit(127);
}
//3. Do I wait for this child aswell?
if ( (pid2 = waitpid(pid2, &status2, 0)) < 0)
printf("waitpid error");
if ( (pid = waitpid(pid, &status, 0)) < 0)
printf("waitpid error");
printf("Command :");
*/
//***************************************************************
}
exit(0);
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//此函数将执行用户的第一个命令。
//它在管道中使用命令数组和布尔值来检查管道
//如果管道存在,则布尔值为true,并将把STDOUT()复制到管道的写入部分
//我们关闭管道的不必要部分,并在命令数组中执行该命令
//如果命令不执行,将出现一些错误checkink
void first_命令(int-pipeA[],char*command[],bool-pipeExists){
如果(存在){
dup2(pipeA[1],1);
关闭(pipeA[0]);
}
execvp(命令[0],命令);
printf(“无法执行第一个命令。\n”);
出口(127);
}
//此函数仅在管道存在时在主管道中调用
//它接受一个管道和一个命令数组
//它从管道的读取端复制STDIN并关闭未指定端
//它将根据管道中提供的内容执行命令
无效第二个命令(int-pipeA[],char*command2[]{
dup2(pipeA[0],0);
关闭(pipeA[1]);
execvp(command2[0],command2);
printf(“无法执行第二个命令。\n”);
出口(127);
}
内部主(空)
{
//此变量将接收用户提交的输入行
char-buf[1024];
//两个子进程的PID
pid_t pid;
pid_t pid2;
//这些将用于检查父进程中每个子进程的状态
智力状态;
int状态2;
//初始化管道
int-pipeA[2];
//输出请求用户输入的第一行
你可能试过什么?会发生什么?试着在strace下运行。试着创建一个更简单的问题示例,并将问题限制在一个不能正常工作的事情上。欢迎使用Stack Overflow!请回答你的问题以向我们展示你所做的调试类型。我希望你已经在Valgrind或类似的检查中运行了r、 例如,使用GDB之类的调试器进行调查。确保您也启用了一整套编译器警告。这些工具告诉了您什么,它们丢失了什么信息?阅读Eric Lippert的。请不要将问题更改为非问题。我已将您的