为什么execl要求我点击;输入“;在运行一个进程之后?

为什么execl要求我点击;输入“;在运行一个进程之后?,c,linux,bash,execl,C,Linux,Bash,Execl,在bash中,当我键入ls并按enter键时,二进制ls将运行,我将再次返回shell提示符,而不做任何操作 但是,用C编写的此程序将阻止: #include <sys/types.h> #include <stdio.h> #include <unistd.h> int main(void) { pid_t other = fork(); // other will be 0 for the child process // oth

在bash中,当我键入
ls
并按enter键时,二进制
ls
将运行,我将再次返回shell提示符,而不做任何操作

但是,用C编写的此程序将阻止:

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

int main(void)
{
    pid_t other = fork();
    // other will be 0 for the child process
    // other will be the childs process' value in the parent process.

    switch(other) {
        case 0:
            printf("%s %i\n", "I am the child process!", other);
            execl("/bin/ls","ls",NULL);         
            return 0;
        default:
            printf("%s %i\n", "I am the parent process!", other);
            return 1;
    }

}
此时,我需要按Enter键,以便返回bash提示符。为什么?

在这一点上,我需要点击回车键,以便返回bash

除了没有,你已经在bash了。但是,提示后的所有ls输出都使您看起来不是。继续,尝试另一个命令

此时,我需要按ENTER键,以便返回bash提示符

事实上,你已经回到了提示,你只是没有意识到

更详细地说,您在这里面临的问题是,父级不会等待子级退出,而是在子级完成执行之前返回。因此,shell提示符返回,然后chlid进程的输出(ls的输出)被打印到输出上

如果你注意到了,你已经得到了提示,你的输出会在稍后出现


请注意上面标记的
***
行。在这里,您的shell提示符又回来了。

但是为什么我看不到Korays MacBook Pro:~koraytugay$?@koraytugay,但您看到了,就在“我是子进程”输出之后。您在说什么?它在上面,在ls输出之前。
Korays-MacBook-Pro:~ koraytugay$ ./a.out 
I am the parent process! 40309
I am the child process! 0
Korays-MacBook-Pro:~ koraytugay$ AndroidStudioProjects  Movies          happyko         koray.i
Applications        Music           hello.c         koray.o
ClionProjects       Pictures        hello.sh        koray.s
Code            Public          innbound        mssql
Desktop         TheElementsFiles    innbound-pf     nono.txt
Documents       VirtualBox VMs      innbound_usage.log  svn-key
Downloads       a.out           k.txt           tugay.c
IdeaProjects        asm.asm         klinnck         webtoolkit
Koray.class     asm.hack        klinnck-pf
Koray.java      cexamples       koray.a
Library         fifa.sql        koray.c
Korays-MacBook-Pro:~ koraytugay$ ./a.out 
I am the parent process! 40309
I am the child process! 0
****Korays-MacBook-Pro:~ koraytugay$***** AndroidStudioProjects  Movies          happyko         koray.i
Applications        Music           hello.c         koray.o
ClionProjects       Pictures        hello.sh        koray.s
Code            Public          innbound        mssql
Desktop         TheElementsFiles    innbound-p