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

Task 1(100 points) There are many variations of the exec system call that can be

ID: 3905633 • Letter: T

Question

Task 1(100 points) There are many variations of the exec system call that can be used to execute an executable file (program) Calling exec does not create a new process, it replaces the current process with the new process. If the exec command fails, then it returns -1. But if it succeeds it does not return because execution proceeds at the beginning of the program that was executed. We will consider some useful forms of exec commands. 1. execl: takes the path name of an executable as its first argument, the rest of the arguments are the command line arguments ending with a NULL execl("/bin s"Is"-a", "-1*, NULL) 2. execv: takes the path name of a binary executable as its first argument, and a NULL terminated array of arguments as its second argument. The first element can be the command or " ". static char * args| = {"ls", "-a", "-I", NULL); execv(" bin/sargs); 3. execlp: same as execl except that we don't have to give the full path name of the command. exec lp ( " ls " , " ls " , "-a" "-I " , NULL) ; , 4. execvp: Same as exexv except that we don't have to give the full path name of the command. static char* args[ ] = {"ls", execvp("Is" args); "-a", "-I", NULL) ;

Explanation / Answer

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

#define BUFFER_LEN 1024

int main(){
    char line[BUFFER_LEN];
    char* argv[100];       
    char* path= "/bin/";   
    char ppath[20];     
    int argc;             

while(1){

    printf("Shell>> ");                   

       if(!fgets(line, BUFFER_LEN, stdin))
         break;
       size_t length = strlen(line);
       if (line[length - 1] == ' ')
          line[length - 1] = '';                               
       if(strcmp(line, "exit")==0){           
        break;
    }
char *token;                 
    token = strtok(line," ");
    int i=0;
    while(token!=NULL){
        argv[i]=token;     
        token = strtok(NULL," ");
        i++;
    }
    argv[i]=NULL;                    
    argc=i;                         
    for(i=0; i<argc; i++){
        printf("%s ", argv[i]);     
    }
    strcpy(ppath, path);          
    strcat(ppath, argv[0]);           
    for(i=0; i<strlen(ppath); i++){  
        if(ppath[i]==' '){     
            ppath[i]='';
        }
    }
    int pid= fork();     
    if(pid==0){              
        execvp(ppath,argv);
        fprintf(stderr, "Child process could not do execvp ");

    }else{                  
        wait(NULL);
        printf("Child exited ");
    }

}
}

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