This homework is the second phase in developing a basic shel

This homework is the second phase in developing a basic shell (Command Line Interpreter). In the previous homework you coded the first phase, parsing commands. In the second phase, you are required to code the execution of the parsed commands. You are given a skeleton code, including a working code of phase 1, and required to build on it to achieve this task. The user entry is still a number of commands with their arguments, if any, separated by pipes, if any. The output should be the result of the executed commands. Functionality In the skeleton of the given code, you need to code the function to execute the commands execute_commands and replace the call to the print_commands function with the call to this function. The function prototype is void execute_commands(struct command mycommands[], int no_commands); This function Creates pipes if needed Determines the input and output file descriptors of a command based on the command\'s order Calls a function execute_command to execute a command at a time. This function has the prototype statement int execute_command(char *command, int argc, char *args[], int fd_in, int fd_out); The execute command function forks a child which does the following Decides if dup2 is needed for any of the standard input or output based on the values of f d_in and fd_out Uses execute to execute the command When the function execute command is called for every command in the array mycommands, it starts waiting for all of them to finish.

Solution

void execute_commands(struct command mycommands[], int no_commands)
{
   for(int i=0; i<no_commands; i++)
   {
       //dont know what command struct holds.. it must be containing the values of the remaining
       execute_command(mycommands[i], i, mycommands[i]->args, mycommands[i]->fd_in, mycommands[i]->fd_out);
   }
}

void execute_command(char *command, int argc, char *args[], int fd_in, int fd_out )
{
pid_t pid;
int status;

if (pid == 0) {
if (execvp(command, args) < 0) { //executing the command
exit(1);
}
}
else {
while (wait(&status) != pid); // wait for other process to complete
}
}

 This homework is the second phase in developing a basic shell (Command Line Interpreter). In the previous homework you coded the first phase, parsing commands.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site