Question

DO NOT COPY PASTE THE SOLUTION FROM ANOTHER QUESTION! IT IS NOT THE RIGHT ONE, ANSWER...

DO NOT COPY PASTE THE SOLUTION FROM ANOTHER QUESTION! IT IS NOT THE RIGHT ONE, ANSWER WILL BE REPORTED IF NOT A UNIQUE SOLUTION.

Write a C program called myshell.c, which, when compiled and run, will do what the shell does, namely, it executes in a loop (until user types exit on the keyboard),

prints a prompt on the screen, reads the command typed on the keyboard (terminated by \n),

creates a new process and lets the child execute the user’s command,

waits for the child to terminate and then goes back to beginning of the loop.

If the command typed is exit, then your program should terminate.

Print the total number of commands executed just before terminating your program.

Assume that each line represents one command only, no command will end with & (all commands will be attached commands, no background execution),

user will not type ^c or ^z, all commands are simple commands, etc

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

#include<string.h>

#include<stdlib.h>

#include<sys/wait.h>

#include<unistd.h>

int main()

{

char buff[100];

char *p;

char *args[10];

int count=0,pid,i;

int no_commands=0;

while(1)

{

printf("myShell>");

count=0;

fgets(buff,sizeof(buff),stdin);

//eliminate /n in buff

buff[strlen(buff)-1]='';

//printf("%s ",buff);

if(strcmp(buff,"exit") == 0)

{

printf("Number of commands executed: %d ",no_commands);

printf("Bye... ");

exit(0);

}

//alocate memory for 10 array of strings

for(i = 0; i < 10; i++)

{

args[i] = (char*)malloc(20*sizeof(char));

}

//now make a argument list and add to string array

p=strtok(buff," ");

strcpy(args[count++],p);

while(p!=NULL)

{

p = strtok(NULL," ");

if(p == NULL)

break;

strcpy(args[count++],p);

}

//after building list of commands , execute command using execvp , for that create child process

args[count]=NULL;

pid = fork();

if(strcmp(buff,"exit") == 0)

{

printf("Number of commands executed: %d ",no_commands);

exit(0);

}

if(pid == 0) //child process

{

//execvp(args[0],args);

execvp(args[0],args);

perror("ps error: ");

printf("After execvp failed ");

exit(0);

}

else //parent process

{

//wait for chidl to finish

wait(0);

//printf("Finished executing user command %s ",buff);

++no_commands;

if(strcmp(buff,"exit") == 0)

{

printf("Number of commands executed: %d ",no_commands);

exit(0);

}

buff[0]='';

count=0;

*args=NULL;

}

fflush(stdin);

}

return 0;

}

================================================

//output1

myShell>exit
Number of commands executed: 0
Bye...

//output2

