0
介紹 UX 作爲我的課程工作的一部分,我正在編寫我自己的shell在linux中。我在後臺放置一些進程時遇到問題。 我知道在命令末尾放置一個'&'會使進程保留在後臺,父進程(myShell)不必等待它。它的做工精細用ls -l命令&,火狐&等,把貓放在背景中凍結我的殼
的問題
與我真正擔心是貓&問題。在這裏,當我運行這個命令時,cat進程轉到後臺,我回到myShell(父進程)提示符,儘管我可以鍵入,但myshell在幾秒鐘內就被凍結了。
這是否與阻止輸入等有關, 有什麼建議嗎?謝謝。
編輯:下面是從我的代碼,如要求通過@minitech
void executeCmd(char **cmdArgs, int inRedirectFd, int outRedirectFd, char *inFileName, char *outFileName, int bgProc, int inPipe, int outPipe, int *pipeFd1, int *pipeFd2){
int childPid;
childPid = fork();
if(childPid==0){
//writing the pipes before the redirection because the redirection can overwrite pipes
if(!inPipe && outPipe){
close(pipeFd1[0]);
dup2(pipeFd1[1], STDOUT_FILENO);
}
else if(inPipe && !outPipe){
close(pipeFd1[1]);
dup2(pipeFd1[0], STDIN_FILENO);
}
else if(inPipe && outPipe){
close(pipeFd1[1]);
close(pipeFd2[0]);
dup2(pipeFd1[0], STDIN_FILENO);
dup2(pipeFd2[1], STDOUT_FILENO);
}
if(outRedirectFd==1){
//token = strtok(NULL, " ");
int fd = open(outFileName, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR | S_IROTH | S_IRGRP);
if(fd==-1){
printf("myShell: %s: %s\n", cmdArgs[0], strerror(errno));
}
dup2(fd, STDOUT_FILENO);
}
if(inRedirectFd==0){
//token = strtok(NULL," ");
int fd = open(inFileName, O_RDONLY | O_APPEND, S_IRUSR | S_IWUSR | S_IROTH | S_IRGRP);
if(fd == -1){
printf("myShell: %s: %s\n", cmdArgs[0], strerror(errno));
}
dup2(fd, STDIN_FILENO);
}
execvp(cmdArgs[0], cmdArgs);
printf("myShell: %s: %s\n", cmdArgs[0], strerror(errno));
_Exit(EXIT_FAILURE);
}
else{
if(!bgProc){
int retStatus;
waitpid(childPid, &retStatus, 0);
//waitpid(childPid, &retStatus, 0);
//printf("%d\n", retStatus);
}
else{
//printf("parentDesn't Wait");
}
}
}
扔,你能想到的任何其他建議執行從我的外殼賦予了命令的功能。謝謝。
你能告訴我你的代碼嗎? – Ryan