Question

In: Computer Science

UNIX/LINUX LAB (1) Review the sample shell program (tryShell.c), and compile/run the program (tryShell) with the...

UNIX/LINUX LAB

(1) Review the sample shell program (tryShell.c), and compile/run the program (tryShell) with the following commands,

and at the end, use CTRL+C to terminate the program:

ls

ls -l tryShell*

date

whoami

hostname

uname -a

ctrl+C   

(2) Run the program (tryShell) with "time -p" with a few commands:

time -p ./tryShell

(3) Edit the program (tryShell.c) so that it will exit (terminate the program) when the input command string is "exit"

try shell.c code at bottom

////////////

#include 
#include 
#include 
#include 
#include 

#define MAX 1024

void  parse(char *line, char **argv)
{
     while (*line != '\0') {    /* if not the end of line */ 
          while (*line == ' ' || *line == '\t' || *line == '\n')
               *line++ = '\0';  /* replace white spaces with 0*/
          *argv++ = line;       /* save the argument position */
          while (*line != '\0' && *line != ' ' && 
                 *line != '\t' && *line != '\n') 
               line++;      /* skip the argument until ...    */
     }
     *argv = '\0';          /* mark the end of argument list  */
}

void  execute(char **argv)
{
     pid_t  pid;
     int    status;

     if ((pid = fork()) < 0) {    /* fork a child process    */
          printf("*** ERROR: forking child process failed\n");
          exit(1);
     }
     else if (pid == 0) {          /* for the child process: */
          if (execvp(*argv, argv) < 0) {     /* exec command  */
               printf("*** ERROR: exec failed\n");
               exit(1);
          }
     }
     else {                            /* for the parent: */
          while (wait(&status) != pid) /* wait for completion */
               ;
     }
}


void  main(void)
{
     char  line[MAX];      /* the input line                 */
     char  *argv[64];       /* the command line argument      */

     while (1) {            /* repeat until done ....         */
          printf("Shell -> ");   /* display a prompt          */
            if (fgets(line, MAX, stdin) != 0){
               line[strcspn(line, "\n")] = '\0';
                  printf("\n");

        // Place your code here to check the command is "exit"
     //   to terminate the shell. 


                  parse(line, argv);     /* parse the line      */
                  execute(argv);        /* otherwise, execute cmd  */
         }  
     }
}

///////////

Solutions

Expert Solution

1)

./tryShell
Shell -> ls

hello hello1 hello.c shell.o tryShell tryShell.c tryShell.o
Shell -> ls -l tryShell*

ls: tryShell*: No such file or directory

Shell -> ls -l tryShell

-rwxr-xr-x+ 1 saes0817 ice 8470 Oct 10 10:44 tryShell
Shell -> date

Sat Oct 10 10:51:39 EDT 2020
Shell -> whoami

saes0817
Shell -> hostname

devapp714cn
Shell -> uname -a

Linux devapp714cn 2.6.18-308.el5 #1 SMP Fri Jan 27 17:17:51 EST 2012 x86_64 x86_64 x86_64 GNU/Linux
Shell ->

2)

time -p ./tryShell
Shell -> ls

hello hello1 hello.c shell.o tryShell tryShell.c tryShell.o
Shell -> date

Sat Oct 10 10:55:11 EDT 2020
Shell -> hostname

devapp714cn
Shell ->
real 14.35
user 0.00
sys 0.01

3) tryShell.c file with exit code

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#define MAX 1024

void  parse(char *line, char **argv)
{
     while (*line != '\0') {    /* if not the end of line */ 
          while (*line == ' ' || *line == '\t' || *line == '\n')
               *line++ = '\0';  /* replace white spaces with 0*/
          *argv++ = line;       /* save the argument position */
          while (*line != '\0' && *line != ' ' && 
                 *line != '\t' && *line != '\n') 
               line++;      /* skip the argument until ...    */
     }
     *argv = '\0';          /* mark the end of argument list  */
}

void  execute(char **argv)
{
     pid_t  pid;
     int    status;

     if ((pid = fork()) < 0) {    /* fork a child process    */
          printf("*** ERROR: forking child process failed\n");
          exit(1);
     }
     else if (pid == 0) {          /* for the child process: */
          if (execvp(*argv, argv) < 0) {     /* exec command  */
               printf("*** ERROR: exec failed\n");
               exit(1);
          }
     }
     else {                            /* for the parent: */
          while (wait(&status) != pid) /* wait for completion */
               ;
     }
}


