Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Time system call help xv6. time.c #include \"types.h\" #include \"user.h\" #incl

ID: 3746588 • 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 ls -s"

Explanation / Answer

You can modify the above code as :-


#include types.h
#include user.h
#include date.h

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <time.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();
}
}

if(argc==2 )
{
if (strcmp("ls", argv[1]) == 0) {
  
int endTicks = uptime();
  
int seconds = (endTicks - startTicks);
printf(1, " Real Time in ticks: %d tick(s) ", seconds);
  
}
  
}
else if(arc==3)
{
  
if (strcmp("-s", argv[2]) == 0) {
  
time_t sec;
sec = time(NULL);
printf ("Number of seconds since January 1, 1970 is %ld ", sec);
  
}
  
  
}
exit();
}

Hope this helps.

Thanks.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote