954,234 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help me Regarding

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
{
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

vijay496
Newbie Poster
4 posts since Oct 2011
Reputation Points: 10
Solved Threads: 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");
}
joehms22
Junior Poster
112 posts since Jan 2010
Reputation Points: 28
Solved Threads: 19
 

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");
}
rubberman
Posting Virtuoso
1,559 posts since Mar 2010
Reputation Points: 277
Solved Threads: 178
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: