6.6k views
2 votes
Write a shell that contains enough code that it actually works so you can test it. You might also add some features such as redirection of input and

output, pipes, and background jobs.
C/C++
#define TRUE 1
while (TRUE) { /* repeat forever */
type prompt( ); /* display prompt on the screen */
read command(command, parameters); /* read input from terminal */
if (for k( ) != 0){ /* fork off child process */
/* Parent code. */
waitpid(−1, &status, 0); /* wait for child to exit */
} else {
/* Child code. */
execve(command, parameters, 0); /* execute command */
}
}

User AlexanderF
by
8.0k points

1 Answer

0 votes

Final answer:

This is a basic shell implementation in C/C++ programming language that can be enhanced with features such as input/output redirection, pipes, and background jobs.

Step-by-step explanation:

The code you provided is a basic shell implementation in C/C++ programming language. It utilizes a while loop to repeat the process of prompting the user for a command, reading the input, forking off a child process using the fork() function, and executing the command using the execve() function.One interesting feature that can be added to this shell is the ability to redirect input and output. For example, you can redirect the output of a command to a file using the > symbol. Another feature is the use of pipes (|) to connect the output of one command to the input of another command.Furthermore, the shell can support running background jobs by appending & to the end of a command. This allows the user to enter other commands without waiting for the background job to complete.The student has provided a snippet of a shell program written in C/C++ and is asking for a complete shell that includes features like input and output redirection, pipes, and background job handling. Such a shell continuously reads commands from the user, forks a child process to execute the command, and waits for the command to finish execution.

Example Shell Code with Additional FeaturesBelow is a basic example of a shell program with the additional features requested:#include #include #include #include #define TRUE 1int main() { char command[1024]; char *parameters[512]; int status; while (TRUE) { printf("> "); fgets(command, sizeof(command), stdin); if (command[0] == '\\') continue; if (fork() != 0) { waitpid(-1, &status, 0); } else { execvp(parameters[0], parameters); _exit(EXIT_FAILURE); } } return 0;}This example takes the provided code structure, uses execvp instead of execve for convenience in executing commands without providing a full environment, and reads the command using fgets. It omits advanced features like pipes and background jobs for simplicity.

User ZenoArrow
by
8.3k points