views:

463

answers:

3

Hi,

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
}
+1  A: 

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.

sharptooth
There's also posix_spawn: https://www.opengroup.org/onlinepubs/9699919799/functions/posix_spawn.html :-P
Chris Jester-Young
+3  A: 

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.

Tadeusz A. Kadłubowski
Is there any way I can have all the data needed by GCC cached in RAM always no matter how heavy the other processes in the system are ?
Throw hardware at the problem. Faster disk. More RAM. Developers need good tools.You can tune low-level I/O scheduling. That's system specific and I have no idea how to do it on whatever OS you use.
Tadeusz A. Kadłubowski
I'm using CentOS.
How to setup per-process disk I/O scheduling or memory buffering on Linux (uname -r ?) is a material for another question, possibly on superuser.com once it launches.
Tadeusz A. Kadłubowski
"uname -r" gives 2.6.18-8.el5
A: 

There are other functions you can call (std::system being one), but they generally end up being implemented as fork and exec.

Max Lybbert