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....
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...
I am writing a shell program in C++, to run this program I would run it...
I am writing a shell program in C++, to run this program I would run it in terminal like the following: ./a.out "command1" "command2" using the execv function how to execute command 1 and 2 if they are stored in argv[1] and argv[2] of the main function?
Research 10 current flavors of Unix and Linux available today. Review who uses those flavors and...
Research 10 current flavors of Unix and Linux available today. Review who uses those flavors and for what purpose. Consider reviewing both enterprise, Windows-like, and home flavors. Complete the following table. Keep your product to two pages. ALL SOURCES MUST BE CITED PER THE RUBRIC OR POINTS WILL BE DEDUCTED. This extends to any information that YOU did not compose.
This program is a simple version of the linux shell using execvp by forking using C...
This program is a simple version of the linux shell using execvp by forking using C Currently this program of the shell assumes user enters no spaces before and no extra spaces between strings. Using exec, an executable binary file (eg: a.out) can be converted into a process. An example of using exec is implementing a shell program or a command interpreter. A shell program takes user commands and executes them. int execvp(const char *file, char *const argv[]); Same as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT