系统调用有时比库调用更昂贵吗? [英] Are system calls sometimes costlier than library calls?

查看:79
本文介绍了系统调用有时比库调用更昂贵吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有两个简单的hello world程序,一个使用printf()[a

库func],另一个使用write()[syscall]


--prog 1--

#include< stdio.h>

#include< stdlib.h>


int main(){

printf(" Hello"); / *到这里写()没有被调用,如果你这是
*在这里给出\ n然后两个write()s将会被调用(?
*)? )* /

printf(World \ n);

返回0;

}


--end--


--prog2--

#include< unistd.h>


int main(){

write(1,Hello,5);

write(1,world \ n,6) ;

返回0;

}


--end--


如果你看到stra1的输出为prog1

- 截断 -

....

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)=?


- 结束 -


只做一个write()系统调用


for prog2如果你看到strace

--truncated--

....

mprotect(0xb7f50000,4096,PROT_READ)= 0

munmap(0xb7f56000,115973)= 0

写(1 ,你好,5)= 5

写(1,world \ n,6)= 6

exit_group(0)=?


--end--


它正在制作两个系统调用。


因此在上面的例子中它清晰printf()比我们想象的那样写()

更便宜。这是为什么?谁告诉我们的

程序在单个wrtie()中组合调用printf()?这是libc的
吗?

它当然不是编译器优化,因为我用-O0

选项编译它。


谢谢

Omkar。

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.

推荐答案

文章< 11 **********************@e9g2000prf.googlegroups。 com>,

omkar pangarkar< om ********* @ gmail.comwrote:
In article <11**********************@e9g2000prf.googlegroups. com>,
omkar pangarkar <om*********@gmail.comwrote:

>因此在上面的例子清楚地表明printf()比我们想象的更便宜写()
。这是为什么?谁告诉我们的
程序在单个wrtie()中组合调用printf()?它是libc吗?
>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?



我建议您阅读有关setbuf()

-

谨防上述错误码;我只是证明它是正确的,

没有尝试过。 - Donald Knuth

I suggest you read about setbuf()
--
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth


2007年11月7日星期三上午1:56 omkar pangarkar< om ********* @ gmail.com>

在文章中写道

< 11 ********************** @ e9g2000prf.googlegroups。 com>:
On Wednesday 07 Nov 2007 1:56 am omkar pangarkar <om*********@gmail.com>
wrote in article
<11**********************@e9g2000prf.googlegroups. com>:

大家好,

我有两个简单的hello world程序,一个使用printf()[a

library func]和其他使用write()[syscall]


--prog 1--

#include< stdio.h>

#include< stdlib.h>


int main(){

printf(" Hello"); / *到这里写()没有被调用,如果你这是
*在这里给出\ n然后两个write()s将会被调用(?
*)? )* /

printf(World \ n);

返回0;

}


--end--


--prog2--

#include< unistd.h>


int main(){

write(1,Hello,5);

write(1,world \ n,6) ;

返回0;

}


--end--


如果你看到stra1的输出为prog1

- 截断 -

...

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)=?


- 结束 -


其o nly make write()系统调用


for prog2如果你看到strace

--truncated--

...

mprotect(0xb7f50000,4096,PROT_READ)= 0

munmap(0xb7f56000,115973)= 0

write(1," Hello" ,5)= 5

写(1,world \ n,6)= 6

exit_group(0)=?


--end--


它正在制作两个系统调用。


因此在上面的例子中它明确表示printf()是写得更便宜()

与我们的想法相反。
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?


为什么会这样?谁告诉我们的

程序在单个wrtie()中组合调用printf()?这是libc的
吗?
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.


当然不是编译器优化因为我用-O0

选项编译它。
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.


2007年11月6日星期二12:26:25 -0800,omkar pangarkar写道:
On Tue, 06 Nov 2007 12:26:25 -0800, omkar pangarkar wrote:

I有两个简单的hello world程序,一个使用printf()[a

库func],另一个使用write()[syscall]
I have two simple hello world programs one using printf()[a
library func] and other using write() [syscall]



< ; snip>

<snip>


因此在上面的例子中明确表示printf()比我们想象的那样写()

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



你的想法太简单了。 printf()可能会在调用write()(实际上是)之前进行大量的处理,这可能比其他系统调用更昂贵。通过简单地监控所谓的内容,你不可能知道哪个更快。

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.


为什么会这样?谁在告诉我们的程序

在单个wrtie()中组合调用printf()?它是libc吗?
Why is that? Who is telling our program
to combine to calls to printf() in a single wrtie() ? Is it the libc?



是的。几乎所有自尊的C库下的标准IO都是缓冲的。

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


它当然不是编译器优化,因为我用-O0编译它br />
选项。
Its certainly not compiler optimization as I compiled it with -O0
option.



的确不是。如果一个编译器合并了这样的函数调用,我会感到沮丧。


你为什么玩这么简单的低增益优化?

它这是毫无意义的,因为你甚至没有正确地衡量成本。

也许类似的原因导致你错误地优化了你这个词。

在工作?


B.

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.


这篇关于系统调用有时比库调用更昂贵吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