Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Are system calls sometimes costlier than library calls?

Reply
Thread Tools

Are system calls sometimes costlier than library calls?

 
 
Richard Tobin
Guest
Posts: n/a
 
      11-06-2007
In article <(E-Mail Removed). com>,
omkar pangarkar <(E-Mail Removed)> wrote:
>Hence in above example its clear that printf() is cheaper that write()
>contrary to what we might think. Why is that? Who is telling our
>program to combine to calls to printf() in a single wrtie() ? Is it
>the libc?


One of the main purposes of the standard i/o library is to provide
buffering in order to reduce the overhead of system calls. You
can control buffering with the setbuf() and setvbuf() functions.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
 
 
 
omkar pangarkar
Guest
Posts: n/a
 
      11-06-2007
Hi all,
I have two simple hello world programs one using printf()[a
library func] and other using write() [syscall]

--prog 1--
#include<stdio.h>
#include<stdlib.h>

int main() {
printf("Hello"); /* up to here write() isn't called, if u
* give \n here then two write()s will
* be called (?)*/
printf("World\n");
return 0;
}

--end--

--prog2--
#include<unistd.h>

int main() {
write(1, "Hello", 5);
write(1, "world\n", 6);
return 0;
}

--end--

If u see the output of strace for prog1

--truncated--
....
munmap(0xb7fa0000, 115973) = 0
fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
0) = 0xb7fbc000
write(1, "HelloWorld\n", 11) = 11
exit_group(0) = ?

-- end --

Its only making ONE write() system call

for prog2 if u see strace
--truncated--
....
mprotect(0xb7f50000, 4096, PROT_READ) = 0
munmap(0xb7f56000, 115973) = 0
write(1, "Hello", 5) = 5
write(1, "world\n", 6) = 6
exit_group(0) = ?

--end--

Its making TWO syscalls.

Hence in above example its clear that printf() is cheaper that write()
contrary to what we might think. Why is that? Who is telling our
program to combine to calls to printf() in a single wrtie() ? Is it
the libc?

Its certainly not compiler optimization as I compiled it with -O0
option.

Thanks
Omkar.

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      11-06-2007
In article <(E-Mail Removed). com>,
omkar pangarkar <(E-Mail Removed)> wrote:
>Hence in above example its clear that printf() is cheaper that write()
>contrary to what we might think. Why is that? Who is telling our
>program to combine to calls to printf() in a single wrtie() ? Is it
>the libc?


I suggest you read about setbuf()
--
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      11-06-2007
On Wednesday 07 Nov 2007 1:56 am omkar pangarkar <(E-Mail Removed)>
wrote in article
<(E-Mail Removed). com>:

> Hi all,
> I have two simple hello world programs one using printf()[a
> library func] and other using write() [syscall]
>
> --prog 1--
> #include<stdio.h>
> #include<stdlib.h>
>
> int main() {
> printf("Hello"); /* up to here write() isn't called, if u
> * give \n here then two write()s will
> * be called (?)*/
> printf("World\n");
> return 0;
> }
>
> --end--
>
> --prog2--
> #include<unistd.h>
>
> int main() {
> write(1, "Hello", 5);
> write(1, "world\n", 6);
> return 0;
> }
>
> --end--
>
> If u see the output of strace for prog1
>
> --truncated--
> ...
> munmap(0xb7fa0000, 115973) = 0
> fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0
> mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
> 0) = 0xb7fbc000
> write(1, "HelloWorld\n", 11) = 11
> exit_group(0) = ?
>
> -- end --
>
> Its only making ONE write() system call
>
> for prog2 if u see strace
> --truncated--
> ...
> mprotect(0xb7f50000, 4096, PROT_READ) = 0
> munmap(0xb7f56000, 115973) = 0
> write(1, "Hello", 5) = 5
> write(1, "world\n", 6) = 6
> exit_group(0) = ?
>
> --end--
>
> Its making TWO syscalls.
>
> Hence in above example its clear that printf() is cheaper that write()
> contrary to what we might think.


