Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.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
在通过execv执行的子进程中使用Scanf()不工作_C_Fork_Scanf_Execv - Fatal编程技术网

在通过execv执行的子进程中使用Scanf()不工作

在通过execv执行的子进程中使用Scanf()不工作,c,fork,scanf,execv,C,Fork,Scanf,Execv,我正在执行一个非常简单的程序,它使用scanf从用户那里获取整数输入。我通过fork()和execv将此程序作为子程序执行。子程序从不接受用户的输入。如有任何帮助,将不胜感激 #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <stdlib.h> #include <string.h> int main(void) { pid_t ch

我正在执行一个非常简单的程序,它使用scanf从用户那里获取整数输入。我通过fork()和execv将此程序作为子程序执行。子程序从不接受用户的输入。如有任何帮助,将不胜感激

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    pid_t childpid;

    if((childpid = fork()) == -1)
    {
        perror("fork");
        exit(1);
    }

    if(childpid == 0)
    {
        execv("child",NULL);
        exit(1);
    }
    else  
    {
        printf("Parent process is terminating...\n");
        return 0;
    }
}

我正在运行安装在虚拟机上的fedora中的代码。谢谢你需要等待子进程完成,请像这样修改你的代码

if(childpid == 0)
{
    execv("child",NULL);
    exit(1);
}
else
{
    wait(); //wait for child
    printf("Parent process is terminating...\n");
    return 0;
}

一旦父进程完成,控制权就返回给shell;并且可以关闭
stdin

要保留子级对stdin的访问权,您可以让父级等待,直到子级完成

所以,在你的父母身上:

else {
    printf("Parent process is terminating...\n");
    wait(NULL);
    return 0;
}

实际上,我写这段代码是为了说明fedora如何不支持级联终止,它显示了子级可以在没有父级的情况下执行。
if(childpid == 0)
{
    execv("child",NULL);
    exit(1);
}
else
{
    wait(); //wait for child
    printf("Parent process is terminating...\n");
    return 0;
}
else {
    printf("Parent process is terminating...\n");
    wait(NULL);
    return 0;
}