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

time.c System Call in xv6: This file is the program what will implement the time

ID: 3746218 • Letter: T

Question

time.c System Call in xv6:

This file is the program what will implement the time system call. This in user space and therefore is
written in C. Implementing the time system requires you to use other xv6 system calls, in particular
uptime, fork, exec, and wait. You can find a detailed explanation of these in Chapter 0 of the xv6
textbook.

xv6 time.c system call and linkage

create a new file called time.c, and create a program that will:
• Accepts arguments from the command line interface. You are expected to do the necessary error
checking with the corresponding error message.
• Fork a child process, the parent process is responsible to measure the time it takes for the child
process to finish executing.
o Get the current time using uptime.
o Fork and then wait for the child process to terminate.
o Then when wait returns in the parent process, get the current time again and calculate the
difference.

• Return an error message if fork is unsuccessful.
• The child process is responsible for executing the command the user wishes to time using the
exec command. You must determine how you will pass the arguments from the command line.
o The child is responsible to return an error message is execution fails.

fork() Create process

wait() Wait for a child process to exit

exec(filename, *argv) Load a file and execute it

Explanation / Answer

#include "types.h"

#include "user.h"

#include "date.h"

int

main (int argc, char *argv[])

{

if(argc<2)

{

//Error handling

printf(2, "Error: Inalid usuage! ");

exit(1);

}

int startTicks = uptime();

int pid = fork();

if (pid < 0) {

printf(2, "Error: Invalid PID! ");

exit(1);

}

if (pid == 0)

{ //Child block

if (exec(argv[1], argv + 1) < 0)

{

printf(2, "Error: Exec fails! ");

exit(1);

}

}

else

{

int status;

if(wait(&status)==-1 )

{

printf(2, "Wait: failed! ");

exit(1);

}

if(status!=0)

{

printf(2, "Child: failed! ");

exit(1);

}

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();

}

}