Reputation: 3208
Does compilation of a program store some data in cache before execution?
I wrote a C program in Linux that starts Sublime Text in one thread and makes manual system calls in another thread. (I'll explain why I do this at the end as this is not relevant to this question)
I'm using getrusage to measure number of I/Os.
My question is, the number of I/O input is reduced by a lot RIGHT AFTER the compilation. In other words, if I do not free cache and memory between compilation and execution, the number of I/O is reduced by a lot. Why and how does this happen? See the cases below.
Case A - 1) Compile 2) Execute
> $ gcc pmulti.c -o pmulti
> $ ./pmulti
<result>
I/O Input: 632 Output: 0
Case B 1) Compile 2) Free Memory and Cache 3) Execute
> $ gcc pmulti.c -o pmulti
> # free && sync && echo 3 > /proc/sys/vm/drop_caches && free
> $ ./pmulti
<result>
I/O Input: 1400 Output: 0
As you can see, number of input is Case A < Case B. This has to mean that something is happening during its compilation, and it is influenced by freeing cache and memory.
Why and how does this happen? Where can I learn more about this?
Full code: https://pastebin.com/R6v00LLW
Abbreviated version of my code:
struct rusage usage;
void *func1(void *vargp)
{
/* Manual System Calls Here */
return NULL;
}
void *func2(void *vargp)
{
long pid;
int stat_loc;
if ((pid = (long) fork()) == 0){
//Format string for execvp here
char s[] = "/opt/sublime_text/sublime_text";
char* separator = " ";
char* argv[64];
int argc = 0;
char* tmp;
argv[argc] = strtok_r(s, separator, &tmp);
while( argv[argc] != NULL){
argc+=1;
argv[argc] = strtok_r(NULL, separator, &tmp);
}
execvp(argv[0],argv);
}
else {
waitpid(pid, & stat_loc, WUNTRACED);
}
return NULL;
}
int main()
{
pthread_t thread_id[2];
pthread_create(&thread_id[1], NULL, func2, NULL);
pthread_create(&thread_id[0], NULL, func1, NULL);
pthread_join(thread_id[0], NULL);
pthread_join(thread_id[1], NULL);
getrusage(RUSAGE_SELF, &usage);
printf("Input: %ld Output: %ld\n", usage.ru_inblock, usage.ru_oublock);
exit(0);
}
The purpose of this program:
I'm trying to come up with ways to improve the starting/load time of an application. I was thinking maybe I could speed up this process by making making manual system calls using multi-threading. I'm just a student so I may be completely wrong about my approach. I can already think of why this could not work, since threads are executed concurrently, execvp might be called before those system calls :(
func1 makes manual system calls and func2 executes Sublime Text.
This is part of my term project from Operating Systems class. I'm running this on Linux MintMate via VirtualBox on Windows 10.
Upvotes: 3
Views: 114
Reputation: 780698
The compiler just finished writing to the executable file. As a result, many, probably even most, of the pages of the file are likely to still be in the buffer cache, and don't need to be read back from disk when you run the program.
Upvotes: 12