Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a special simple command interpreter that takes command(s) and its argumen

ID: 3768373 • Letter: W

Question

Write a special simple command interpreter that takes command(s) and its arguments. This special command interpreter will only have three different cases that need to be addressed in your interpreter program.
1. If it’s a single command, the main process created a child process to execute the command using exec() family functions. After executing the command, it asks for new command input (parent wait for child).
2. If it is a single command and it is to be executed in background,(i.e. the command line has a & symbol at the end) the interpreter will not wait for its child.
3. If the command is a multiple background command (i.e. if the background command are separated by the symbol ;) then the interpreter creates a number of child process equal to the number of commands and executes one after the other in the same order as appear in the command line. Each child process will execute one command using exec family of function. (Parent will not wait for any child, make sure the first command child process finishes first and then the second will starts).

Explanation / Answer

Code using Command line Interpreter in C:

#include <stdio.h>
#include <string.h>
#include <unistd.h>

void parse(char *line, char **argv)
{
while (*line != '') {     
      while (*line == ' ' || *line == ' ' || *line == ' ' || *line=='|')
           *line++ = '';   
      *argv++ = line;       
      while (*line != '' && *line != ' ' &&
             *line != ' ' && *line != ' ')
           line++;          
}
*argv = '';             
}


int main(int argc,char *argv[])
{
int child_pid;
char * arg[10];     
   int child_status;
char str[10];
do
{
printf(">> ");
gets(str);
parse(str,arg);
if(!strcmp(arg[0],"end"))
{
    break;
   }
child_pid = fork();
if(child_pid == 0)
{
    execvp(*arg,arg);
    printf("Unknown command ");


   }
   else
{
    wait(&child_status);
    printf("koniec ");
}
}
while(1==1);
    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote