叉后失去控制() [英] Losing control after fork()

查看:122
本文介绍了叉后失去控制()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个简单的程序,以更好地了解叉(),等待()和execvp()。我的问题是我运行程序后,控制不会回传给壳,我不知道为什么。我要的是能够输入另一个命令到外壳中的code完成后。我看了看<一个href=\"http://stackoverflow.com/questions/14793636/child-processes-wont-die-in-c-program?rq=1\">this,但我不认为这是适用于我的情况。我基本上只是复制的code从我的这里

输入/输出(#是我打字行的前面,虽然不是输入的一部分):

 壳GT; #GCC test.c以-o测试
外壳&GT; #./test
输入程序(LS)
#LS
输入ARG(。)
#。
测试test.c的extra.txt
#一个; dlghasdf
# 回去
#:(

我的code:

  INT主要(无效){
    //只是走和清洁输入
    的printf(输入程序(LS)\\ n);
    炭inputprogram [5] = {0,0,0,0,0};
    与fgets(inputprogram,5,标准输入); //读取用户命令
    INT I;
    对于(I = 0; I&小于5;我++){
        如果(inputprogram [I] =='\\ n'){
            inputprogram [I] = 0;
        }
    }    的printf(输入ARG()\\ n);
    炭inputarg [5] = {0,0,0,0,0};
    与fgets(inputarg,5,标准输入); //读取用户命令
    对于(I = 0; I&小于5;我++){
        如果(inputarg [I] =='\\ n'){
            inputarg [I] = 0;
        }
    }    每[] =炭{inputarg [0],0};
    字符* ARG [] = {inputprogram,每次,NULL};    INT状态= 0;
    将为pid_t的孩子;    //叉(),execvp(),等待()
    //////////////////////////////////
    如果((子=叉())小于0){
        / * fork一个子进程* /
        的printf(*** ERROR:分叉子进程失败\\ n);
        出口(1);
    }否则如果(孩子== 0){
        execvp(inputprogram,ARG);
        出口(1);
    }其他{
        而(等待(安培;状态=小孩)!);
    }    返回EXIT_SUCCESS;
}


解决方案

这行

 而(等待(安培;!=状态子));

是不正确

您需要

 等待(安培;状态);

或者使用 waitpid函数 - 请参阅这里

I'm writing a simple program to better understand fork(), wait(), and execvp(). My problem is that after I run the program, control is not passed back to the shell and I have no clue why. What I want is to be able to input another command into the shell after the code is finished. I took a look at this, but I don't think it's applicable in my case. I've basically just the copied code I found from here.

input/output (# is in front of lines I typed, though is not part of the input):

shell> # gcc test.c -o test
shell> # ./test
input program (ls)
# ls
input arg (.)
# .
test test.c extra.txt
# a;dlghasdf
# go back
# :(

my code:

int main(void) {
    //just taking and cleaning input
    printf("input program (ls)\n");
    char inputprogram [5] = {0,0,0,0,0};
    fgets(inputprogram,5,stdin); //read in user command
    int i;
    for(i = 0; i < 5; i++) {
        if(inputprogram [i] == '\n' ){
            inputprogram[i] = 0;
        }
    }

    printf("input arg (.)\n");
    char inputarg [5] = {0,0,0,0,0};
    fgets(inputarg,5,stdin); //read in user command
    for(i = 0; i < 5; i++) {
        if(inputarg [i] == '\n' ){
            inputarg[i] = 0;
        }
    }

    char per []= {inputarg[0], 0};
    char *arg [] = {inputprogram, per , NULL};

    int status = 0;
    pid_t child;

    //the fork(), execvp(), wait()
    //////////////////////////////////
    if ((child = fork()) < 0) {
        /* fork a child process           */
        printf("*** ERROR: forking child process failed\n");
        exit(1);
    } else if(child == 0){
        execvp(inputprogram, arg);
        exit(1);
    } else {
        while(wait(&status != child));
    }

    return EXIT_SUCCESS;
}

解决方案

This line

while(wait(&status != child));

is incorrect

You need

wait(&status);

Or use waitpid - See here

这篇关于叉后失去控制()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