SlideShare a Scribd company logo
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 {
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;
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] == ' '){
// 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;
}
struct cmd*
redircmd(struct cmd *subcmd, char *file, int type)
{
struct redircmd *cmd;
cmd = malloc(sizeof(*cmd));
memset(cmd, 0, sizeof(*cmd));
cmd->type = type;
cmd->cmd = subcmd;
cmd->file = file;
cmd->mode = (type == '<') ? O_RDONLY : O_WRONLY|O_CREAT|O_TRUNC;
cmd->fd = (type == '<') ? 0 : 1;
return (struct cmd*)cmd;
}
struct cmd*
pipecmd(struct cmd *left, struct cmd *right)
{
struct pipecmd *cmd;
cmd = malloc(sizeof(*cmd));
memset(cmd, 0, sizeof(*cmd));
cmd->type = '|';
cmd->left = left;
cmd->right = right;
return (struct cmd*)cmd;
}
// Parsing
char whitespace[] = " trnv";
char symbols[] = "<|>";
int
gettoken(char **ps, char *es, char **q, char **eq)
{
char *s;
int ret;
s = *ps;
while(s < es && strchr(whitespace, *s))
s++;
if(q)
*q = s;
ret = *s;
switch(*s){
case 0:
break;
case '|':
case '<':
s++;
break;
case '>':
s++;
break;
default:
ret = 'a';
while(s < es && !strchr(whitespace, *s) && !strchr(symbols, *s))
s++;
break;
}
if(eq)
*eq = s;
while(s < es && strchr(whitespace, *s))
s++;
*ps = s;
return ret;
}
int
peek(char **ps, char *es, char *toks)
{
char *s;
s = *ps;
while(s < es && strchr(whitespace, *s))
s++;
*ps = s;
return *s && strchr(toks, *s);
}
struct cmd *parseline(char**, char*);
struct cmd *parsepipe(char**, char*);
struct cmd *parseexec(char**, char*);
// make a copy of the characters in the input buffer, starting from s through es.
// null-terminate the copy to make it a string.
char
*mkcopy(char *s, char *es)
{
int n = es - s;
char *c = malloc(n+1);
assert(c);
strncpy(c, s, n);
c[n] = 0;
return c;
}
struct cmd*
parsecmd(char *s)
{
char *es;
struct cmd *cmd;
es = s + strlen(s);
cmd = parseline(&s, es);
peek(&s, es, "");
if(s != es){
fprintf(stderr, "leftovers: %sn", s);
exit(-1);
}
return cmd;
}
struct cmd*
parseline(char **ps, char *es)
{
struct cmd *cmd;
cmd = parsepipe(ps, es);
return cmd;
}
struct cmd*
parsepipe(char **ps, char *es)
{
struct cmd *cmd;
cmd = parseexec(ps, es);
if(peek(ps, es, "|")){
gettoken(ps, es, 0, 0);
cmd = pipecmd(cmd, parsepipe(ps, es));
}
return cmd;
}
struct cmd*
parseredirs(struct cmd *cmd, char **ps, char *es)
{
int tok;
char *q, *eq;
while(peek(ps, es, "<>")){
tok = gettoken(ps, es, 0, 0);
if(gettoken(ps, es, &q, &eq) != 'a') {
fprintf(stderr, "missing file for redirectionn");
exit(-1);
}
switch(tok){
case '<':
cmd = redircmd(cmd, mkcopy(q, eq), '<');
break;
case '>':
cmd = redircmd(cmd, mkcopy(q, eq), '>');
break;
}
}
return cmd;
}
struct cmd*
parseexec(char **ps, char *es)
{
char *q, *eq;
int tok, argc;
struct execcmd *cmd;
struct cmd *ret;
ret = execcmd();
cmd = (struct execcmd*)ret;
argc = 0;
ret = parseredirs(ret, ps, es);
while(!peek(ps, es, "|")){
if((tok=gettoken(ps, es, &q, &eq)) == 0)
break;
if(tok != 'a') {
fprintf(stderr, "syntax errorn");
exit(-1);
}
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"

More Related Content

Similar to Shell to be modified#include stdlib.h #include unistd.h .pdf

System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbsSystem Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
ashukiller7
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
Programming Exam Help
 
Write a C++ program 1. Study the function process_text() in file.pdf
Write a C++ program 1. Study the function process_text() in file.pdfWrite a C++ program 1. Study the function process_text() in file.pdf
Write a C++ program 1. Study the function process_text() in file.pdf
jillisacebi75827
 
operating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdfoperating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdf
aptcomputerzone
 
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321Teddy Hsiung
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
javaTpoint s
 
-- C Program How might I modify this code to handle any number of comm.docx
-- C Program How might I modify this code to handle any number of comm.docx-- C Program How might I modify this code to handle any number of comm.docx
-- C Program How might I modify this code to handle any number of comm.docx
asser7
 
-- C Program How might I modify this code to handle any number of comm.docx
-- C Program How might I modify this code to handle any number of comm.docx-- C Program How might I modify this code to handle any number of comm.docx
-- C Program How might I modify this code to handle any number of comm.docx
hoggardbennie
 
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
 Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
MARRY7
 
Sysprog 13
Sysprog 13Sysprog 13
Sysprog 13
Ahmed Mekkawy
 
Gps c
Gps cGps c
Unit 8
Unit 8Unit 8
Unit 8siddr
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
Programming Homework Help
 
Usp
UspUsp
Rust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineRust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command Line
Matt Provost
 
Linux seccomp(2) vs OpenBSD pledge(2)
Linux seccomp(2) vs OpenBSD pledge(2)Linux seccomp(2) vs OpenBSD pledge(2)
Linux seccomp(2) vs OpenBSD pledge(2)
Giovanni Bechis
 
Sysprog17
Sysprog17Sysprog17
Sysprog17
Ahmed Mekkawy
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in cgkgaur1987
 
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the bfinalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
ChereCheek752
 

Similar to Shell to be modified#include stdlib.h #include unistd.h .pdf (20)

System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbsSystem Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
Write a C++ program 1. Study the function process_text() in file.pdf
Write a C++ program 1. Study the function process_text() in file.pdfWrite a C++ program 1. Study the function process_text() in file.pdf
Write a C++ program 1. Study the function process_text() in file.pdf
 
operating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdfoperating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdf
 
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
-- C Program How might I modify this code to handle any number of comm.docx
-- C Program How might I modify this code to handle any number of comm.docx-- C Program How might I modify this code to handle any number of comm.docx
-- C Program How might I modify this code to handle any number of comm.docx
 
-- C Program How might I modify this code to handle any number of comm.docx
-- C Program How might I modify this code to handle any number of comm.docx-- C Program How might I modify this code to handle any number of comm.docx
-- C Program How might I modify this code to handle any number of comm.docx
 
Npc14
Npc14Npc14
Npc14
 
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
 Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
 
Sysprog 13
Sysprog 13Sysprog 13
Sysprog 13
 
Gps c
Gps cGps c
Gps c
 
Unit 8
Unit 8Unit 8
Unit 8
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
 
Usp
UspUsp
Usp
 
Rust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineRust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command Line
 
Linux seccomp(2) vs OpenBSD pledge(2)
Linux seccomp(2) vs OpenBSD pledge(2)Linux seccomp(2) vs OpenBSD pledge(2)
Linux seccomp(2) vs OpenBSD pledge(2)
 
Sysprog17
Sysprog17Sysprog17
Sysprog17
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in c
 
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the bfinalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
 

More from clarityvision

public class SLLT { protected SLLNodeT head, tail; pub.pdf
public class SLLT {     protected SLLNodeT head, tail;     pub.pdfpublic class SLLT {     protected SLLNodeT head, tail;     pub.pdf
public class SLLT { protected SLLNodeT head, tail; pub.pdf
clarityvision
 
Q1How would this be explained in the witness box, how would this lo.pdf
Q1How would this be explained in the witness box, how would this lo.pdfQ1How would this be explained in the witness box, how would this lo.pdf
Q1How would this be explained in the witness box, how would this lo.pdf
clarityvision
 
Professor Lessiarty directs an underground network of n criminal maste.pdf
Professor Lessiarty directs an underground network of n criminal maste.pdfProfessor Lessiarty directs an underground network of n criminal maste.pdf
Professor Lessiarty directs an underground network of n criminal maste.pdf
clarityvision
 
Project Advanced Inventory Management System Upgrade for ABCBio.pdf
Project Advanced Inventory Management System Upgrade for ABCBio.pdfProject Advanced Inventory Management System Upgrade for ABCBio.pdf
Project Advanced Inventory Management System Upgrade for ABCBio.pdf
clarityvision
 
Scenario 3 Capacity and Legality Micah was a talented tennis player a.pdf
Scenario 3 Capacity and Legality  Micah was a talented tennis player a.pdfScenario 3 Capacity and Legality  Micah was a talented tennis player a.pdf
Scenario 3 Capacity and Legality Micah was a talented tennis player a.pdf
clarityvision
 
Scenario 2 Types of Contracts Dr. Brown was an adjunct instructor at.pdf
Scenario 2 Types of Contracts  Dr. Brown was an adjunct instructor at.pdfScenario 2 Types of Contracts  Dr. Brown was an adjunct instructor at.pdf
Scenario 2 Types of Contracts Dr. Brown was an adjunct instructor at.pdf
clarityvision
 
PROJECT 2Construction of a Medical ClinicMany people were severe.pdf
PROJECT 2Construction of a Medical ClinicMany people were severe.pdfPROJECT 2Construction of a Medical ClinicMany people were severe.pdf
PROJECT 2Construction of a Medical ClinicMany people were severe.pdf
clarityvision
 
Procuring textiles made from recycled fibresBackgroundThe Mini.pdf
Procuring textiles made from recycled fibresBackgroundThe Mini.pdfProcuring textiles made from recycled fibresBackgroundThe Mini.pdf
Procuring textiles made from recycled fibresBackgroundThe Mini.pdf
clarityvision
 
Read the following excerpt and answer the questions that followPle.pdf
Read the following excerpt and answer the questions that followPle.pdfRead the following excerpt and answer the questions that followPle.pdf
Read the following excerpt and answer the questions that followPle.pdf
clarityvision
 
Question 4 Read the case study about Ms Zandi and then answer the fo.pdf
Question 4 Read the case study about Ms Zandi and then answer the fo.pdfQuestion 4 Read the case study about Ms Zandi and then answer the fo.pdf
Question 4 Read the case study about Ms Zandi and then answer the fo.pdf
clarityvision
 

More from clarityvision (10)

public class SLLT { protected SLLNodeT head, tail; pub.pdf
public class SLLT {     protected SLLNodeT head, tail;     pub.pdfpublic class SLLT {     protected SLLNodeT head, tail;     pub.pdf
public class SLLT { protected SLLNodeT head, tail; pub.pdf
 
Q1How would this be explained in the witness box, how would this lo.pdf
Q1How would this be explained in the witness box, how would this lo.pdfQ1How would this be explained in the witness box, how would this lo.pdf
Q1How would this be explained in the witness box, how would this lo.pdf
 
Professor Lessiarty directs an underground network of n criminal maste.pdf
Professor Lessiarty directs an underground network of n criminal maste.pdfProfessor Lessiarty directs an underground network of n criminal maste.pdf
Professor Lessiarty directs an underground network of n criminal maste.pdf
 
Project Advanced Inventory Management System Upgrade for ABCBio.pdf
Project Advanced Inventory Management System Upgrade for ABCBio.pdfProject Advanced Inventory Management System Upgrade for ABCBio.pdf
Project Advanced Inventory Management System Upgrade for ABCBio.pdf
 
Scenario 3 Capacity and Legality Micah was a talented tennis player a.pdf
Scenario 3 Capacity and Legality  Micah was a talented tennis player a.pdfScenario 3 Capacity and Legality  Micah was a talented tennis player a.pdf
Scenario 3 Capacity and Legality Micah was a talented tennis player a.pdf
 
Scenario 2 Types of Contracts Dr. Brown was an adjunct instructor at.pdf
Scenario 2 Types of Contracts  Dr. Brown was an adjunct instructor at.pdfScenario 2 Types of Contracts  Dr. Brown was an adjunct instructor at.pdf
Scenario 2 Types of Contracts Dr. Brown was an adjunct instructor at.pdf
 
PROJECT 2Construction of a Medical ClinicMany people were severe.pdf
PROJECT 2Construction of a Medical ClinicMany people were severe.pdfPROJECT 2Construction of a Medical ClinicMany people were severe.pdf
PROJECT 2Construction of a Medical ClinicMany people were severe.pdf
 
Procuring textiles made from recycled fibresBackgroundThe Mini.pdf
Procuring textiles made from recycled fibresBackgroundThe Mini.pdfProcuring textiles made from recycled fibresBackgroundThe Mini.pdf
Procuring textiles made from recycled fibresBackgroundThe Mini.pdf
 
Read the following excerpt and answer the questions that followPle.pdf
Read the following excerpt and answer the questions that followPle.pdfRead the following excerpt and answer the questions that followPle.pdf
Read the following excerpt and answer the questions that followPle.pdf
 
Question 4 Read the case study about Ms Zandi and then answer the fo.pdf
Question 4 Read the case study about Ms Zandi and then answer the fo.pdfQuestion 4 Read the case study about Ms Zandi and then answer the fo.pdf
Question 4 Read the case study about Ms Zandi and then answer the fo.pdf
 

Recently uploaded

How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 

Recently uploaded (20)

How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 

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; }
  • 5. struct cmd* redircmd(struct cmd *subcmd, char *file, int type) { struct redircmd *cmd; cmd = malloc(sizeof(*cmd)); memset(cmd, 0, sizeof(*cmd)); cmd->type = type; cmd->cmd = subcmd; cmd->file = file; cmd->mode = (type == '<') ? O_RDONLY : O_WRONLY|O_CREAT|O_TRUNC; cmd->fd = (type == '<') ? 0 : 1; return (struct cmd*)cmd; } struct cmd* pipecmd(struct cmd *left, struct cmd *right) { struct pipecmd *cmd; cmd = malloc(sizeof(*cmd)); memset(cmd, 0, sizeof(*cmd)); cmd->type = '|'; cmd->left = left; cmd->right = right; return (struct cmd*)cmd; } // Parsing char whitespace[] = " trnv"; char symbols[] = "<|>"; int gettoken(char **ps, char *es, char **q, char **eq) {
  • 6. char *s; int ret; s = *ps; while(s < es && strchr(whitespace, *s)) s++; if(q) *q = s; ret = *s; switch(*s){ case 0: break; case '|': case '<': s++; break; case '>': s++; break; default: ret = 'a'; while(s < es && !strchr(whitespace, *s) && !strchr(symbols, *s)) s++; break; } if(eq) *eq = s; while(s < es && strchr(whitespace, *s)) s++; *ps = s; return ret; } int peek(char **ps, char *es, char *toks)
  • 7. { char *s; s = *ps; while(s < es && strchr(whitespace, *s)) s++; *ps = s; return *s && strchr(toks, *s); } struct cmd *parseline(char**, char*); struct cmd *parsepipe(char**, char*); struct cmd *parseexec(char**, char*); // make a copy of the characters in the input buffer, starting from s through es. // null-terminate the copy to make it a string. char *mkcopy(char *s, char *es) { int n = es - s; char *c = malloc(n+1); assert(c); strncpy(c, s, n); c[n] = 0; return c; } struct cmd* parsecmd(char *s) { char *es; struct cmd *cmd; es = s + strlen(s); cmd = parseline(&s, es); peek(&s, es, "");
  • 8. if(s != es){ fprintf(stderr, "leftovers: %sn", s); exit(-1); } return cmd; } struct cmd* parseline(char **ps, char *es) { struct cmd *cmd; cmd = parsepipe(ps, es); return cmd; } struct cmd* parsepipe(char **ps, char *es) { struct cmd *cmd; cmd = parseexec(ps, es); if(peek(ps, es, "|")){ gettoken(ps, es, 0, 0); cmd = pipecmd(cmd, parsepipe(ps, es)); } return cmd; } struct cmd* parseredirs(struct cmd *cmd, char **ps, char *es) { int tok; char *q, *eq; while(peek(ps, es, "<>")){ tok = gettoken(ps, es, 0, 0);
  • 9. if(gettoken(ps, es, &q, &eq) != 'a') { fprintf(stderr, "missing file for redirectionn"); exit(-1); } switch(tok){ case '<': cmd = redircmd(cmd, mkcopy(q, eq), '<'); break; case '>': cmd = redircmd(cmd, mkcopy(q, eq), '>'); break; } } return cmd; } struct cmd* parseexec(char **ps, char *es) { char *q, *eq; int tok, argc; struct execcmd *cmd; struct cmd *ret; ret = execcmd(); cmd = (struct execcmd*)ret; argc = 0; ret = parseredirs(ret, ps, es); while(!peek(ps, es, "|")){ if((tok=gettoken(ps, es, &q, &eq)) == 0) break; if(tok != 'a') { fprintf(stderr, "syntax errorn"); exit(-1); }
  • 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"