fork() 然后 execvp(“gcc program.c”) 有更好的替代方法吗?
以下代码段首次运行时需要更多时间(5s),连续运行时需要更少时间(250ms)。 有没有更好的方法来执行gcc.
int pid,status;
char * argv[] = {"gcc","program.c",NULL};
if(!(pid=fork())){
execvp("gcc",argv);
}
while(pid!=wait(&status)){
//do nothing
}
The following code segment takes more time (5s) when it is run first time and takes less time(250ms) on consecutive runs. Is there any better way to execute gcc.
int pid,status;
char * argv[] = {"gcc","program.c",NULL};
if(!(pid=fork())){
execvp("gcc",argv);
}
while(pid!=wait(&status)){
//do nothing
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,我不这么认为。 第一次 5 秒可能是将所有内容从磁盘读取到内存的时间。 编译器本身、要编译的源代码、要包含的标头、要链接的库等。它分散在整个磁盘上,因此磁盘头必须进行大量寻道。 那很慢。
之后,您将所有数据缓存在 RAM 中,它只是解析和编译,并且可能成为 CPU boudn 而不是磁盘 IO 绑定。
Well, I don't think so. 5 seconds on the first go is probably time to read everything from disk to memory. The compiler itself, the sources to compile, the headers to include, libraries to link against etc. It's scattered all across the disk, so the disk heads have to seek a lot. That's slow.
After that you have all the data cached in RAM, it's just parsing and compilation and probably becomes CPU boudn rather than disk IO bound.
没有其他方法可以在不同的 Unix 版本上运行。 要运行单独的进程,您必须使用 fork-exec - 这正是它们的用途。
There's no other way to do it that would run across different Unix versions. To run a separate process you have to use fork-exec - that's exactly what they are made for.
您还可以调用其他函数(
std::system
就是其中之一),但它们通常最终被实现为fork
和exec
。There are other functions you can call (
std::system
being one), but they generally end up being implemented asfork
andexec
.