Page 1 of 2
Posted: Sun Oct 17, 2004 7:52 pm
by dj_de
Usage: runtime program [arguments]
runtime returns the total turnaround time of program in number of milliseconds.
Example
$ runtime who >/dev/null
Total elasped time: 70 milliseconds
$ runtime sleep 3
Total elasped time: 3030 milliseconds
Implementation Details
* Spawn a new process using fork.
* Invoke program in the child process using a version of exec.
* Wait for the child to finish using wait.
* On Unix or Linux, time the program use the gettimeofday function to compute the elapsed time.
* Write the time output to stderr.
fork, exec, and wait, are system calls. They are documented in Section 2 of the online Programmer's Manual. On Unix, use the command man -s 2 function to access the documentation.
HOW TO DO
Posted: Sun Oct 17, 2004 8:16 pm
by Hamburglar
Beats the hell outta me..
MikeJ???
Posted: Sun Oct 17, 2004 10:04 pm
by MikeJ
/* dj.c a program that dont do jack shit */
#include <stdio.h>
#include <sys/types.h> /* this shit is for */
#include <unistd.h> /* ())=======D */
#include <sys/time.h> /* lol */
#include <sys/wait.h> /* the system calls */
int main(int argc, char **argv)
{
struct timeval tv; /* For getting time, from sys/time.h */
struct timezone tz;
int pid; /* process id */
long int t; /* for holding seconds */
char *newargv[20];
char *file[20];
int i, j, a;
if ( argc > 1 )
{
file[0] = argv[1];
a = 0;
while ( --argc > 0 )
newargv[a++] = (*++argv); /* build arguments */
newargv[a] = NULL;
j = 0;
gettimeofday(&tv, &tz);
printf("%s", asctime(localtime(&tv.tv_sec)));
t = (long int) time(0);
printf("time since 00:00:00 GMT Jan/1/1970 is %d secs\n", t);
while ( j < 1 ) {
j++;
if ( ( pid = fork() ) == 0 ) {
i = execvp( file[0], newargv );
} else {
wait( &i );
}
}
gettimeofday(&tv, &tz);
printf("%s", asctime(localtime(&tv.tv_sec)));
printf("Elapsed time is %ld secs\n", (long int) time(0) - t);
}
return 0;
}
this sorta works but i was too lazy to perfect it, uh hopefully it helps, btw here is sample output:
[mikej@snapzilla mikej]$ ./dj sleep 4
Sun Oct 17 15:03:33 2004
time since 00:00:00 GMT Jan/1/1970 is 1098050613 secs
Sun Oct 17 15:03:37 2004
Elapsed time is 4 secs
[mikej@snapzilla mikej]$
Posted: Sun Oct 17, 2004 10:23 pm
by dj_de
THANKS
i have 2 questions though:
1. did u use some peticular command to compile it?
2. if i get rid of some of those printf statements about timeofday, will it affect anything?
Posted: Mon Oct 18, 2004 12:02 am
by MikeJ
THANKS
i have 2 questions though:
1. did u use some peticular command to compile it?
2. if i get rid of some of those printf statements about timeofday, will it affect anything?
1. just gcc (gcc -o file file.c)
2. it shouldn't
Posted: Mon Oct 18, 2004 1:03 am
by MikeJ
ps that second while loop with the incrementing J is pretty useless
Posted: Mon Oct 18, 2004 1:18 am
by dj_de
// LOL NAME
// gcc
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/wait.h>
int main(int argc, char **argv)
{
struct timeval tv;
struct timezone tz;
int pid;
long double t;
char *newargv[20];
char *file[20];
int i, a;
if ( argc > 1 )
{
file[0] = argv[1];
a = 0;
while ( --argc > 0 )
newargv[a++] = (*++argv);
newargv[a] = NULL;
gettimeofday(&tv, &tz);
t = (long double) time(0);
if ( ( pid = fork() ) == 0 ) {
i = execvp( file[0], newargv );
} else {
wait( &i );
}
}
gettimeofday(&tv, &tz);
printf("Elapsed time is %ld ms\n", (long double) (time(0) - t)*10);
}
return 0;
}
Posted: Mon Oct 18, 2004 1:39 am
by MikeJ
// LOL NAME
// gcc
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/wait.h>
int main(int argc, char **argv)
{
struct timeval tv;
struct timezone tz;
int pid;
double t;
char *newargv[20];
char *file[20];
int i, a;
if ( argc > 1 )
{
file[0] = argv[1];
a = 0;
while ( --argc > 0 )
newargv[a++] = (*++argv);
newargv[a] = NULL;
gettimeofday(&tv, &tz);
t = time(0);
if ( ( pid = fork() ) == 0 ) {
i = execvp( file[0], newargv );
} else {
wait( &i );
}
gettimeofday(&tv, &tz);
printf("Elapsed time is %f ms\n", (double) (time(0) - t) * 1000);
}
return 0;
}
Posted: Mon Oct 18, 2004 3:33 am
by MikeJ
ps: it's only getting rounded seconds, uhhhhh hopefully your teacher wont notice (it will still say 3000ms, but never like 3029 ms)
lunix :darkstar:
Posted: Mon Oct 18, 2004 8:10 am
by Kungfubar
ps: it's only getting rounded seconds, uhhhhh hopefully your teacher wont notice (it will still say 3000ms, but never like 3029 ms)
lunix :darkstar:
MikeJ is my hero :eek: