SlideShare a Scribd company logo
1 of 4
Download to read offline
Write a shell in C/C++
**Prefer this to be written in C++**
Your shell should use the fork system call and the execv system call (or one of its variants) to
execute commands. The shell needs to use either wait or waitpid system calls to wait for a
program to complete execution. You should recognize the command exit to mean that your shell
program should terminate by calling the exit() system call.
A very simple shell such as this needs at least the following components:
a command-line parser to figure out what the user is trying to do.
If a valid command has been entered, the shell should use fork to create a new child process, and
the child process should exec the command.
Your shell will need to support file redirection. Use the same syntax as defined in the Bash shell:
a single '>' implies that one needs to redirect the standard output of the program being started to
the referenced file while a single '<' implies the same with standard input. The double '>>'
implies that standard output will append to an existing file rather than create a new file (similar
behavior is required for the '<<' operator and standard input). You do not need to implement
support for the Bash pipeline operator '|'.
Before calling exec to begin execution, the child process may have to close stdin (file desriptor
0) and/or stdout (file descriptor 0), open the corresponding file and use the dup2 system call to
make it the appropriate file descriptor. Don't forget to use the close system call to close the old
file descriptor.
Solution
Shell program in C++
launching program:
int lsh_launch(char **args)
{
pid_t pid, wpid;
int status;
pid = fork();
if (pid == 0) {
// Child process
if (execvp(args[0], args) == -1) {
perror("lsh");
}
exit(EXIT_FAILURE);
} else if (pid < 0) {
// Error forking
perror("lsh");
} else {
// Parent process
do {
wpid = waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
Commands to shell:
int lsh_cd(char **args);
int lsh_help(char **args);
int lsh_exit(char **args);
/*
List of builtin commands, followed by their corresponding functions.
*/
char *builtin_str[] = {
"cd",
"help",
"exit"
};
int (*builtin_func[]) (char **) = {
&lsh_cd,
&lsh_help,
&lsh_exit
};
int lsh_num_builtins() {
return sizeof(builtin_str) / sizeof(char *);
}
/*
Builtin function implementations.
*/
int lsh_cd(char **args)
{
if (args[1] == NULL) {
fprintf(stderr, "lsh: expected argument to "cd" ");
} else {
if (chdir(args[1]) != 0) {
perror("lsh");
}
}
return 1;
}
int lsh_help(char **args)
{
int i;
printf("This is shell program ");
printf("Type programs names and arguments, and hit enter. ");
printf("The following are built in: ");
for (i = 0; i < lsh_num_builtins(); i++) {
printf(" %s ", builtin_str[i]);
}
printf("Use command for information on other programs. ");
return 1;
}
int lsh_exit(char **args)
{
return 0;
}
implement Ish_execute():
int lsh_execute(char **args)
{
int i;
if (args[0] == NULL) {
// An empty command was entered.
return 1;
}
for (i = 0; i < lsh_num_builtins(); i++) {
if (strcmp(args[0], builtin_str[i]) == 0) {
return (*builtin_func[i])(args);
}
}
return lsh_launch(args);
}

More Related Content

Similar to Write C++ Shell with System Calls

Program Assignment Process ManagementObjective This program a.docx
Program Assignment  Process ManagementObjective This program a.docxProgram Assignment  Process ManagementObjective This program a.docx
Program Assignment Process ManagementObjective This program a.docxwkyra78
 
What is-a-computer-process-os
What is-a-computer-process-osWhat is-a-computer-process-os
What is-a-computer-process-osManish Singh
 
httplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docxhttplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docxadampcarr67227
 
11_UNIX_Processes_Including_Select.ppt
11_UNIX_Processes_Including_Select.ppt11_UNIX_Processes_Including_Select.ppt
11_UNIX_Processes_Including_Select.pptSIDDHARTHANANDCSE202
 
Write a C program called pross-c to implement the UNIX-Linux equivalen.docx
Write a C program called pross-c to implement the UNIX-Linux equivalen.docxWrite a C program called pross-c to implement the UNIX-Linux equivalen.docx
Write a C program called pross-c to implement the UNIX-Linux equivalen.docxSUKHI5
 
OS presentation (1).pptx
OS presentation (1).pptxOS presentation (1).pptx
OS presentation (1).pptxJenish62
 
1 of 9 CSCE 3600 Systems Programming Major Assignm.docx
  1 of 9 CSCE 3600 Systems Programming  Major Assignm.docx  1 of 9 CSCE 3600 Systems Programming  Major Assignm.docx
1 of 9 CSCE 3600 Systems Programming Major Assignm.docxShiraPrater50
 
Unix And Shell Scripting
Unix And Shell ScriptingUnix And Shell Scripting
Unix And Shell ScriptingJaibeer Malik
 
Can someone put this code in a zip file. I tried running it last tim.pdf
Can someone put this code in a zip file. I tried running it last tim.pdfCan someone put this code in a zip file. I tried running it last tim.pdf
Can someone put this code in a zip file. I tried running it last tim.pdffedosys
 
PHP Backdoor: The rise of the vuln
PHP Backdoor: The rise of the vulnPHP Backdoor: The rise of the vuln
PHP Backdoor: The rise of the vulnSandro Zaccarini
 
Recipe to build open splice dds 6.3.xxx Hello World example over Qt 5.2
 Recipe to build open splice dds 6.3.xxx Hello World example over Qt 5.2   Recipe to build open splice dds 6.3.xxx Hello World example over Qt 5.2
Recipe to build open splice dds 6.3.xxx Hello World example over Qt 5.2 Adil Khan
 
04_ForkPipe.pptx
04_ForkPipe.pptx04_ForkPipe.pptx
04_ForkPipe.pptxvnwzympx
 

Similar to Write C++ Shell with System Calls (20)

Program Assignment Process ManagementObjective This program a.docx
Program Assignment  Process ManagementObjective This program a.docxProgram Assignment  Process ManagementObjective This program a.docx
Program Assignment Process ManagementObjective This program a.docx
 
Systemcall1
Systemcall1Systemcall1
Systemcall1
 
File management
File managementFile management
File management
 
What is-a-computer-process-os
What is-a-computer-process-osWhat is-a-computer-process-os
What is-a-computer-process-os
 
httplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docxhttplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docx
 
11_UNIX_Processes_Including_Select.ppt
11_UNIX_Processes_Including_Select.ppt11_UNIX_Processes_Including_Select.ppt
11_UNIX_Processes_Including_Select.ppt
 
Write a C program called pross-c to implement the UNIX-Linux equivalen.docx
Write a C program called pross-c to implement the UNIX-Linux equivalen.docxWrite a C program called pross-c to implement the UNIX-Linux equivalen.docx
Write a C program called pross-c to implement the UNIX-Linux equivalen.docx
 
OS_lab_file.pdf
OS_lab_file.pdfOS_lab_file.pdf
OS_lab_file.pdf
 
OS presentation (1).pptx
OS presentation (1).pptxOS presentation (1).pptx
OS presentation (1).pptx
 
1 of 9 CSCE 3600 Systems Programming Major Assignm.docx
  1 of 9 CSCE 3600 Systems Programming  Major Assignm.docx  1 of 9 CSCE 3600 Systems Programming  Major Assignm.docx
1 of 9 CSCE 3600 Systems Programming Major Assignm.docx
 
Unix And Shell Scripting
Unix And Shell ScriptingUnix And Shell Scripting
Unix And Shell Scripting
 
File io
File ioFile io
File io
 
Can someone put this code in a zip file. I tried running it last tim.pdf
Can someone put this code in a zip file. I tried running it last tim.pdfCan someone put this code in a zip file. I tried running it last tim.pdf
Can someone put this code in a zip file. I tried running it last tim.pdf
 
Linux Programming
Linux ProgrammingLinux Programming
Linux Programming
 
PHP Backdoor: The rise of the vuln
PHP Backdoor: The rise of the vulnPHP Backdoor: The rise of the vuln
PHP Backdoor: The rise of the vuln
 
Multithreading in PHP
Multithreading in PHPMultithreading in PHP
Multithreading in PHP
 
Fork handout
Fork handoutFork handout
Fork handout
 
Unix kernal
Unix kernalUnix kernal
Unix kernal
 
Recipe to build open splice dds 6.3.xxx Hello World example over Qt 5.2
 Recipe to build open splice dds 6.3.xxx Hello World example over Qt 5.2   Recipe to build open splice dds 6.3.xxx Hello World example over Qt 5.2
Recipe to build open splice dds 6.3.xxx Hello World example over Qt 5.2
 
04_ForkPipe.pptx
04_ForkPipe.pptx04_ForkPipe.pptx
04_ForkPipe.pptx
 

More from forladies

You isolated an enveloped RNA virus. Purified RNA is not capable of .pdf
You isolated an enveloped RNA virus. Purified RNA is not capable of .pdfYou isolated an enveloped RNA virus. Purified RNA is not capable of .pdf
You isolated an enveloped RNA virus. Purified RNA is not capable of .pdfforladies
 
What properties should the following molecules NOT have in common (S.pdf
What properties should the following molecules NOT have in common (S.pdfWhat properties should the following molecules NOT have in common (S.pdf
What properties should the following molecules NOT have in common (S.pdfforladies
 
What is the difference between Schedule C and Schedule E income (i.e.pdf
What is the difference between Schedule C and Schedule E income (i.e.pdfWhat is the difference between Schedule C and Schedule E income (i.e.pdf
What is the difference between Schedule C and Schedule E income (i.e.pdfforladies
 
What are the ethical and legal concerns associated with managing tel.pdf
What are the ethical and legal concerns associated with managing tel.pdfWhat are the ethical and legal concerns associated with managing tel.pdf
What are the ethical and legal concerns associated with managing tel.pdfforladies
 
Using the Graphical User Interface (GUI)Create a user nam.pdf
Using the Graphical User Interface (GUI)Create a user nam.pdfUsing the Graphical User Interface (GUI)Create a user nam.pdf
Using the Graphical User Interface (GUI)Create a user nam.pdfforladies
 
The adjusting entry to record the salaries earned due to employees f.pdf
The adjusting entry to record the salaries earned due to employees f.pdfThe adjusting entry to record the salaries earned due to employees f.pdf
The adjusting entry to record the salaries earned due to employees f.pdfforladies
 
TF A document type definition (DTD) can be referenced by many Exten.pdf
TF A document type definition (DTD) can be referenced by many Exten.pdfTF A document type definition (DTD) can be referenced by many Exten.pdf
TF A document type definition (DTD) can be referenced by many Exten.pdfforladies
 
Summarize the first and the second checkpoints during T cell develop.pdf
Summarize the first and the second checkpoints during T cell develop.pdfSummarize the first and the second checkpoints during T cell develop.pdf
Summarize the first and the second checkpoints during T cell develop.pdfforladies
 
PLEASE HELP!!Loren Seguara and Dale Johnson both work for Southern.pdf
PLEASE HELP!!Loren Seguara and Dale Johnson both work for Southern.pdfPLEASE HELP!!Loren Seguara and Dale Johnson both work for Southern.pdf
PLEASE HELP!!Loren Seguara and Dale Johnson both work for Southern.pdfforladies
 
Please answer the following at the bottom of the case. ThanksTo ne.pdf
Please answer the following at the bottom of the case. ThanksTo ne.pdfPlease answer the following at the bottom of the case. ThanksTo ne.pdf
Please answer the following at the bottom of the case. ThanksTo ne.pdfforladies
 
Mitchell sets sail for the Chemiosmotic New World, despite dire w.pdf
Mitchell sets sail for the Chemiosmotic New World, despite dire w.pdfMitchell sets sail for the Chemiosmotic New World, despite dire w.pdf
Mitchell sets sail for the Chemiosmotic New World, despite dire w.pdfforladies
 
Learn the genetics vocabulary (see HW4)] For each of the following ge.pdf
Learn the genetics vocabulary (see HW4)] For each of the following ge.pdfLearn the genetics vocabulary (see HW4)] For each of the following ge.pdf
Learn the genetics vocabulary (see HW4)] For each of the following ge.pdfforladies
 
If nominal GDP is 28000 and the money supply is 7000, what is velocit.pdf
If nominal GDP is 28000 and the money supply is 7000, what is velocit.pdfIf nominal GDP is 28000 and the money supply is 7000, what is velocit.pdf
If nominal GDP is 28000 and the money supply is 7000, what is velocit.pdfforladies
 
Investments in trade securities are always short term investments. T.pdf
Investments in trade securities are always short term investments. T.pdfInvestments in trade securities are always short term investments. T.pdf
Investments in trade securities are always short term investments. T.pdfforladies
 
Information Securityfind an article online discussing defense-in-d.pdf
Information Securityfind an article online discussing defense-in-d.pdfInformation Securityfind an article online discussing defense-in-d.pdf
Information Securityfind an article online discussing defense-in-d.pdfforladies
 
implement the following funtions. myg1 and myg2 are seperate. x and .pdf
implement the following funtions. myg1 and myg2 are seperate. x and .pdfimplement the following funtions. myg1 and myg2 are seperate. x and .pdf
implement the following funtions. myg1 and myg2 are seperate. x and .pdfforladies
 
If two peers share a link in the overlay (they are neighbors in the .pdf
If two peers share a link in the overlay (they are neighbors in the .pdfIf two peers share a link in the overlay (they are neighbors in the .pdf
If two peers share a link in the overlay (they are neighbors in the .pdfforladies
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfforladies
 
how important is Negative Emotionality to an accounting career plea.pdf
how important is Negative Emotionality to an accounting career plea.pdfhow important is Negative Emotionality to an accounting career plea.pdf
how important is Negative Emotionality to an accounting career plea.pdfforladies
 
How do I know whether miscellaneous expense goes on top or bottom of.pdf
How do I know whether miscellaneous expense goes on top or bottom of.pdfHow do I know whether miscellaneous expense goes on top or bottom of.pdf
How do I know whether miscellaneous expense goes on top or bottom of.pdfforladies
 

More from forladies (20)

You isolated an enveloped RNA virus. Purified RNA is not capable of .pdf
You isolated an enveloped RNA virus. Purified RNA is not capable of .pdfYou isolated an enveloped RNA virus. Purified RNA is not capable of .pdf
You isolated an enveloped RNA virus. Purified RNA is not capable of .pdf
 
What properties should the following molecules NOT have in common (S.pdf
What properties should the following molecules NOT have in common (S.pdfWhat properties should the following molecules NOT have in common (S.pdf
What properties should the following molecules NOT have in common (S.pdf
 
What is the difference between Schedule C and Schedule E income (i.e.pdf
What is the difference between Schedule C and Schedule E income (i.e.pdfWhat is the difference between Schedule C and Schedule E income (i.e.pdf
What is the difference between Schedule C and Schedule E income (i.e.pdf
 
What are the ethical and legal concerns associated with managing tel.pdf
What are the ethical and legal concerns associated with managing tel.pdfWhat are the ethical and legal concerns associated with managing tel.pdf
What are the ethical and legal concerns associated with managing tel.pdf
 
Using the Graphical User Interface (GUI)Create a user nam.pdf
Using the Graphical User Interface (GUI)Create a user nam.pdfUsing the Graphical User Interface (GUI)Create a user nam.pdf
Using the Graphical User Interface (GUI)Create a user nam.pdf
 
The adjusting entry to record the salaries earned due to employees f.pdf
The adjusting entry to record the salaries earned due to employees f.pdfThe adjusting entry to record the salaries earned due to employees f.pdf
The adjusting entry to record the salaries earned due to employees f.pdf
 
TF A document type definition (DTD) can be referenced by many Exten.pdf
TF A document type definition (DTD) can be referenced by many Exten.pdfTF A document type definition (DTD) can be referenced by many Exten.pdf
TF A document type definition (DTD) can be referenced by many Exten.pdf
 
Summarize the first and the second checkpoints during T cell develop.pdf
Summarize the first and the second checkpoints during T cell develop.pdfSummarize the first and the second checkpoints during T cell develop.pdf
Summarize the first and the second checkpoints during T cell develop.pdf
 
PLEASE HELP!!Loren Seguara and Dale Johnson both work for Southern.pdf
PLEASE HELP!!Loren Seguara and Dale Johnson both work for Southern.pdfPLEASE HELP!!Loren Seguara and Dale Johnson both work for Southern.pdf
PLEASE HELP!!Loren Seguara and Dale Johnson both work for Southern.pdf
 
Please answer the following at the bottom of the case. ThanksTo ne.pdf
Please answer the following at the bottom of the case. ThanksTo ne.pdfPlease answer the following at the bottom of the case. ThanksTo ne.pdf
Please answer the following at the bottom of the case. ThanksTo ne.pdf
 
Mitchell sets sail for the Chemiosmotic New World, despite dire w.pdf
Mitchell sets sail for the Chemiosmotic New World, despite dire w.pdfMitchell sets sail for the Chemiosmotic New World, despite dire w.pdf
Mitchell sets sail for the Chemiosmotic New World, despite dire w.pdf
 
Learn the genetics vocabulary (see HW4)] For each of the following ge.pdf
Learn the genetics vocabulary (see HW4)] For each of the following ge.pdfLearn the genetics vocabulary (see HW4)] For each of the following ge.pdf
Learn the genetics vocabulary (see HW4)] For each of the following ge.pdf
 
If nominal GDP is 28000 and the money supply is 7000, what is velocit.pdf
If nominal GDP is 28000 and the money supply is 7000, what is velocit.pdfIf nominal GDP is 28000 and the money supply is 7000, what is velocit.pdf
If nominal GDP is 28000 and the money supply is 7000, what is velocit.pdf
 
Investments in trade securities are always short term investments. T.pdf
Investments in trade securities are always short term investments. T.pdfInvestments in trade securities are always short term investments. T.pdf
Investments in trade securities are always short term investments. T.pdf
 
Information Securityfind an article online discussing defense-in-d.pdf
Information Securityfind an article online discussing defense-in-d.pdfInformation Securityfind an article online discussing defense-in-d.pdf
Information Securityfind an article online discussing defense-in-d.pdf
 
implement the following funtions. myg1 and myg2 are seperate. x and .pdf
implement the following funtions. myg1 and myg2 are seperate. x and .pdfimplement the following funtions. myg1 and myg2 are seperate. x and .pdf
implement the following funtions. myg1 and myg2 are seperate. x and .pdf
 
If two peers share a link in the overlay (they are neighbors in the .pdf
If two peers share a link in the overlay (they are neighbors in the .pdfIf two peers share a link in the overlay (they are neighbors in the .pdf
If two peers share a link in the overlay (they are neighbors in the .pdf
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
 
how important is Negative Emotionality to an accounting career plea.pdf
how important is Negative Emotionality to an accounting career plea.pdfhow important is Negative Emotionality to an accounting career plea.pdf
how important is Negative Emotionality to an accounting career plea.pdf
 
How do I know whether miscellaneous expense goes on top or bottom of.pdf
How do I know whether miscellaneous expense goes on top or bottom of.pdfHow do I know whether miscellaneous expense goes on top or bottom of.pdf
How do I know whether miscellaneous expense goes on top or bottom of.pdf
 

Recently uploaded

Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 

Recently uploaded (20)

Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 

Write C++ Shell with System Calls

  • 1. Write a shell in C/C++ **Prefer this to be written in C++** Your shell should use the fork system call and the execv system call (or one of its variants) to execute commands. The shell needs to use either wait or waitpid system calls to wait for a program to complete execution. You should recognize the command exit to mean that your shell program should terminate by calling the exit() system call. A very simple shell such as this needs at least the following components: a command-line parser to figure out what the user is trying to do. If a valid command has been entered, the shell should use fork to create a new child process, and the child process should exec the command. Your shell will need to support file redirection. Use the same syntax as defined in the Bash shell: a single '>' implies that one needs to redirect the standard output of the program being started to the referenced file while a single '<' implies the same with standard input. The double '>>' implies that standard output will append to an existing file rather than create a new file (similar behavior is required for the '<<' operator and standard input). You do not need to implement support for the Bash pipeline operator '|'. Before calling exec to begin execution, the child process may have to close stdin (file desriptor 0) and/or stdout (file descriptor 0), open the corresponding file and use the dup2 system call to make it the appropriate file descriptor. Don't forget to use the close system call to close the old file descriptor. Solution Shell program in C++ launching program: int lsh_launch(char **args) { pid_t pid, wpid; int status; pid = fork(); if (pid == 0) { // Child process if (execvp(args[0], args) == -1) { perror("lsh"); }
  • 2. exit(EXIT_FAILURE); } else if (pid < 0) { // Error forking perror("lsh"); } else { // Parent process do { wpid = waitpid(pid, &status, WUNTRACED); } while (!WIFEXITED(status) && !WIFSIGNALED(status)); } return 1; } Commands to shell: int lsh_cd(char **args); int lsh_help(char **args); int lsh_exit(char **args); /* List of builtin commands, followed by their corresponding functions. */ char *builtin_str[] = { "cd", "help", "exit" }; int (*builtin_func[]) (char **) = { &lsh_cd, &lsh_help, &lsh_exit }; int lsh_num_builtins() { return sizeof(builtin_str) / sizeof(char *); } /* Builtin function implementations. */ int lsh_cd(char **args)
  • 3. { if (args[1] == NULL) { fprintf(stderr, "lsh: expected argument to "cd" "); } else { if (chdir(args[1]) != 0) { perror("lsh"); } } return 1; } int lsh_help(char **args) { int i; printf("This is shell program "); printf("Type programs names and arguments, and hit enter. "); printf("The following are built in: "); for (i = 0; i < lsh_num_builtins(); i++) { printf(" %s ", builtin_str[i]); } printf("Use command for information on other programs. "); return 1; } int lsh_exit(char **args) { return 0; } implement Ish_execute(): int lsh_execute(char **args) { int i; if (args[0] == NULL) { // An empty command was entered. return 1; } for (i = 0; i < lsh_num_builtins(); i++) { if (strcmp(args[0], builtin_str[i]) == 0) {