Write a small shell (a “C” program) that accepts inputs from the user and processes these inputs to run different commands.

The fork system call is defined as follows :

pid_t fork(void);

  • Where pid_t return value differs for each process (child and parent)
  • If fork() is successful:
    • If the process is the newly created child: returns 0 (zero)
    • If the process is the parent: returns the child’s PID
  • If fork() is unsuccessful:
    • -1
    • No child process is created.

Use the return value to determine if the current process running is a parent or a child process. Another useful system call is the exec family of system calls. These are used to execute a file and load the executable file information and replace the currently running process information (Code, Data, Heap, and Stack sections). Notice that the system call loads the program in the currently running process address space and maintain the process PID, since the exec() system call does not create a new process but changes the currently running process memory.

In this lab, we will focus on one of exec system calls,

#include <unistd.h>

int execve(const char *filename, char *const argv[], char *const envp[]);

The function takes the following arguments:

  • filename: The name of the program to load. This must be either an executable or a script file that must start with the #!/bin/bash”.
  • argv: is an array of argument strings passed to the new program.
  • envp: is an array of strings, conventionally of the form key=value, which are the environment to the new program.

For this lab, you can set argv and envp to NULL. The exec() functions return only if an error has occurred. The return value is -1, and errno is set to indicate the error.

[IMAGE1]

For example, this program executes a fork system call and loads a new program “Prog” into the Child process. The “Prog” program exists in the same directory as the program since execv will look at the local file locations. If you want to point to other locations, you will have to use the full path using “/”. For example. If you want to run a program in a directory named “test”:

[IMAGE2]

For more information about the exec system call, visit: http://man7.org/linux/man-pages/man3/exec.3.html

Fork with execve system calls

To compile C program, we will start using a famous compiler named GCC, which stands for (GNU Compiler Collection). GCC is used to compile Linux kernel and a lot of mainstream applications since it is known for its stability. The following is the command line you use to compile a C program using GCC :

$ gcc c_program.c -o output

  • gcc : is the command to start the GCC compiler.
  • c_program : your “C” program to compile.
  • -o : an option to the GCC command to specify the name of the compiled program. In this case, the program will be named “output”. You can change this name according to your preference, although it is recommended to use descriptive names for your executables.

To run this program, you can simply use the “./”, the “.” means in this directory. For example, to run the output program:

$ ./output

LAB INSTRUCTIONS:

Write a small shell that accepts inputs from the user and processes these inputs to run different commands. These commands are small programs that you have in your local directory that you have to write as well. The programs are:

  • (FactNum.c)
    • A “C” program that calculates the factorial of a number, the numbers are obtained from the user.
    • The program is compiled and saved as “FN”.
  • (Strlen.c)
    • A “C” program that returns the number of characters in a user inputted string.
    • The program compiled and saved as “SL”.

Your Simple shell will accept input from the user which indicates the name of the program to run. your shell uses fork() to run the inputted command as a newly created process. The shell will use the execve() system call to load the commands’ code, data, heap, and stack data to replace the child shell data. Afterward, your shell will wait till the command is finished before it can prompt the user to enter the next command to run.

Functionality:

  • If the commands exist, the shell waits for the command to complete before prompting the user for a new command.
  • If the user inputs “End,” the shell exits.
  • If the command does not exist or causes an error, the shell should print an error message

SAMPLE ASSIGNMENT

Sample-2

Powered by WordPress