1
#include<stdio.h>
int main()
{   
    if (fork())
    {       
        wait();
        printf("Now I am showing you ls -l"); // why can't we see this ?
        execlp("ls","ls", "-l", 0); //gets printed second
     }
    else
    {
        printf("We are in the child process."); // gets printed first
    }
}

I have this simple peace of code. My question is why don't we see on the console the first print, in the parent process?

user253956
  • 313
  • 6
  • 14
  • 3
    You need newlines in your prints, otherwise the rest of the string gets buffered and potentially never flushed. Also, when forking, the buffer can get duplicated into the child and the message can be printed twice. See this similar question: http://stackoverflow.com/questions/23818222 – nobody May 30 '14 at 12:19
  • OT: `execlp("ls","ls", "-l", 0);` shall be `execlp("ls","ls", "-l", (char*) NULL);` – alk May 30 '14 at 12:21

4 Answers4

3

The printf function provides buffered IO. When printing to stdout, as you are here, the buffer will normally get flushed when you print a newline \n, which you have not done, so your string is sitting in a buffer waiting to be printed.

The next thing you are doing is calling execlp. The exec family of functions replace the current process with the new one you have specified. The buffer is lost without ever being flushed, because the whole process is replaced.

If you add \n to the strings you are printing, you will probably see the output.

harmic
  • 28,606
  • 5
  • 67
  • 91
  • Thank you ! But according to this logic, why does the first prinf gets printed? Doesn't it also stay in the buffer ? – user253956 May 30 '14 at 13:23
  • @user253956 the other printf is in the child process, which exits normally (as opposed to execlp). The buffer is flushed when the process exits normally. – harmic May 30 '14 at 22:38
1

you have to flush stdout before the execlp.

if you put a \n at the end of the printf (or you call fflush(stdout)) you will get the correct result

Yuri
  • 351
  • 1
  • 5
0

In the interactive case, standard output is line buffered by default. And that execlp() will replace its memory image, therefore that output line may be dropped before it is written to console.

Change that print() statement to

printf("Now I am showing you ls -l\n");

to fix this problem.

Lee Duhem
  • 14,695
  • 3
  • 29
  • 47
0

There is 2 problems with your code:

  1. first you need to flush your outputs, either by adding \n in the strings or by calling fflush(stdout)
  2. But it should be the job of the son to exec(), of course you can always exec in the parent, but this is probably not what you want.

A more common fork/exec sample looks like :

int main() {  
  if (fork()) { // parent  
    wait(NULL); // wait his child  
    printf("Ok, my children finished the job\n");  
    exit(0);
  }  
  else { // child  
    printf("We are in the child process.\n"); // gets printed first  
    printf("Now I am showing you ls -l\n");   
    execlp("ls","ls", "-l", 0); // never return from this  
  }
}
Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69