Time system call help xv6. time.c #include \"types.h\" #include \"user.h\" #incl
ID: 3746778 • Letter: T
Question
Time system call help xv6.
time.c
#include "types.h"
#include "user.h"
#include "date.h"
int
main (int argc, char *argv[])
{
int startTicks = uptime();
int pid = fork();
if (pid < 0) {
printf(2, "Error: Invalid PID! ");
exit();
}
if (pid > 0)
wait();
if (pid == 0) {
if (exec(argv[1], argv + 1) < 0) {
printf(2, "Error: Exec fails! ");
exit();
}
}
int endTicks = uptime();
int seconds = (endTicks - startTicks);
printf(1, " Real Time in ticks: %d tick(s) ", seconds);
exit();
}
1. If command is “time ls”, it should display in number of ticks
2. If command is “time ls -s”, it should display time in seconds (floating point value).
Just need help modifying this code to fit the requirments of the second inputoption. "time <PID> -s"
Explanation / Answer
#include "types.h"
#include "user.h"
#include "date.h"
int
main (int argc, char *argv[])
{
int startTicks = uptime();
int pid = fork();
if (pid < 0) {
printf(2, "Error: Invalid PID! ");
exit();
}
if (pid > 0)
wait();
if (pid == 0) {
if (exec(argv[1], argv + 1) < 0) {
printf(2, "Error: Exec fails! ");
exit();
}
}
int endTicks = uptime();
int seconds = (endTicks - startTicks)/100;
int partial_seconds = (endTicks - startTicks)%100;
printf(1, "%s", argv[1]);
printf(1, " ran in %d.", seconds);
if (partial_seconds < 10)
printf(1, "0");
printf(1, "%d ", partial_seconds);
exit();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.