I have a simple C program to start the time process (I would prefer not to publish the full code, as this is an active school assignment). My main function looks like this:
int main(void) { int i; for (i = 0; i < 5; i++) { printf("%lf\n", sample_time()); } exit(0); }
sample_time() is a function that takes the time to deploy a new process and returns the result in seconds as a double . The sample_time() , which forks:
double sample_time() { // timing stuff if (!fork()) exit(0); // immediately close new process // timing stuff return a_sample_time; }
As expected, running the program, times , in the terminal displays 5 numbers as follows:
$ ./times 0.000085 0.000075 0.000079 0.000071 0.000078
However, an attempt to transfer this to a file (or to another location) on a Unix terminal produces unexpected results.
For example, ./times > times.out creates a file with fifteen numbers. Also ./times | wc -l ./times | wc -l prints 15 , confirming an earlier result. By running ./times | cat ./times | cat , I again see fifteen numbers , more than five of which are different .
Does anyone know what on earth can cause something like this? I have no ideas.
./times ! = ./times | cat ./times | cat . Wat.
c linux unix command-line-interface
John zeringue
source share