加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

为什么execl要求我在运行进程后点击“Enter”?

发布时间:2020-12-15 22:10:32 所属栏目:安全 来源:网络整理
导读:在bash中,当我键入ls并按Enter键时,二进制ls将运行,我将再次返回 shell提示符而不执行任何操作. 但是这个用C编写的程序会阻止: #include sys/types.h#include stdio.h#include unistd.hint main(void){ pid_t other = fork(); // other will be 0 for the c
在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 %in","I am the child process!",other);
            execl("/bin/ls","ls",NULL);         
            return 0;
        default:
            printf("%s %in","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键以便返回bash提示符.为什么?

解决方法

At this point I will need to hit ENTER so that I return to bash prompt.

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

详细说明,你面临的问题是,父母不会等待孩子退出并事先返回孩子完成执行.因此,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提示.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读