Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 当我使用&;为什么进程没有在后台运行_C_Linux_Shell - Fatal编程技术网

C 当我使用&;为什么进程没有在后台运行

C 当我使用&;为什么进程没有在后台运行,c,linux,shell,C,Linux,Shell,我正在尝试使用C语言在Linux中创建自己的shell。 嗯,我成功地制作了“pormpt,basic commands and i/O redirection”,但当我尝试制作“后台执行程序时,我失败了。它不在后台工作。例如,预期的结果是: > sleep 5 & > cat file.txt will print the content of file.txt > [24617] retval: 0 但正在发生的是: > sleep 5 & >

我正在尝试使用C语言在Linux中创建自己的shell。 嗯,我成功地制作了“pormpt,basic commands and i/O redirection”,但当我尝试制作“后台执行程序时,我失败了。它不在后台工作。例如,预期的结果是:

> sleep 5 &
> cat file.txt
will print the content of file.txt
> [24617] retval: 0
但正在发生的是:

> sleep 5 &
> [24617] retval: 0
> cat file.txt
will print the content of file.txt
这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <signal.h>
#include <termios.h>


#define MAX_WORD 10
#define MAX_CHAR 100
int input_redirection_flag;
int output_redirection_flag;
int background;
char* input_file=NULL;
char* output_file=NULL;
void remove_endOfLine(char line[]){
    int i=0;
    while(line[i]!='\n')
        i++;
    line[i]='\0';
}
void read_line(char line[]){
    char* ret=fgets(line,MAX_CHAR,stdin);
    remove_endOfLine(line);
    if(strcmp(line,"quit")==0 || ret==NULL)
        exit(0);
}
int process_line(char* temp[],char line[]){
    int i=0;
    temp[i]=strtok(line," ");
    while(temp[i]!=NULL){
            temp[++i]=strtok(NULL," ");
    }
    return 1;
}
int redirection_background_checking(char* temp[]){
    int i=0;
    background=0;
    while(temp[i] != NULL){
        if(strcmp(temp[i],">")==0){
            output_redirection_flag=1;
            output_file=temp[i+1];
            return i;
        }
        if(strcmp(temp[i],"<")==0){
            input_redirection_flag=1;
            input_file=temp[i+1];
            return i;
        }
        if(strcmp(temp[i],"&")==0){
            background=1;
            temp[i]=NULL;
        }
        i++;

    }
    return i;

}
void check_line(char* temp[]){
    int i=0;
    int output_redirection_cnt=0;
    int input_redirection_cnt=0;
    if(temp[i]==NULL){
        printf("NO COMMAND\n");
        return ;
    }
    while(temp[i] !=NULL){
        if(strcmp(temp[i],">")==0)
            output_redirection_cnt++;
        if(strcmp(temp[i],"<")==0)
            input_redirection_cnt++;

        i++;
    }
    if(output_redirection_cnt+input_redirection_cnt>1){
        printf("you cannot use more than 1 signal.");
        temp[0]=NULL;
    }
}
int read_parse_line(char* args[],char line[]){
    char* temp[MAX_WORD];
    int pos;
    int i=0;
    read_line(line);
    process_line(temp,line);
    check_line(temp);
    pos=redirection_background_checking(temp);
    while(i<pos){
        args[i]=temp[i];
        i++;
    }
    args[i]=NULL;
    i++;

}
int main()
{
    char line[MAX_CHAR];
    char* args[MAX_WORD];
    char * tokens[MAX_WORD];
    printf("> ");
    fflush(stdout);
    while(read_parse_line(args,line)){
        pid_t pid=fork();
        if(pid==0){
            if(input_redirection_flag==1&&input_file!=NULL)
                dup2(open(input_file,O_RDWR|O_CREAT,0777),0);
            if(output_redirection_flag==1&&output_file!=NULL)
                dup2(open(output_file,O_RDWR|O_CREAT,0777),1);
            execvp(args[0],args);
        }
        else if(pid<0){
            printf("failed Process");
        }
        else{
            input_redirection_flag=0;
            output_redirection_flag=0;
            input_file=NULL;
            output_file=NULL;
        }
        if (background == 0){
           waitpid(pid,NULL,0);

        }else{

         printf("[%d] retval: %d\n",pid,0);
        }
        if(background == 1 && args[1]=="quit")
            break;

        printf("> ");
        fflush(stdout);
    }
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义最大单词10
#定义最大字符100
int输入\重定向\标志;
int输出_重定向_标志;
int背景;
char*input_file=NULL;
char*output_file=NULL;
void remove_endOfLine(字符行[]){
int i=0;
而(第[i]行!='\n')
i++;
第[i]行=“\0”;
}
无效读取行(字符行[]){
char*ret=fgets(行,最大字符,标准输入);
去除_endOfLine(线);
if(strcmp(第行,“退出”)==0 | | ret==NULL)
出口(0);
}
int process_行(字符*临时[],字符行[]){
int i=0;
温度[i]=标准温度(第行“”);
while(temp[i]!=NULL){
温度[++i]=strtok(空,“”);
}
返回1;
}
整数重定向\背景\检查(字符*临时[]){
int i=0;
背景=0;
while(temp[i]!=NULL){
如果(strcmp(温度[i],“>”==0){
输出\重定向\标志=1;
输出_文件=temp[i+1];
返回i;
}
如果(strcmp(温度[i],“”)==0)
输出_重定向_cnt++;
if(strcmp(temp[i],“”);
fflush(stdout);
while(read_parse_line(args,line)){
pid_t pid=fork();
如果(pid==0){
if(input\u redirection\u flag==1&&input\u file!=NULL)
dup2(打开(输入文件,O|U RDWR | O|U CREAT,0777),0);
if(output\u redirection\u flag==1&&output\u file!=NULL)
dup2(打开(输出文件,O|RDWR | O|U创建,0777),1);
execvp(args[0],args);
}

else if(pid)如果您有这个期望,那么您的代码不应该有
if(background==0){waitpid(pid,NULL,0);}else{printf([%d]retval:%d\n”,pid,0);}
IOW,您的代码正在后台运行,但您在生成进程后立即打印一条消息,这似乎让您感到困惑。即使在我将其删除后,我得到的输出仍然是:>sleep 5&>[24617]retval:0>cat file.txt 5秒后将打印文件的内容。txtexa我希望消息在后台处理完成后打印。在完成之前,我将能够编写另一个命令
echo“sleep 2;echo sleep”>my_script;chmod+x my_script
。然后从您的shell:
/my_script&echo which
之前将出现的which。我认为这是解决方案,我将尝试它。谢谢