[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: fork or printf problem



printf just sends the data to whatever has been defined as the standard
output. Under Unix a terminal is also a file (alebit a special kind).
Your code doesn't need to know what kind of file it is it just needs a
file pointer. The actual work is done in what is called a kernel module
for the particular device. If you are writing to a file actually the
data gets sent to the IDE/SCSI disk driver, in case of the terminal it
would be something else. All the driver cares for is that it should get
a string of data with an appropriate end of file in it (well it will
assume there was a end of file if the data pipe is closed from the other
side - usually!!) and then do it's magic so that the data can be stored
in an appropriate way in the particular device.



Mithun

Sumit Rangawala wrote:
> 
> Hi
>        This question is the one i posted some time ago.
> One thing i still couldn't understand is that  how printf distinguish
> between a terminal and a file.
> for it everything it has to do is to write to a file pointer ( fp = 1 )
> .  How then the buffering changes with the type of file pointed by file
> pointer 1.
> 
> TIA
> 
> Regards
> Sumit
> 
> Binand Raj S wrote:
> 
> > Sumit Rangawala forced the electrons to say:
> > > running the output executable of the following program as
> > > $a.out
> > > and
> > > $a.out > test
> > > give different results
> > > WHY??
> >
> > Buffering. Read man setbuf for the full story. In particular, stdout,
> > when directed to a terminal, is line buffered, but when directed to a
> > file is block buffered.
> >
> > Binand
> 
> > ****************************
> > :#include <stdio.h>
> > :#include <sys/types.h>
> > :#include <unistd.h>
> > :
> > :main()
> > :{
> > :
> > :    printf("Hello\n");
> > :    if(fork()==0)
> > :    {
> > :        printf("world\n");
> > :    }
> > :}
> > :
> >
> >
> >
> 
> >
> >
> > ---------------------------------------------
> > LIP is all for free speech.  But it was created
> > for a purpose.  Violations of the rules of
> > this list will result in stern action.
> 
> ---------------------------------------------
> The mailing list archives are available at
> http://lists.linux-india.org/cgi-bin/wilma/LIP