Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.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_Unix_Terminal - Fatal编程技术网

C 模拟'&';同样,终端定制外壳

C 模拟'&';同样,终端定制外壳,c,unix,terminal,C,Unix,Terminal,我正在尝试用C重新创建自己的终端,我正在后台运行一个进程: 例如,在Linux终端中,如果我键入 ivo@ivo-Surface-Pro-6:~$firefox& 它立即打印并返回到终端: [1] 29093 ivo@ivo-Surface-Pro-6:~$ 我的不这样做,它要等到我关闭窗口才能返回终端,我该如何处理 ivo@ivo-Surface-Pro-6:/home/ivo/Documents/SO1/soi-myshell-Ivoo25$firefox& [1] 28505 如果我在打开

我正在尝试用C重新创建自己的终端,我正在后台运行一个进程: 例如,在Linux终端中,如果我键入

ivo@ivo-Surface-Pro-6:~$firefox&

它立即打印并返回到终端:

[1] 29093

ivo@ivo-Surface-Pro-6:~$

我的不这样做,它要等到我关闭窗口才能返回终端,我该如何处理

ivo@ivo-Surface-Pro-6:/home/ivo/Documents/SO1/soi-myshell-Ivoo25$firefox&

[1] 28505

如果我在打开firefox后关闭它,它将返回以下内容:

[1] 完成

我怎样才能模拟这个呢?除此之外,这是我处理这个问题的代码部分

    void execute(char **tokens, int token_Size, int *blk)
     {
    pid_t pid, wpid;

    int status;
    int result;

    int flag;
    int isPipe;
    int pipeAm = 0;
    int output;
    int input;

    int isAmper;

    int pipefd[2], pipefd2[2];

    if (needs_amper(tokens, token_Size) > 0)
    {
        *blk += 1;
    }

    signal(SIGCHLD, SIG_IGN); //Silently (and portably) reap children

    pid = fork();
    if (pid == -1)
    {
        perror("Fork:");
        exit(1);
    }
    else if (pid == 0) //Child 1
    {
        signal(SIGTSTP, handleSigtstp);
        signal(SIGCONT, handleSigCont);
        isAmper = needs_amper(tokens, token_Size);
        output = needs_out_redir(tokens, token_Size);
        input = needs_in_redir(tokens, token_Size);
        isPipe = needs_pipe(tokens, token_Size);
        pipeAm = needs_pipe2(tokens, token_Size);

        if (isAmper != -1)
        {
            printf("[%d] %d \n", *blk, getppid()); //-> [blk] is the job number asigned to the job
            tokens[isAmper] = NULL;
            
        }

        if (strcmp(tokens[0], "echo") == 0)
        {
            for (int i = 1; tokens[i]; i++)
            {
                printf("%s ", tokens[i]);
            }
        }

        flag = 0;

        if (output != -1)
        {
            redirect_output(tokens, output);
            tokens[output] = NULL;
            flag = 1;
        }

        if (input != -1)
        {
            redirect_input(tokens, input);
            tokens[input] = NULL;
            flag = 1;
        }

        if (isPipe != -1)
        {
            create_pipe(tokens, output, input, isPipe, pipefd, pipefd2, &pipeAm, &pid, &wpid);
        }

        if (flag || isPipe == -1)
        {
            execvp(tokens[0], tokens);
            perror("Unkown Command:");
            exit(1);
        }

        //  exit(0);
    }
    else // Main (parent) process after fork succeeds
    {
        while ((wpid = wait(&status)) > 0)
            ; // this way, the father waits for all the child processes
        result = waitpid(pid, &status, 0);
        if (result == 1) //If the process terminated correctly, result returns 0.
        {
            printf("The child process terminated with an error!.\n");
        }
    }
}

我如何解决这个问题这到底是什么?希望shell在子进程退出时接收异步通知吗?trap SIGCHLD或poll on waitpid。@KamilCuk我想要的是模拟terminall函数,当我键入firefox时,它会打开它并立即返回到终端,只需删除
wait
waitpid
…但对于其他命令,终端开始失败@KamilCukso您可以区分哪些命令末尾有
&
,哪些命令没有,并决定是否等待
后台进程。。。