0
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
void main()
{
  int i = 1;
  pid_t child_pid;
  printf("The main program process ID is %d", (int) getpid());
  printf("%d", i);
  child_pid = fork();
  if (child_pid != 0) {
    i++;
    printf("%d", i);
    printf("This is the parent process, with ID %d \n", (int) getpid());
    printf("The child process is %d ", (int) child_pid);
  } else {
    printf("%d", i);
    printf("This is the child process, with ID %d \n", (int) getpid());
  }
}

I'm running this program using the C language, using the fork() function. As I understand it, when a process calls fork(), a duplicate process, called a child process, is created. The parent process continues executing from the point that fork() was called, and the child process, too, executes the same program from the same place.

So when I run my program, I expect the output be like the following text:

The main program process ID is 181411This is the child process, with ID 1815
The main program process ID is 18142This is the parent process,with ID 1814
The child process is 1815

But I actually see this output:

The main program process ID is 181411This is the child process, with ID 1815
The main program process ID is 181412This is the parent process,with ID 1814
The child process is 1815

It means that the child executes the program first!!! When I put \n at the end of each printf statement the output is correct!!!

I've tried it on the Fedora v12 and rhel 5 distributions. Is there any logical relation between the \n and the fork() operation? How I can solve this problem?

0

4 Answers 4

4

The problem is that output is line-buffered and it isn't really flushed to your screen until you either output \n or explicitly call fflush on stdout. (That said, there's no guarantee about which process is going to be faster at outputting stuff).

Sign up to request clarification or add additional context in comments.

2 Comments

This means the text that is buffered will be printed twice? first when the "printf" statement was executes and second when the OS switches between processes?
Yes, if parent process buffers output before the fork and then flushes in both processes it's natural to assume the first output will be printed twice.
3

stdoutcommonly is line buffered. So the text to be printed is flushed to the console when receiving a '\n'.

Which process writes first is nondeterministicly handled by the OS.

To have things printed out without the need to have it triggered by '\n' using strerr is an option, as it normally is unbuffered.

5 Comments

Are you sure OS does something nondeterministically? Surely the aggregate outcome may appear random...
Ok,ok ... I'm missing some "and" there. It should have read: "Which process writes first is nondeterministicly and handled by the OS." @AkiSuihkonen
"So the text to be printed is flushed to the console when receiving a '\n'." It means the text that is buffered will be printed when the OS switches between processes?
If the process sees a '\n' (which implies it's active) it flushes stdout's buffer, at least it initiates it. Depending on the OS' scheduling mechanics it might be set inactive again just before the printing. @Azad
Unfortunately the OS will not guarantee that the parent will run first, in fact it is more likely that the child will run first. fork() suspends the parent to create the child. But the child never ran before, while the parent has. So the child will have higher scheduling priority. If you really badly want the parent to run first you can try calling nice or sched_yield in the child or synchronize your processes (mutexes and the likes) to have it guaranteed.
0

My experience is that using stderr as fprintf(stderr,......) always appears to write unbuffered output. I was able to print the contents of a line before a segment error stopped execution. Try it again with stderr.

Comments

0

You can use:

setbuf(stdout, NULL)

to set the unbuffered output in stdout stream.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.