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

Recommended Answers

All 2 Replies

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");
}

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");
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.