process of executing fork () [duplicate] - c

This question already has answers here:
How do 2 or more fork system calls work?
(4 answers)
Closed 8 years ago.
I have executed the following code :
#include<stdio.h>
int main()
{
printf("hello \t");
fork();
fork();
return 0;
}
Output : hello hello hello hello
Does this mean that fork() create exact copy of the code for the child process as that of the parent process except the fork() call which gets executed is eliminated?
This is confusing me because I studied somewhere that "fork() begins execution from the next line of the code". So, if this is true then it should create 3 child processes and 1 parent process and should print "hello" only once.
Please resolve this .
I ran this code on gcc compiler.

That's because the string "hello" was stored in the output buffer of the parent process.
printf flushes the buffer when it meets \n, otherwise it prints the output string only if the buffer is full.
By default, the child process inherits the buffer of parent process, so if the buffer is not flushed, the buffer of child process also contains "hello".
if :
#include<stdio.h>
int main()
{
printf("hello \n");
fork();
fork();
return 0;
}
You will see only one "hello".

This is because of printf... u say use printf("hello \n") in order the buffer is flushed after a newline or u can use fflush(stdout) in u r code and the output will be just one hello...

Related

What Race Causes the Output to Look Different?

I was given this question on a midterm last year.
Consider the following program.
#include <stdio.h>
#include <unistd.h>
int main (void) {
int pid = getpid ();
printf ("hello world (pid:%d)\n", pid);
int rc = fork();
if (rc < 0) {
fprintf (stderr, "fork failed\n");
retrun 1;
}
pid = getpid();
if (rc == 0) {
printf ("hello, I am child (pid:%d)\n", pid);
} else {
printf("hello, I am parent of %d (pid:%d)\n", rc, pid);
}
return 0;
}
And consider the following behavior that I got when I compiled and ran this program:
$ gcc -02 -Wall question.c
$ ./a.out # First program run
hello world (pid:20600)
hello, I am parent of 20601 (pid:20600)
hello, I am child (pid:20601)
$ ./a.out | cat # Second program run
hello world (pid:20605)
hello, I am parent of 20607 (pid:20605)
hello world (pid:20605)
hello, I am child (pid:20607)
a) What race could cause the output to look substantially different from either the first or the second run, and what would this output look like?
b) Explain each different in the outputs of the two program runs.
For part (a) I argued that there is a race between the child process and the parent process, and that the child can print before the parent, but apparently that was wrong. Is there any other race that would cause the output to be different? And why is my answer wrong?
For part (b) I am shaky on multiple things. First is that I see the PIDs are different in both runs, but I don't have a good explanation for that. Second that extra hello world in the second run is because of the way the program is being run with a pipe and the cat command?
The problem is that you pipe the output to cat.
By default, when stdout is connected to a terminal or console, then stdout is line buffered which means that the internal buffers are flushed at newline (or when the buffer is full, or when fflush is called).
But when stdout is not connected to a terminal or console, like when it is connected to a pipe, it becomes fully buffered. This means it will only be flushed if the buffer becomes full or fflush is called. Printing newlines doesn't do anything special, the newline is just added to the buffer.
Now because stdout is fully buffered the buffer with the contents of the first printf call will be copied to the child process as part of the fork call, and will be flushed when the child process exits.

Where does fork() start from [duplicate]

This question already has answers here:
printf anomaly after "fork()"
(3 answers)
Closed 4 years ago.
So as far as i know fork creates a duplicate of the process it's called from but it also copy's it's program counter so it continues from the line after it's called but why is this code printing hello world twice when it's before the fork
#include <stdio.h>
#include <sys/wait.h>
int main()
{
printf("Hello World");
fork();
wait(NULL);
return 0;
}
printf doesn't actually print -- it actually just puts data into a buffer to be printed later. It will actually be printed when the buffer gets flushed, which can happen in a variety of ways.
In your case, the buffer flush doesn't happen until after the fork, so both the parent and the child have a copy of the string to be printed in the buffer when they fork, and both end up printing it.

Why the printf() that precede the fork() gets executed even after the fork()? [duplicate]

This question already has answers here:
printf anomaly after "fork()"
(3 answers)
Closed 6 years ago.
If after a fork() is called the program should continue from the first instruction following the fork, why then the word START gets printed two times?
#include<stdio.h>
#include<unistd.h>
int main(){
int pid;
printf("START...");
pid = fork();
printf("%d: I've got %d\n", getpid(), pid);
return 0;
}
For example a possible output is:
START...605: I've got 606
START...606: I've got 0
Because you didn't flush the output buffer and so the text exists in both parent and child's output buffer after the fork().
Add fflush(stdout); after the first printf() and see the difference.

fork is confusing me [duplicate]

This question already has answers here:
fork() branches more than expected?
(3 answers)
Closed 8 years ago.
Can anyone, please, explain how does this code work ?
int main()
{
printf("Hello");
fork();
printf("World");
}
Prints:
HelloWorldHelloWorld
My exact question is, why hello is printed twice. Isn't hello printed first, then fork() is executed ?
Also, sometimes it prints:
HelloWorld
// then the reports....process exited with return value 0..etc etc.. then...//
HelloWorld
Why this output ?
The reason is: buffered output. "Hello" is in the buffer but not yet put out when you do the fork, so the forked process starts with the same buffer including the same word "Hello". Then, both the parent and the child output "World" so the total output is "HelloWorld" for both of them.
Adding to #ammoQ answer:
int main()
{
printf("Hello");
fflush(stdout);
fork();
printf("World");
}
will get you to the expected result.
Fork creates a copy of the process. And printf() may be buffered when the fork happens, this buffer would be copied.
Pretty solid explanation here:
fork() branches more than expected?

Running fork and printing hello [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Working of fork() in linux gcc
Why does this code print two times?
#include<stdio.h>
main()
{
printf("hello\n");
fork();
}
The above code prints "hello" one time.The code below prints "hello" two times.
#include<stdio.h>
main()
{
printf("hello");
fork();
}
The code above prints "hello" two times.
Please Somebody explain this strange behavior.
It's not guaranteed to behave in this way, but the usual behaviour is: With
printf("hello");
the "hello" is printed to the output buffer, but that buffer is not yet flushed. Then upon the
fork();
the program state is copied to the child process, including the non-empty output buffer. Upon exit, the output buffers of parent and child are both flushed.
With the newline, the output buffer is flushed before the fork().

Resources