Why are you worried about microoptimisation like these?

> Why is that? Who is telling our
> program to combine to calls to printf() in a single wrtie() ? Is it
> the libc?


No. It's the compiler's optimiser.

> Its certainly not compiler optimization as I compiled it with -O0
> option.


BTW, write per se isn't likely to be a system call, but a wrapper to
one.

 
Reply With Quote
 
Rob Kendrick
Guest
Posts: n/a
 
      11-06-2007
On Tue, 06 Nov 2007 12:26:25 -0800, omkar pangarkar wrote:

> I have two simple hello world programs one using printf()[a
> library func] and other using write() [syscall]


<snip>

> Hence in above example its clear that printf() is cheaper that write()
> contrary to what we might think.


You're thinking too simply. printf() may well be doing a lot of
processing before it calls write() (in fact, it is) which may be more
expensive than an additional system call. You can't know which is faster
by simply monitoring what is called.

> Why is that? Who is telling our program
> to combine to calls to printf() in a single wrtie() ? Is it the libc?


Yes. Standard IO under almost all self-respecting C libraries is
buffered.

> Its certainly not compiler optimization as I compiled it with -O0
> option.


Indeed not. I'd be upset if a compiler merged function calls like this.

Why are you playing about with such a trivial low-gain optimisation?
It's doubly pointless, as you're not even measuring the costs properly.
Perhaps similar reasons to your faulty optimisation of the word "you" is
at work?

B.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-06-2007
santosh wrote On 11/06/07 15:32,:
> On Wednesday 07 Nov 2007 1:56 am omkar pangarkar <(E-Mail Removed)>
> wrote
>> [... two printf() calls generate just one write() ...]
>>
>>Why is that? Who is telling our
>>program to combine to calls to printf() in a single wrtie() ? Is it
>>the libc?

>
>
> No. It's the compiler's optimiser.


What compiler does this? Libraries have been
buffering output since time immemorial, but I've
never seen a compiler merge two successive printf()
calls into one call. (I've seen a compiler replace
printf("Hello!\n") with puts("Hello!"), but never
the transformation you describe.)

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
omkar pangarkar
Guest
Posts: n/a
 
      11-06-2007
On Nov 7, 1:38 am, Rob Kendrick <(E-Mail Removed)> wrote:
> On Tue, 06 Nov 2007 12:26:25 -0800, omkar pangarkar wrote:
> > I have two simple hello world programs one using printf()[a
> > library func] and other using write() [syscall]

>
> <snip>
>
> > Hence in above example its clear that printf() is cheaper that write()
> > contrary to what we might think.

>
> You're thinking too simply. printf() may well be doing a lot of
> processing before it calls write() (in fact, it is) which may be more
> expensive than an additional system call. You can't know which is faster
> by simply monitoring what is called.
>
> > Why is that? Who is telling our program
> > to combine to calls to printf() in a single wrtie() ? Is it the libc?

>
> Yes. Standard IO under almost all self-respecting C libraries is
> buffered.
>
> > Its certainly not compiler optimization as I compiled it with -O0
> > option.

>
> Indeed not. I'd be upset if a compiler merged function calls like this.
>
> Why are you playing about with such a trivial low-gain optimisation?
> It's doubly pointless, as you're not even measuring the costs properly.
> Perhaps similar reasons to your faulty optimisation of the word "you" is
> at work?
>
> B.


This is not an attempt to optimize any code. I just need to know why
such thing is happening and who is behind it?

 
Reply With Quote
 
cr88192
Guest
Posts: n/a
 
      11-06-2007

"omkar pangarkar" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> On Nov 7, 1:38 am, Rob Kendrick <(E-Mail Removed)> wrote:
>> On Tue, 06 Nov 2007 12:26:25 -0800, omkar pangarkar wrote:
>> > I have two simple hello world programs one using printf()[a
>> > library func] and other using write() [syscall]

>>
>> <snip>
>>


<snip>