myShell>ls -l
total 16
-rwxr-xr-x 1 runner runner 8832 Feb 19 05:28 main
-rw-r--r-- 1 runner runner 1986 Feb 19 05:28 main.c
myShell>date
Tue Feb 19 05:28:46 UTC 2019
myShell>wc -l main.c
81 main.c
myShell>ps -eaf
UID PID PPID C STIME TTY TIME CMD
runner 1 0 0 05:20 ? 00:00:00 /dev/init -- /bin/init
runner 6 1 0 05:20 ? 00:00:00 /bin/init
runner 14 6 0 05:20 ? 00:00:02 cquery --init={"progressReportFr
runner 355 6 0 05:28 pts/0 00:00:00 ./main
runner 359 355 0 05:28 pts/0 00:00:00 ps -eaf
myShell>exit
Number of commands executed: 4
Bye...

============================

//screenshot


Add a comment
Know the answer?
Add Answer to:
DO NOT COPY PASTE THE SOLUTION FROM ANOTHER QUESTION! IT IS NOT THE RIGHT ONE, ANSWER...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Project 1: Implementing a Shell 1 Overview In this individual project you will have to design...

    Project 1: Implementing a Shell 1 Overview In this individual project you will have to design and implement a simple shell command interpreter called mysh. The basic function of a shell is to accept lines of text as input and execute programs in response. The shell must be able to execute built-in commands in a process different from the one executing mysh. 2 Requirements When first started, your shell should initialize any necessary data structures and then enter a loop...

  • Creating a Shell Interface Using Java This project consists of modifying a Java program so that...

    Creating a Shell Interface Using Java This project consists of modifying a Java program so that it serves as a shell interface that accepts user commands and then executes each command in a separate process external to the Java virtual machine. Overview A shell interface provides the user with a prompt, after which the user enters the next command. The example below illustrates the prompt jsh> and the user’s next command: cat Prog.java. This command displays the file Prog.java on...

  • The original code using the gets() function is written below. You need to do (a) change...

    The original code using the gets() function is written below. You need to do (a) change the provided code so that you now use fgets() function to obtain input from the user instead of gets(), (b) make any other necessary changes in the code because of using fgets() function, and (c) fill in the code for the execute() function so that the whole program works as expected (a simple shell program). Note: part c is already done, and the execute...

  • Unix questions, i need help on Hint: Commands to study to answer this question: predefined shell...

    Unix questions, i need help on Hint: Commands to study to answer this question: predefined shell variables, and .profile script file, echo SHELL, HOME, PATH, MAIL and TERM are predefined shell variables. You can use the value of a shell variable in a shell command putting $ in front of it. For example, to display the value of the HOME directory of the user, specify $HOME in the echo command like echo $HOME. Do not give just the value of...

  • Edit the code (shell.c) given to do the tasks asked! will rate for correct answer! Also,...

    Edit the code (shell.c) given to do the tasks asked! will rate for correct answer! Also, include a screen shot of the output and terminal window of each command you used. Read carefully to do this task please. shell.c code given below. #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/wait.h> #include <string.h> int main() { int PID; char lineGot[256]; char *cmd; while (1){ printf("cmd: "); fgets(lineGot, 256, stdin); // Get a string from user (includes \n) cmd = strtok(lineGot, "\n");...

  • Update the program in the bottom using C++ to fit the requirements specified in the assignment....

    Update the program in the bottom using C++ to fit the requirements specified in the assignment. Description For this assignment, you will be writing a single program that enters a loop in which each iteration prompts the user for two, single-line inputs. If the text of either one of the inputs is “quit”, the program should immediately exit. If “quit” is not found, each of these lines of input will be treated as a command line to be executed. These...

  • Please do not copy other solution from the site as they never fully implement all the...

    Please do not copy other solution from the site as they never fully implement all the listed guidelines below. Please write a shell script called atm.bash similar to the ones used in ATM machines. Essentially your script is to handle a person's savings and checking accounts and should handle the following services:             Transfer from savings account to checking account             Transfer from checking account to savings account             Cash withdrawal from either account             Balance statements for both the...

  • 2. capture This solution to this problem will require using some system calls that will be...

    2. capture This solution to this problem will require using some system calls that will be discussed in week 3. This program should accept the name of a file that includes a list of commands (one per line) to execute. Each line of the file should be executed by a subprocess and the output written to the file capture.txt For example, given the command file: /bin/ls-1 /bin/cat apple banana usr/bin/wc -1-wcanteloupe The output saved to capture.txt might be: W1 schubert...

  • CSC 130 Lab Assignment 8 – Program Menu Create a C source code file named lab8.c...

    CSC 130 Lab Assignment 8 – Program Menu Create a C source code file named lab8.c that implements the following features. Implement this program in stages using stepwise refinement to ensure that it will compile and run as you go. This makes it much easier to debug and understand. This program presents the user a menu of operations that it can perform. The choices are listed and a prompt waits for the user to select a choice by entering a...

  • Using PuTTY Linux Server Task Compiling:             1) Download the two files from blackboard, driver.cpp, and...

    Using PuTTY Linux Server Task Compiling:             1) Download the two files from blackboard, driver.cpp, and circle.h             2) Create a new directory to store the files in             3) Compile the code a) Run the command g++ driver.cpp -o executable_name, this will compile the code both for driver.cpp and the referenced file circle.h note: -o parameter specifies a new name for the executable, if you do not specify the “-o” parameter the default name of the executable is “a.out”...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT