Science 7 - LAND and SEA BREEZE and its Characteristics
Shell to be modified#include stdlib.h #include unistd.h .pdf
1. Shell to be modified
#include
#include
#include
#include
#include
#include
#include
#include
// Simplifed xv6 shell.
#define MAXARGS 10
// All commands have at least a type. Have looked at the type, the code
// typically casts the *cmd to some specific cmd type.
struct cmd {
int type; // ' ' (exec), | (pipe), '<' or '>' for redirection
};
struct execcmd {
int type; // ' '
char *argv[MAXARGS]; // arguments to the command to be exec-ed
};
struct redircmd {
int type; // < or >
struct cmd *cmd; // the command to be run (e.g., an execcmd)
char *file; // the input/output file
int mode; // the mode to open the file with
int fd; // the file descriptor number to use for the file
};
struct pipecmd {
2. int type; // |
struct cmd *left; // left side of pipe
struct cmd *right; // right side of pipe
};
int fork1(void); // Fork but exits on failure.
struct cmd *parsecmd(char*);
// Execute cmd. Never returns.
void
runcmd(struct cmd *cmd)
{
int p[2], r;
struct execcmd *ecmd;
struct pipecmd *pcmd;
struct redircmd *rcmd;
if(cmd == 0)
exit(0);
switch(cmd->type){
default:
fprintf(stderr, "unknown runcmdn");
exit(-1);
case ' ':
ecmd = (struct execcmd*)cmd;
if(ecmd->argv[0] == 0)
exit(0);
fprintf(stderr, "exec not implementedn");
// Your code here ...
break;
case '>':
case '<':
rcmd = (struct redircmd*)cmd;
3. fprintf(stderr, "redir not implementedn");
// Your code here ...
runcmd(rcmd->cmd);
break;
case '|':
pcmd = (struct pipecmd*)cmd;
fprintf(stderr, "pipe not implementedn");
// Your code here ...
break;
}
exit(0);
}
int
getcmd(char *buf, int nbuf)
{
if (isatty(fileno(stdin)))
fprintf(stdout, "$ ");
memset(buf, 0, nbuf);
fgets(buf, nbuf, stdin);
if(buf[0] == 0) // EOF
return -1;
return 0;
}
int
main(void)
{
static char buf[100];
int fd, r;
// Read and run input commands.
while(getcmd(buf, sizeof(buf)) >= 0){
if(buf[0] == 'c' && buf[1] == 'd' && buf[2] == ' '){
4. // Clumsy but will have to do for now.
// Chdir has no effect on the parent if run in the child.
buf[strlen(buf)-1] = 0; // chop n
if(chdir(buf+3) < 0)
fprintf(stderr, "cannot cd %sn", buf+3);
continue;
}
if(fork1() == 0)
runcmd(parsecmd(buf));
wait(&r);
}
exit(0);
}
int
fork1(void)
{
int pid;
pid = fork();
if(pid == -1)
perror("fork");
return pid;
}
struct cmd*
execcmd(void)
{
struct execcmd *cmd;
cmd = malloc(sizeof(*cmd));
memset(cmd, 0, sizeof(*cmd));
cmd->type = ' ';
return (struct cmd*)cmd;
}
10. cmd->argv[argc] = mkcopy(q, eq);
argc++;
if(argc >= MAXARGS) {
fprintf(stderr, "too many argsn");
exit(-1);
}
ret = parseredirs(ret, ps, es);
}
cmd->argv[argc] = 0;
return ret;
}
This assignment consists of three parts. The first part asks you to trace the execution of the open
() system call. The second part asks you to implement a system call that will allow a user
program to obtain the number of operating system traps that the program has encountered since it
started running. The third part asks you to modify the xv6 scheduling algorithm so that it
considers the number of traps that a program has encountered. 1) (15 points) You will trace the
open () system call starting from the time that it is called from a user program to when its trap
handler is invoked. We ask you to provide two traces, one when the system call is successful and
the other when it is not. For each trace, we ask you to write down in a document which lines in
which files of xv6 are executed. Organize the lines of code into blocks of code and explain the
purpose of each of block. The result will be a story of what happened after open ( ) is called. The
story starts in the user space when the system call first gets executed and ends when the system
call returns. 2) (15 points) You will implement the system call Count Traps () that will return the
number of traps a user program has encountered since it begins running. 3) (a) (5 points)
Describe the existing scheduling policy in xv6. (b) (20 points) You will modify the scheduling
algorithm in xv6 such that the program that has encountered the most traps will be executed first.
You will need to understand how xv6 handles timer interrupts and process preemption. The xv6
code for these functionalities is basically in proc.c and trap.c. Chapter 6 of the xv6 book will be
helpful. (c) (15 points) Write the programs that will help you test and demonstrate your result
P(b). (d) (5 points) Criticize the scheduling policy in (b). Propose how you can make it better
(Hint: what does "better"