1

Hi friends,
help me out
Generally If the call to fork() is executed successfully Unix will create identical copies address spaces and the execution starts from the next statement of the fork()

So, in such case
output of the following prog must be
#include<stdio.h>
{
printf("\nwelcome to");
fork();
printf("\nDaniWeb Discussion");
}

O/P as per the theory:
------------
welcome to
DaniWeb Discussion
DaniWeb Discussion

but O/p coming is


welcome to
DaniWeb Discussion
welcome to
DaniWeb Discussion

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by rubberman
0

Try putting your newlines after the text, I'll bet that because you put it before, the compiler optimizes the first it in to the second string.

#include<stdio.h>

main()
{
printf("welcome to\n");
fork();
printf("DaniWeb Discussion\n");
}
0

It's sort of like joehms22 said, but it may not work using newlines alone. The printf() function is to stdout, which is buffered by default, and not output until flushed. A newline will not necessarily flush the output. So, do this instead:

#include<stdio.h>

main()
{
    printf("welcome to\n");
    fflush(stdout);
    fork();
    printf("DaniWeb Discussion\n");
    fflush(stdout);
}

Note that stderr is not buffered. All output to stderr is output immediately, so this will also work:

#include<stdio.h>

main()
{
    fprintf(stderr, "welcome to\n");
    fork();
    fprintf(stderr, "DaniWeb Discussion\n");
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.