在输出中看不到execl之前打印 [英] Prints before execl is not visible in output

查看:98
本文介绍了在输出中看不到execl之前打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

int main(void)
{
    pid_t Checksum_pid = fork();

    if (Checksum_pid < 0)
        printf("Fork Failed\n");
    else if (Checksum_pid == 0)
    {
    printf("\nInside Child Process before execl");        
    //execl("/bin/sleep", "/bin/sleep", "2", NULL);
    execl("/bin/ls","ls",(char *)NULL) ;
        //exit(EXIT_FAILURE);
    printf("\nInside Child Process after execl\n");
    exit(EXIT_FAILURE);
    }
    else
    {
        int childStatus;        
    printf("\nInside Parent Process");
    sleep(5);
    pid_t returnValue = waitpid(Checksum_pid, &childStatus, WNOHANG);

        if (returnValue > 0)
        {
            if (WIFEXITED(childStatus))
                printf("\nChild Exit Code: %d\n", WEXITSTATUS(childStatus));
            else
                printf("\nChild Exit Status: 0x%.4X\n", childStatus);
        }
        else if (returnValue == 0)
            printf("\nChild process still running\n");
        else
        {
            if (errno == ECHILD)
                printf("\nError ECHILD!!\n");
            else if (errno == EINTR)
                printf("\nError EINTR!!\n");
            else
                printf("\nError EINVAL!!\n");
        }
    printf("\nParent: I am about to finish\n");
    }

    return 0;
}

输出:

kth4kor-2:~/bosch/Test1$ ./a.out  a.out  ___waitpid_test1  waitpid_test1~  waitpid_test1.c  waitpid_test1.c~  waitpid_test2.c  waitpid_test2.c~ 
Inside Parent Process Child Exit Code: 0
Parent: I am about to finish

现在,如果我要删除父级的sleep(5),则输出:

Now If I will remove sleep(5) in parent then output:

kth4kor-2:~/bosch/Test1$ ./a.out 
Inside Parent Process

Child process still running

Parent: I am about to finish

kth4kor@g3gdev-kth4kor-2:~/bosch/Test1$ a.out  ___waitpid_test1  waitpid_test1~  waitpid_test1.c  waitpid_test1.c~  waitpid_test2.c  waitpid_test2.c~

最后,我尝试使用0作为waitpid而不是WNOHANG的第三个参数,然后它在下面的输出中显示了所有子项:

Finally I tried with 0 as third argument to waitpid instead of WNOHANG then its gave me below output with any prints of child:

kth4kor-2:~/bosch/Test1$ ./a.out 
a.out  ___waitpid_test1  waitpid_test1~  waitpid_test1.c  waitpid_test1.c~  waitpid_test2.c  waitpid_test2.c~
Inside Parent Process
Child Exit Code: 0
Parent: I am about to finish

有人可以帮助我理解执行execl之后和执行之前的进程行为吗?

Can somebody help me to understand the behavior of process after executing execl and before executing it?

推荐答案

请记住,通过printf(到stdout)的输出通常是行缓冲的.这意味着输出缓冲区将在换行符上刷新.由于要打印的字符串后没有换行符,因此不会清除输出缓冲区.

Remember that output through printf (to stdout) is normally line buffered. That means the output-buffer gets flushed on newline. As you don't have a newline after the string you want to print, the output-buffer will not be flushed.

或者在字符串的最后添加换行符,或者使用 fflush 手动刷新缓冲区.

Either add a newline last in the string, or manually flush the buffers with fflush.

这篇关于在输出中看不到execl之前打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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