Question

Write a simple shell that is similar to this: while(1 //repeat forever //display prompt on screen type_prompt )i read_command

please write down the code

Thank you.

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

A shell script is a computer program designed to be run by the Unix/Linux shell which could be one of the following:

  • The Bourne Shell
  • The C Shell
  • The Korn Shell
  • The GNU Bourne-Again Shell   

The following script uses the read command which takes the input from the keyboard and assigns it as the value of the variable PERSON and finally prints it on STDOUT.

#!/bin/sh
echo "What is your name?"
read PERSON
echo "Hello, $PERSON"

Sample run of Output:

$./test.sh
What is your name?
Zara Ali
Hello, Zara Ali
$

Fork() System Call:

Process creation is achieved through the fork() system call. The newly created process is called the child process and the process that initiated it (or the process when execution is started) is called the parent process. After the fork() system call, now we have two processes - parent and child processes

A child process uses the same pc(program counter), same CPU registers, same open files which use in the parent process.

It takes no parameters and returns an integer value. Below are different values returned by fork().

Negative Value: creation of a child process was unsuccessful.
Zero: Returned to the newly created child process.
Positive value: Returned to parent or caller. The value contains process ID of newly created child process.

Sample Program:

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

void forkexample()

{

    // child process because return value zero

    if (fork() == 0)

        printf("Hello from Child!\n");

  

    // parent process because return value non-zero.

    else

        printf("Hello from Parent!\n");

}

int main()

{

    forkexample();

    return 0;

}

Output:

1.
Hello from Child!
Hello from Parent!
     (or)
2.
Hello from Parent!
Hello from Child!

Explanation:

In the above code, a child process is created, fork() returns 0 in the child process and positive integer to the parent process.
Here, two outputs are possible because the parent process and child process are running concurrently. So we don’t know if OS first give control to which process a parent process or a child process.

Important: Parent process and child process are running the same program, but it does not mean they are identical. OS allocate different data and state for these two processes and also control the flow of these processes can be different. See next example

Add a comment
Know the answer?
Add Answer to:
please write down the code Thank you. Write a simple shell that is similar to this: while(1 //repeat forever //display prompt on screen type_prompt )i read_command ( command, parameters); // read inp...
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
  • Write a SHELL SCRIPT: Write a simple shell that is similar to what we have discussed...

    Write a SHELL SCRIPT: Write a simple shell that is similar to what we have discussed in class. while (1) {//repeat forever type_prompt();//display prompt on screen read_command (command, parameters);//read input from terminal if (fork() != theta)//parent wait (NULL);//wait for child else{execve (command, parameters, theta);//execute command}} but contains enough code that it actually works so you can test it. For simplicity, you may assume that all commands are in the directory/bin.

  • 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...

  • 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...

  • 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");...

  • I have the following code....from the previous lab....the above needs to be added to what is...

    I have the following code....from the previous lab....the above needs to be added to what is already existing. ALSO MODIFY SEMAPHORES TO USE pthreads instead of the pipe constructs P() & V() #include <stdio.h> #include <string.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> #include <stdlib.h> #include <sys/stat.h> void printStat(char *filename); //Main int main(int argc, char *argv[]) { //Process Id (storing)    pid_t pid;    int j;    //printf("Welcome to Project Three\n”);    // For loop*/    for (j = 1; j...

  • This is for a Unix class. Please help me out. I am attaching a skeletal code of the program below, it just needs ti be filled in. Below is a skeletal code of the program. Fork a child process...

    This is for a Unix class. Please help me out. I am attaching a skeletal code of the program below, it just needs ti be filled in. Below is a skeletal code of the program. Fork a child process and then use the parent for reading and the child for writing.  This is just a way of sending and receiving messages asynchronously. /* ************************************************************* * Utility functions * ************************************************************** */ static void usageError(const char * progName, const char *msg) {...

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