Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

myshell.c #include #include #include #include #include #include #include #define MAX_LINE_SIZE 255 int main() { while ( 1 ) { fflush(0); // show prompt to user

image text in transcribed

myshell.c

#include  #include  #include  #include  #include  #include  #include  #define MAX_LINE_SIZE 255 int main() { while ( 1 ) { fflush(0); // show prompt to user printf("myshell> "); // asking user for the command char line[MAX_LINE_SIZE]; char temp[MAX_LINE_SIZE]; fgets(line, MAX_LINE_SIZE, stdin); strcpy(temp,line); int i = 0; char *words[20] = {0}; // removing the new line character from string line[ strlen(line) - 1 ] = 0; temp[ strlen(temp) - 1 ] = 0; // if blank line is entered skip command if ( strlen(line) == 0 ) continue; words[i] = strtok(line," "); while ( words[i] != NULL ) { i++; words[i] = strtok(NULL," "); } // exit if user command is exit if ( strcmp( words[0], "exit" ) == 0) exit(0); // any other command is invalid except run and exit if ( strcmp( words[0], "run" ) ) { printf("myshell: %s is not a valid command ",temp); continue; } int status; pid_t result = fork(); if ( result == 0 ){ int ret = execvp(words[1], words + 1); if ( ret == -1 ) printf("%s %s ", strerror(errno), words[1]); return ret; } printf("myshell: started child pid %d ", result); sleep(1); wait(&status); } return 0; } 
Your shell should support using ; to execute multiple programs with a single command. Foir example: --... -/myshell myshell> run ls l date who myshell: started child pid 19410 total 33 rwx-x--x 1 hb117 faculty 9132 Oct 24 11:17 a.out drwx--x--x 10 hb117 faculty 1024 Oct 6 20:28 code rw1 hb117 faculty 1706 Oct 7 12:58 duplicatefile.c -rw--1 hb117 rwx-x--x 1 hb117 faculty 13331 Oct 26 21:07 myshell rw1 hb117 faculty 1798 Oct 24 11:18 myshel12.c rw1 hb117 faculty 2839 Oct 23 21:16 myshell.c drwx--x--x 2 hb117 faculty drwx-xr-x 2 hb117 faculty rw1 hb117 faculty 392 Oct 19 08:54 test.c myshell: started child pid 19411 Wed Sep 20 21:07:56 CDT 2017 myshell: started child pid 19412 jwb128 pts/4 mflll pts/6 tcs113 pts/8 hb117 myshell> faculty476 Oct 19 13:15 exec.c 96 Sep 23 13:05 po 96 Oct 4 15:46 pl 2017-9-20 11:11 (:7) 2017-9-20 18:56 (:1) 2017-9-20 20:21 (10.164.71.161) 2017-9-20 20:38 (c-71-201-31-239:S.0) pts/7

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions