EDIT : 일부 들여 쓰기가 올바르게 수행되지 않지만 코드가 완전하고 올바르게 차단되었습니다. 죄송합니다.간단한 UNIX 셸에서 리디렉션 및 파이프 라이닝 문제가 발생했습니다.
클래스 할당의 경우 간단한 UNIX 쉘의 일부를 구현해야했습니다. 리디렉션, 파이핑 및 후면 접지를 지원해야합니다. Command_line이라는 구조체를 채우는 파서가 제공되었습니다 (아래 구조체 프로토 타입을 포함 할 것입니다). 제 커미션은 이러한 Command_line을 처리하는 함수를 작성하는 것입니다 (리디렉션, 역 접지, 파이핑 및 프로그램 실행).
거의 작동했습니다. 그러나 어떤 이유로 그것이 형식의 명령을 제대로 처리하지 못합니다. program1 | program2 - 파일. 예 : cat < file1.in | cat - file2.in. 리디렉션을 필요로하지 않지만 여전히 동일한 문제를 야기하는 파이프 앞에 테스트 프로그램을 작성하여 문제가 리디렉션되지 않는 것 같습니다. 대부분의 경우 파이프 라이닝이 작동합니다. 문제를 일으키는 인수로 "-"를 사용하는 것은 이러한 프로그램입니다.
이러한 문제가있는 명령 줄 중 하나를 실행하면 첫 번째 프로그램의 출력이 인쇄되고 프로세스가 멈 춥니 다 (수동으로 일시 중단하고 종료해야 함). 그것은 나중에 사용자에게 프롬프트를주지 않거나 입력에 반응하지 않습니다 (프로세스를 일시 중단하는 데 사용하는 Ctrl + Z는 제외).
이 작업을 수행하는 방법에 대한 조언은 많은 도움이 될 것입니다.
/* This is the structure that holds the information about a parsed
* command line. The argvs array is an array of string vectors; in
* other words, for some int i, argvs[i] is an array of strings.
* You should be able to use argvs[i] in calls to one of the execv*()
* functions.
*/
typedef struct {
char *argvs[MAX_PROGS + 1][MAX_ARGS + 1];
int num_progs; /* Number of argument vectors; if > 1, piping is requested */
char *infile; /* Name of stdin redirect file; NULL if no redirection */
char *outfile; /* Name of stdout redirect file; NULL if no redirection */
int append; /* Is output redirection appending? */
int bg; /* Put command into background? */
} Command_line;
그리고이 구조체 중 하나를 처리하는 내 코드, (나는 #include를 밖으로 떠 났어요) :
다음은 구조체입니다. run_proc()
내
pid_t runproc(int fd[][2], int num, Command_line *cmd);
void execute_command_line(Command_line *cmd) {
int n;
int temp_pipe[2];
int fd[MAX_PROGS-1][2];
pid_t pids[MAX_PROGS];
/* Clears pipes (sets all values to -1*/
for(n = 0; n < cmd->num_progs; n++){
fd[n][0] = -1;
fd[n][1] = -1;
}
/*Uses temp_pipe to connect write end of nth pipe to read end of (n+1)th
pipe*/
for(n = 0; n < cmd->num_progs - 1; n++){
pipe(temp_pipe);
fd[n][1] = temp_pipe[1];
fd[n+1][0] = temp_pipe[0];
}
/*If input file redirection is occuring, redirects read end of first pipe to
file*/
if(cmd->infile){
fd[0][0] = open(cmd->infile, O_RDONLY);
if(fd[0][0] < 0){
printf("Error executing command\n");
exit(1);
}
}
/*If output file redirection is occurring, redirects write end of last pipe to
file. Sets append option according to append field of command*/
if(cmd->outfile){
if(cmd->append){
fd[cmd->num_progs - 1][1] = open(cmd->outfile, O_APPEND | O_WRONLY);
if(fd[cmd->num_progs - 1][1] < 0){
printf("Error executing command\n");
exit(1);
}
}else{
fd[cmd->num_progs - 1][1] = open(cmd->outfile, O_WRONLY);
if(fd[cmd->num_progs - 1][1] < 0){
printf("Error executing command\n");
exit(1);
}
}
}
/*Runs runproc for every program in pipe, stores return values (pids of
children) in array*/
for(n = 0; n < cmd->num_progs; n++){
pids[n] = runproc(fd, n, cmd);
}
/*Closes all pipes*/
for(n = 0; n < cmd->num_progs; n++){
if(fd[n][0] >= 0) close(fd[n][0]);
if(fd[n][1] >= 0) close(fd[n][1]);
}
/*Waits for all children*/
for(n = 0; n < cmd->num_progs; n++){
wait(NULL);
}
}
pid_t runproc(int fd[][2], int num, Command_line *cmd){
pid_t pid;
int n;
int frk_chk;
pid = fork();
if(pid < 0){
printf("Error executing command\n");
exit(1);
}else if (!pid){ /*Child code*/
/*Redirects stdin/stdout of process to read/write end of corresponding
pipe*/
if(fd[num][0] >= 0) dup2(fd[num][0], STDIN_FILENO);
if(fd[num][1] >= 0) dup2(fd[num][1], STDOUT_FILENO);
/*Closes pipe ends*/
for(n=0; n < cmd->num_progs - 1; n++){
if(fd[num][0] >= 0) close(fd[num][0]);
if(fd[num][1] >= 0) close(fd[num][1]);
}
/*If backgrounding: forks, parent exits, child executes program.
If not backgrounding: program just executes*/
if(cmd->bg){
if((frk_chk = fork()) < 0){
printf("Error executing command\n");
exit(1);
}else if(frk_chk){
exit(0);
}else{
if(!(cmd->infile) && num == 0) close(STDIN_FILENO);
execvp(cmd->argvs[num][0], cmd->argvs[num]);
}
}else{
if(!num){
dup2(fd[0][1], STDOUT_FILENO);
}
execvp(cmd->argvs[num][0], cmd->argvs[num]);
}
printf("Error executing command\n");
exit(1);
}else{ /*Parent code*/
/*Returns pid of child, used for reaping loop*/
return pid;
}
}