For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > December 2007 > Regarding standared i/o functions









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Regarding standared i/o functions
venkat

2007-12-24, 8:14 am

Hi,

i have a program like


main()
{
char a[10]= " hello";
printf("%s", a);
fork();



}


which prints twice. i have similar program ,

main()
{
char a[10]= " hello";
write(1, a , sizeof a);
fork();



}


which will print only once. I am not sure whether it belongs to C or
Unix. How does the printf(buffered) works over write(unbuffer). Please
also refer pointers to it if any?

Thanks,
Vikas.
venkat

2007-12-24, 8:14 am

I mean why this behaves differently in bounded and unbounded. Mostly
because of Implementation of these functions. How this has been
achieved.

Vikas.

On Dec 24, 3:25=A0pm, venkat <venkatavi...@gmail.com> wrote:
> Hi,
>
> i have a program like
>
> main()
> {
> char a[10]=3D " hello";
> printf("%s", a);
> fork();
>
> }
>
> which prints twice. i have similar program ,
>
> main()
> {
> char a[10]=3D " hello";
> write(1, a , sizeof a);
> fork();
>
> }
>
> which will print only once. I am not sure whether it belongs to C or
> Unix. How does the printf(buffered) works over write(unbuffer). Please
> also refer pointers to it if any?
>
> Thanks,
> Vikas.


John Tsiombikas

2007-12-24, 8:14 am

On 2007-12-24, venkat <venkatavikas@gmail.com> wrote:
>
> Hi,
>
> i have a program like
>
>
> main()
> {
> char a[10]= " hello";
> printf("%s", a);
> fork();
> }
>
>
> which prints twice. i have similar program ,


The output buffer is flushed by newlines, or when it reaches a certain
size. Here you don't have any newlines, thus the output is actually
written only after your process exits. At which point there are two of
them.

If you add a newline at ithe end of your string, you will see that it
will get printed only once.

>
> main()
> {
> char a[10]= " hello";
> write(1, a , sizeof a);
> fork();
> }
>
>
> which will print only once.


This is not even correct... you write 10 characters, while you
initialize only 7 of them... the other 3 might be anything. Change the
sizeof a, with an strlen(a).
Anyhow, as to why it prints only once... of course, that's because it's
unbufferd, and the output happens before the fork.

P.S. main returns an int. whether implicit or explicit, you must add a
return 0 at the end. Having said that, it's pretty bad form to use
implicit ints nowadays. Change your function to: ``int main(void)''.
Also, you don't have to specify the size of strings that you
initialize... just use: ``char a[] = "hello";'' or ``char *a =
"hello";''. Note that the two forms are not really equivalent, there is
a subtle difference.

--
John Tsiombikas (Nuclear / Mindlapse)
http://nuclear.sdf-eu.org/
turistu@gmail.com

2007-12-25, 4:23 am

On Dec 24, 1:03 pm, John Tsiombikas <nucl...@siggraph.org> wrote:
>
>
> This is not even correct... you write 10 characters, while you
> initialize only 7 of them... the other 3 might be anything.


no, he's initializing 6 of them, and the other 4 are guaranteed to be
'\0'
as per the C standard.
William Pursell

2007-12-25, 4:23 am

On Dec 24, 10:25 am, venkat <venkatavi...@gmail.com> wrote:

> main()
> {
> char a[10]= " hello";
> printf("%s", a);
> fork();
> }
>
> which prints twice. i have similar program ,
>
> main()
> {
> char a[10]= " hello";
> write(1, a , sizeof a);
> fork();
> }
>
> which will print only once. I am not sure whether it belongs to C or
> Unix. How does the printf(buffered) works over write(unbuffer). Please
> also refer pointers to it if any?


Consider adding a call to fflush() after printf() and before fork().
That will cause the first program to only output once. All that
happening is that printf() writes the data to a buffer which
is copied in the child. When the parent exits, the
FILE * is flushed and the buffer is written to the underlying
file. Same thing happens in the child, so the output is
repeated. When you invoke write() directly, this buffering
does not occur. Calling fflush() causes write() to be invoked,
so that the buffer is empty when fork() is called.
John Tsiombikas

2007-12-25, 4:23 am

On 2007-12-25, turistu@gmail.com <turistu@gmail.com> wrote:
>
> On Dec 24, 1:03 pm, John Tsiombikas <nucl...@siggraph.org> wrote:
>
> no, he's initializing 6 of them, and the other 4 are guaranteed to be
> '\0'
> as per the C standard.


Indeed, thanks for the correction.
Still the point that the OP doesn't really want to write all those
zeroes to stdout, and should use strlen there, stands.


--
John Tsiombikas (Nuclear / Mindlapse)
http://nuclear.sdf-eu.org/
David Schwartz

2007-12-25, 10:12 pm

On Dec 24, 2:25 am, venkat <venkatavi...@gmail.com> wrote:

> main()
> {
> char a[10]= " hello";
> printf("%s", a);
> fork();
> }
>
> which prints twice. i have similar program ,


This code has a bug, fix the bug and the problem will go away. You
allow both the parent and child to return from 'fork' and implicitly
call 'exit' when 'main' returns. This is an error. You want to call
'_exit' in the child.

DS
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com