Question: Can someone help me with the / / TO - DO P 5 . 3 section of my program. Instructions: 3 . Modify this project
Can someone help me with the TODO P section of my program. Instructions: Modify this project so that NN reexecutes the nth command entered. You can assume that NN will only be tested with values through no more than values will be entered. If it goes over loop back to use a circular queue structure Shell ls Shell # reexecutes ls Shell # reexecutes ls Shell # prints "Not valid" to stderr Shell # prints "Not valid" to stderr Test: shell test rerun SMP: Simple Shell LIBRARY SECTION #include Character types #include Standard buffered inputoutput #include Standard library functions #include String operations #include Data types #include Declarations for waiting #include Standard symbolic constants and types #include smptests.h Builtin test system DEFINE SECTION #define SHELLBUFFERSIZE Size of the Shell input buffer #define SHELLMAXARGS Maximum number of arguments parsed VARIABLE SECTION enum STATESPACE, STATENONSPACE ; Parser states int counter ; int imthechildconst char pathtoexec, char const args TODO P Split the command into its name and path components char commandname strrchrpathtoexec, ; char path strtokgetenvPATH:; int status ; Loop through each directory in the path while path NULL Construct the full path to the executable file char filepath; snprintffilepath, sizeoffilepathss path, commandname; Try to execute the file execvfilepath, args; If execv returns, it means the file could not be executed using this path, so try the next one path strtokNULL:; If we get here, the command could not be executed using any path return ; void imtheparentpidt childpid, int runinbackground int childreturnval, childerrorcode; fork returned a positive pid so we are the parent fprintfstderr Parent says 'child process has been forked with pidd childpid; if runinbackground fprintfstderr Parent says 'runinbackground so we're not waiting for the child'"; return; TODO P waitpidchildpid, childreturnval,; Use the WEXITSTATUS to extract the status code from the return value childerrorcode WEXITSTATUSchildreturnval; fprintfstderr Parent says 'wait returned so the child with pidd is finished. childpid; if childerrorcode Error: Child process failed. Most likely a failed exec fprintfstderr Parent says 'Child process d failed with code d childpid, childerrorcode; MAIN PROCEDURE SECTION int mainint argc, char argv pidt shellpid, pidfromfork; int nread, i execargc, parserstate, runinbackground; buffer: The Shell's input buffer. char bufferSHELLBUFFERSIZE; execargv: Arguments passed to exec call including NULL terminator. char execargvSHELLMAXARGS ; TODO new variables for P P P Entrypoint for the testrunner program if argc strcmpargvtest" return runsmptestsargc argv ; Allow the Shell prompt to display the pid of this process shellpid getpid; while if execargc counter; Print shell prompt fprintfstdout "Shellpiddd shellpid, counter; fflushstdout; Read a line of input. if fgetsbuffer SHELLBUFFERSIZE, stdin NULL return EXITSUCCESS; nread strlenbuffer; runinbackground nread buffernread ; buffernread runinbackground ; TODO P Parse the arguments: the first argument is the file or command we want to run. parserstate STATESPACE; for execargc i ; bufferiexecargc SHELLMAXARGS; i if isspacebufferi if parserstate STATESPACE execargvexecargc bufferi; parserstate STATENONSPACE; else bufferi; parserstate STATESPACE; runinbackground is if the input line's last character is an ampersand indicating background execution bufferi; Terminate input, overwriting the if it exists If no command was given empty line the Shell just prints the prompt again if execargc continue; Terminate the list of exec parameters with NULL execargvexecargc NULL; If Shell runs 'exit' it exits the program. if strcmpexecargv "exit" printfExiting process d shellpid; return EXITSUCCESS; End Shell program else if strcmpexecargvcd execargc Running cd changes the Shell's working directory. Alternative: try chdir inside a forked child: iffork if chdirexecargv Error: change directory failed fprintfstderrcd: failed to chdir s execargv; End alternative: exitEXITSUCCESS; else Execute Commands Try replacing 'fork with What happens? pidfromfork fork; if pidfromfork Error: fork failed. Unlikely, but possible eg OS kernel runs out of memory or process descriptors fprintfstderr "fork failed"; continue; if pidfromfork TODO P return imthechildexecargv execargv; Exit from main. else imtheparentpidfromfork, runinbackground; Parent will continue around the loop. end if end while loop return EXITSUCCESS; end main
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