>>
>> Indeed not. I'd be upset if a compiler merged function calls like this.
>>
>> Why are you playing about with such a trivial low-gain optimisation?
>> It's doubly pointless, as you're not even measuring the costs properly.
>> Perhaps similar reasons to your faulty optimisation of the word "you" is
>> at work?
>>
>> B.

>
> This is not an attempt to optimize any code. I just need to know why
> such thing is happening and who is behind it?
>


CRTL...

as others have noted, it does buffering, and may choose to write output in
larger chunks than what is initially passed to it.

I suspect, at least in the case of printf, that this buffering is usually
done until either:
the buffer is full;
a newline or similar is encountered.


for file IO, it is less clear if/when exactly contents are written, but we
do know at least that fflush and fclose cause contents to be written.

I am not sure if in general fflush has lower-level effects (for example, if
it also flushes OS-level file caching or commits changes to the underlying
filesystem, or if it only ensures that changes are commited to the OS
proper). this would mostly effect cases like, say, one calls fflush, and
then very shortly following, the kernel crashes or the power fails or
something...



 
Reply With Quote
 
Tzy-Jye Daniel Lin
Guest
Posts: n/a
 
      11-07-2007
On Tue, 06 Nov 2007 15:52:18 -0500, Eric Sosman wrote:

> What compiler does this? Libraries have been
> buffering output since time immemorial, but I've never seen a compiler
> merge two successive printf() calls into one call. (I've seen a
> compiler replace printf("Hello!\n") with puts("Hello!"), but never the
> transformation you describe.)


stdio flushes its output buffer on every '\n'.

#include <stdio.h>
int main() {
char c, s[2] = "";
for (s[0] = 'A'; s[0] <= 'Z'; s[0]++)
puts(c);
for (c = 'a'; c <= 'z'; c++)
putchar(c);
putchar('\n');
return 0;
}

You will see that 26 write("A\n")..write("Z\n") syscalls are made, but
only one write("abcdefghijklmnopqrstuvwxyz\n") syscall is made.
 
Reply With Quote
 
Martien Verbruggen
Guest
Posts: n/a
 
      11-07-2007
On Wed, 7 Nov 2007 01:21:35 +0000 (UTC),
Tzy-Jye Daniel Lin <(E-Mail Removed)> wrote:
> On Tue, 06 Nov 2007 15:52:18 -0500, Eric Sosman wrote:
>
>> What compiler does this? Libraries have been
>> buffering output since time immemorial, but I've never seen a compiler
>> merge two successive printf() calls into one call. (I've seen a
>> compiler replace printf("Hello!\n") with puts("Hello!"), but never the
>> transformation you describe.)

>
> stdio flushes its output buffer on every '\n'.


Not necessarily. A stream can be unbuffered, fully buffered, or line
buffered (which is presumably what you think is always the case).
setvbuf() can be used to control this behaviour.

The C standard tells us that stderr is (initially) not fully buffered
(i.e. line buffered or unbuffered), and stdin and stdout are fully
buffered when a stream is determined not to be an interactive device.

\begin{offtopic}
On the OP's system, assuming it is some Unix-like system, stdout
will be line buffered when connected to an interactive terminal, and
fully buffered when connected to a non-interactive terminal.
\end{offtopic}

Martien
--
|
Martien Verbruggen | Louis Pasteur's theory of germs is ridiculous
| fiction -- Pierre Pachet, Professor of
| Physiology at Toulouse, 1872
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Firefox locks up on first URL sometimes; Sometimes closes itself Jim Firefox 0 06-28-2005 01:42 PM
Rebel XT Black Costlier than Silver. Why? Artistry Digital Photography 10 04-14-2005 08:01 PM
? Content Entered in Forms Sometimes Duplicated and Sometimes Not ? Nehmo Sergheyev HTML 1 05-09-2004 07:07 PM
::std sometimes needed, sometimes not Marcin Vorbrodt C++ 24 09-17-2003 03:01 PM
Re: Sometimes ASP.NET does find the dll, sometimes doesn't John Saunders ASP .Net 0 08-28-2003 01:40 PM



Advertisments