4
#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;
}

Output:

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

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~

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

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

alk
  • 69,737
  • 10
  • 105
  • 255
kapilddit
  • 1,729
  • 4
  • 26
  • 51
  • 2
    Other than solution mentioned by @Joachim you can set disable buffuring by using `setbuf(stdout, NULL);` or print your messages on `stderr`. – Dayal rai Apr 04 '14 at 06:18
  • printf("\nInside Child Process before execl\n"); is working fine... – kapilddit Apr 04 '14 at 07:05

1 Answers1

9

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.

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

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thanks, Its working now. means printf before execl is visible!! What execl will do with child process? I read that execl will create the image of process. what is the return code from execl if it is successful? – kapilddit Apr 04 '14 at 06:22
  • 2
    @kapilddit If `execl` (and family) is successful, it will *not* return. It will only return on failure. What is does is to *replace* the image (the program, roughly) of the current process with the one loaded by the call. – Some programmer dude Apr 04 '14 at 06:25
  • What happens to the child process if I will not use sleep(5); in parent process. Then child process becomes orphan? As I am getting msg "Child process still running". How to solve that if I don't want to use sleep(5) in parent process with WNOHANG in waitpid? – kapilddit Apr 04 '14 at 06:31
  • 1
    @kapilddit The `sleep` function only puts the current process to sleep a little while, nothing else. The only way a process can be orphaned is if the parent process exits before the child process. – Some programmer dude Apr 04 '14 at 06:36
  • So If I will not use sleep(5); (with WNOHANG in waitpid) then my parent process will exits before child? If I change WNOHANG flag to 0 then I is working as expected. – kapilddit Apr 04 '14 at 06:40
  • 1
    @kapilddit I think I misunderstood you regarding the "child still running issue". You have no control over when a specific process runs, and you can't know beforehand how long a process will take. When you remove the `sleep` call, it seems that the operating systems runs the parent process before or quicker than the child process, and therefore it *might* exit before the child process (the child process may exit after your `waitpid` call but before the parent process exits). When the parent process `sleep` the child process have time to finish before the sleep ends. – Some programmer dude Apr 04 '14 at 06:44
  • See here in output, I am getting last prints from parent, after that It is executing ls command which I have used in child. So it means that parent exited before child? 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~ – kapilddit Apr 04 '14 at 06:58
  • 1
    @kapilddit Probably, but you can't really tell if the child exits between the last `printf` call in the parent and the `return` from the `main` function. It's not likely, but it could happen. Not that it really matters though, because if the child doesn't exit before the `waitpid` call, then it will be orphaned no matter the order of process exit. The reason is that if the parent exits before it has *waited* on all child processes to exit, the child processes will be orphaned. – Some programmer dude Apr 04 '14 at 07:07
  • 1
    @kapilddit It's successfull `wait` (or `waitpid` etc.) call that causes the child process to not become orphaned. – Some programmer dude Apr 04 '14 at 07:08