void  main(void)
{
     char  line[MAX];      /* the input line                 */
     char  *argv[64];       /* the command line argument      */

     while (1) {            /* repeat until done ....         */
          printf("Shell -> ");   /* display a prompt          */
            if (fgets(line, MAX, stdin) != 0){
               line[strcspn(line, "\n")] = '\0';
                  printf("\n");

        // Place your code here to check the command is "exit"
     //   to terminate the shell. 
                   if (0 == strcmp(line, "exit")){
                    exit(1);
               }
                  parse(line, argv);     /* parse the line      */
                  execute(argv);        /* otherwise, execute cmd  */
         }  
     }
}

///////////

OUTPUT:

./tryShell
Shell -> ls

hello hello1 hello.c shell.o tryShell tryShell.c tryShell.o
Shell -> exit

bash-3.2$
bash-3.2$
bash-3.2$


Related Solutions

UNIX/LINUX LAB   (1) Compile hello.c program: gcc hello.c -o hello (2) Compile hello.c program with -static...
UNIX/LINUX LAB   (1) Compile hello.c program: gcc hello.c -o hello (2) Compile hello.c program with -static option: gcc -static hello.c -o hello1 (3) Use size command to see the memory layout of these two programs (hello and hello1). (4) Write a brief summary of what you have noted here. Submit the session history (log) of this activity and a brief summary. hello.c program bottom //////////// #include <stdio.h> #include <stdlib.h> int main() { printf("Hello World\n"); // waw hello Richard and ok...
Project 1—UNIX Shell This project consists of designing a C program to serve as a shell...
Project 1—UNIX Shell This project consists of designing a C program to serve as a shell interface that accepts user commands and then executes each command in a separate process. Your implementation will support input and output redirection, as well as pipes as a form of IPC between a pair of commands. Completing this project will involve using the UNIX fork(), exec(), wait(), dup2(), and pipe() system calls and can be completed on any Linux, UNIX, or macOS system. I....
UNIX 1. Compile and run shell0.c file 2. Design and implement shell1.c program to handle "exit"...
UNIX 1. Compile and run shell0.c file 2. Design and implement shell1.c program to handle "exit" command to terminate the program. That is, if the user input-string is "exit", then the program terminates. 3.Design and Implement shell2.c to implement a signal handler to handle ctrl+C (SIGINT) as listed below             void sig_int(int signo) { printf("\nCaught SIGINT!\n"); } Here is shell0.c file shell0.c /* * shell0.c * running in loop to read input string (command) to be processed * To exit,...
When programming in the Unix/Linux shell, short-circuit evaluation of an expression is a standard way to...
When programming in the Unix/Linux shell, short-circuit evaluation of an expression is a standard way to execute a second command ONLY IF the first command was successful. How does this work? What is a ternary operator? When should it be used and when should it be avoided? We perform a hand trace of a C program to validate what?
I am a student taking an introductory Unix / LInux shell programming course and need some...
I am a student taking an introductory Unix / LInux shell programming course and need some help with an assignment. Using the file CISED as input, write the sed command to do the following: Change 2 or more occurrences of g to a single g Add > to beginning of any line that begins with CIS132 Add < to the end of any line that ends in Tux or tux Replace any five to eight digit number with XX For...
Design two shell programs working on Linux (Ubuntu) Design a shell script program, 1) reading given...
Design two shell programs working on Linux (Ubuntu) Design a shell script program, 1) reading given only two integer numbers from command line arguments and computing their multiplication. If two integer numbers are not given, print “Wrong Input” on your screen. Note that, the number of arguments is known when the script runs. Take a screenshot showing your shell program and its execution step. Design a shell program to remove all the shell programming files ending with sh on your...
System Administration Linux/Unix This lab is for you to research and implement old school DNS for...
System Administration Linux/Unix This lab is for you to research and implement old school DNS for your virtualized environment Currently, if you tried to ping your server01 by IP address (192.168.10.11), you would receive a response. If you tried to ping using its hostname "server01", it would result in "ping: hostname: Temporary failure in name resolution" 1) Research how and where to implement 2) Create the following entries: 192.168.10.1 router 192.168.10.11 SRV1 192.168.10.12 SRV2 192.168.10.13 client 3) Ensure these hostnames...
Programming Projects Project 1—UNIX Shell This project consists of designing a C program to serve as...
Programming Projects Project 1—UNIX Shell This project consists of designing a C program to serve as a shell interface that accepts user commands and then executes each command in a separate process. Your implementation will support input and output redirection, as well as pipes as a form of IPC between a pair of commands. Completing this project will involve using the UNIX fork(), exec(), wait(), dup2(), and pipe() system calls and can be completed on any Linux, UNIX, or macOS...
please write in c using linux or unix Write a program that will simulate non -...
please write in c using linux or unix Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally 10 jobs) and...
Please write in C using linux or unix. Write a program that will simulate non -...
Please write in C using linux or unix. Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally 10 jobs) and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT