Menu

Implement Bash Shell C Following Functionalities 1 Shell Must Know Launch Programs Foregro Q43889827

Implement a bash shell in C having the followingfunctionalities:

1.The shell must know how to launch programs in the foregroundand background. If command is followed by & it must be executedin background.

2.It should recognize the internal commands: exit, ls, pwd andcd.

[Hint: exit should use the exit()system call to terminate the shell. cd uses the chdir() system callto change to a new directory. You can use the standard C libraryfunctions chdir, getcwd, mkdir, rmdir, readdir, stat etc. toimplement the calls. Note that these are built-in commands for yourshell and hence no new process will be created to execute them.They neither read from the standard input stream nor write to thestandard output stream. So your program should ignore fileredirection with those built-in commands. Your program should stillrecognize invalid commands, however.]

  • If the command line does not indicate any internal commands, itshould be in the following form:
         <program name> <arg1><arg2> …. <argN>
    Your shell should invoke the program, passing it the list ofarguments in the command line. The shell must wait until thestarted program completes.

To allow users to pass arguments you need to parse the inputline into words separated by whitespace (spaces and ‘t’ tabcharacters). You might try to use strtok_r() for parsing (check themanual page of strtok_r() and Google it for examples of using it).In case you wonder, strtok_r() is a user-level utility, not asystem call. This means this function is fulfilled without the helpof the operating system kernel.

The shell runs programs using two core system calls: fork() andexecvp(). Read the manual pages to see how to use them. In short,fork() creates an exact copy of the currently running process, andis used by the shell to spawn a new process. The execvp() call isused to overlay the currently running program with a new program,which is how the shell turns a forked process into the program itwants to run. In addition, the shell must wait until the previouslystarted program. This is done with the wait() system call or one ofits variants (such as waitpid()). All these system calls can faildue to unforeseen reasons (see their manual pages for details). Youshould check their return status and report errors if theyoccur.

No input the user gives should cause the shell to exit (exceptwhen the user types exit or Ctrl+D). This means your shell shouldhandle errors gracefully, no matter where they occur. Even if anerror occurs in the middle of a long pipeline, it should bereported accurately and your shell should recover gracefully. Youmay need to use signal() and kill() system calls. In addition, yourshell should not generate leaking open file descriptors.

Hint: you can monitor the current open filedescriptors of the shell process through the /proc file system.

Your shell must run in one of two modes: interactive or batch.If no command-line argument is provided, your shell shouldinteractively accept inputs from the user and execute them. If abatch file of commands is provided as command-line input to yourprogram, then your shell must execute all commands in the batchfile one after the other

  

Expert Answer


Answer to Implement a bash shell in C having the following functionalities: 1.The shell must know how to launch programs in the fo…

OR