为什么 execl 要求我在 运行 一个进程后点击 "Enter"?
Why does execl require me to hit "Enter" after running a process?
在 bash 中,当我键入 ls
并按回车键时,二进制 ls
将 运行 而我将 return 变为 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;
}
}
为什么?
输出结果如下:
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
此时我需要按 Enter 以便我 return 到 bash 提示。为什么?
At this point I will need to hit Enter so that I return to bash.
除了不,您已经在 bash。但是 ls 在提示后输出的所有内容使您看起来好像不是。继续,尝试另一个命令。
At this point I will need to hit ENTER so that I return to bash prompt.
其实你已经回到了提示符,只是你没有意识到而已。
具体来说,你这里面临的问题是,父进程不等待子进程退出,returns提前让子进程完成执行。因此,shell 提示符返回,然后 chlid 进程的输出(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-p
以上,请注意****
标记的那一行。在那里,你得到了 shell 提示。
在 bash 中,当我键入 ls
并按回车键时,二进制 ls
将 运行 而我将 return 变为 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;
}
}
为什么?
输出结果如下:
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
此时我需要按 Enter 以便我 return 到 bash 提示。为什么?
At this point I will need to hit Enter so that I return to bash.
除了不,您已经在 bash。但是 ls 在提示后输出的所有内容使您看起来好像不是。继续,尝试另一个命令。
At this point I will need to hit ENTER so that I return to bash prompt.
其实你已经回到了提示符,只是你没有意识到而已。
具体来说,你这里面临的问题是,父进程不等待子进程退出,returns提前让子进程完成执行。因此,shell 提示符返回,然后 chlid 进程的输出(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-p
以上,请注意****
标记的那一行。在那里,你得到了 shell 提示。