stdout vs stderr in fprintf
5 posters
Page 1 of 1
stdout vs stderr in fprintf
The following program doesn't "seem" to print "hello-out". (Try executing it)
#include <stdio.h>
#include <unistd.h>
int main()
{
while(1)
{
fprintf(stdout,"hello-out");
fprintf(stderr,"hello-err");
sleep(1);
}
return 0;
}
What could be the reason?
I don't know the answer.
#include <stdio.h>
#include <unistd.h>
int main()
{
while(1)
{
fprintf(stdout,"hello-out");
fprintf(stderr,"hello-err");
sleep(1);
}
return 0;
}
What could be the reason?
I don't know the answer.
shivang- Posts : 22
Join date : 2008-08-11
Age : 35
Location : Tondon 174
Re: stdout vs stderr in fprintf
stderr is related to parent shell itself.
stdout related to the child shell in which process is executing and storing the o/p in buffer.This will be shown on parent shell If the program terminates or when buffer overflows.
stdout related to the child shell in which process is executing and storing the o/p in buffer.This will be shown on parent shell If the program terminates or when buffer overflows.
ankurgutpa- Posts : 44
Join date : 2008-08-10
Age : 36
Location : Tandon 72
Re: stdout vs stderr in fprintf
writing to stdout is writing to descriptor 1....
writing to stderr is writing to descriptor 2....
but the above strange behaviour wasn't observed with this code....
this still fascinates me...
writing to stderr is writing to descriptor 2....
but the above strange behaviour wasn't observed with this code....
- Code:
main()
{
while( 1 )
{
write(1,"out",3) ;
write(2,"err",3) ;
sleep(1) ;
}
return 0 ;
}
this still fascinates me...
vijay- Posts : 6
Join date : 2008-08-18
Re: stdout vs stderr in fprintf
ankur the explanation you have given is incomplete actually it has nothing to do with parent shell and child shell this program can be changed to give proper output by just only changing one character .
vijay wen you are using write it does so because write is lower implementation of fprintf and it does not use bufferring were as fprintf does.....
vijay wen you are using write it does so because write is lower implementation of fprintf and it does not use bufferring were as fprintf does.....
Admin
i've read i somewhere on internet that stderr is parent shell and the process executes in child subshell and return it's o/p to parent shell
ankurgutpa- Posts : 44
Join date : 2008-08-10
Age : 36
Location : Tandon 72
Re: stdout vs stderr in fprintf
watever you are saying is correct but that is how shell executes the command
this code can be changed as i mentioned in prev reply this is all about bufferring nothing else.......
just check it out .......
this code can be changed as i mentioned in prev reply this is all about bufferring nothing else.......
just check it out .......
Re: stdout vs stderr in fprintf
actually stdout is a buffered stream whereas stderr isn't
Try running the code for 2-3 min you will get the answer
Try running the code for 2-3 min you will get the answer
ankitgr8- Posts : 6
Join date : 2008-08-11
Re: stdout vs stderr in fprintf
yes ankit your xplanation is correct .........
just try fprintf(stdout,"hello-out\n");
and observe the output ..........
just try fprintf(stdout,"hello-out\n");
and observe the output ..........
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum