Time system call help xv6. 1. If command is “time l<PID>”, it should display in
ID: 3746892 • Letter: T
Question
Time system call help xv6.
1. If command is “time l<PID>”, it should display in number of ticks
2. If command is “time <PID> -s”, it should display time in seconds (floating point value).
The code below can time the number inticks, im just struggling to do the check for the "-s" part if the user chooses to input that.
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();
}
Explanation / Answer
Here as per your requirement you need to display time in seconds (floating point value).
We also need to caluclate the milliseconds in our code. We know that 1 second has 100 miliseconds. Using this condition I will share the code changes.
As, it is a floating point value, you need to make follwing changes to your existing code for "time <PID> -s” command :
Please replace this part of your code with the code I am sharing :
int seconds = (endTicks - startTicks);
printf(1, " Real Time in ticks: %d tick(s) ", seconds);
Please add below code in place of the above 2 lines in your code :
int seconds = (endTicks - startTicks)/100;
int remainderSeconds = (endTicks - startTicks)%100;
printf(1, "%s", argv[1]);
printf(1, " Time in ticks: %d tick(s) ", seconds);
if (remainderSeconds < 10){
printf(1, "0");
}
printf(1, "%d ", remainderSeconds);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.