CSEShell

In this assignment, you are tasked to create a shell as well as a daemon process, both of which are the common applications of fork().

The assignment is written entirely in C. At the end of this assignment, you should be able to:

  • Create a shell and wait for user input
  • Write several other system programs that can be invoked by the shell
  • Parse user input and invoke fork() with the appropriate program
  • Create a program that results in a daemon process
  • Use your shell to keep track of the state of your daemon processes

You may complete this assignment in pairs. Indicate your partner’s name in the google sheet provided in our course handout.

Starter Code

You need to run this assignment in your POSIX compliant OS.

Download the starter code: git clone https://github.com/natalieagus/pa1.git

This will result in a directory called pa1.

Create a Github Remote Repo

Open your web browser in your host OS and create a PRIVATE repository called pa_1. Make sure you create the master branch and push to this master branch for this assignment (and not use main!). You also need to make master the default branch.

Then, cd to /pa1/ that you have downloaded, and add this project to your own private repo:

git remote remove origin
git remote add origin https://github.com/[your_github_username]/pa_1.git
git push -u origin master

If you only have main branch, the third command will fail. You need to create the master branch first and switch there:

git branch master
git checkout master
git push -u origin master

From now on, just stay at master. You’re free to create other branches but master is the branch which we will grade.

You will be required to make a commit after each Task in this PA1. This is part of our grading requirement. We want to see that you actually make good practices and perform periodic commits.

Add natalieagus-sutd as collaborator

In your pa1 repo, invite natalieagus-sutd as your collaborator. This is so that your repo can remain private and and we can pull your submissions for grading when it is due.

pa1 Files

You should have the following files:

pa1/
  |- bin/source/
      |- check_daemon.c
      |- count_line.c
      |- display.c
      |- find.c
      |- listdir_all.c
      |- listdir.c
      |- shell.c
      |- shell.h
      |- summond.c
      |- system_program.h
  |- files/
      |-combined.txt
      |- file1.txt
      |- file2.txt
      |- intermediate.txt
      |- lorem_ipsum.txt
      |- notes.pdf
      |- oneline.txt
      |- paragraph.txt
      |- ss.png
  |- .gitignore
  |- README.md
  |- makefile

You will only need to modify four files for this assignment: shell.c, countline.c, summond.c, and checkdaemon.c.

DO NOT modify any directories and file names.

Submission Rules

Type your answer in the spaces provided in each file, labeled as BEGIN ANSWER.

  1. DO NOT modify any makefile
  2. DO NOT create more scripts other than what’s given in the starter code
  3. DO NOT modify any interface (keep original functions as-is)
  4. DO NOT print anything to the console, other than the provided print statements. Any print statements you used for debugging must be deleted.

Your shell should NOT crash due to any input from the user or any ABSENCE of input from the user after the given command. In other words, if the original code crash in any way even without you modifying it, we meant for you to fix it 🙂.

Preparation

Go to /pa1/ and type make. Then, run the compiled shell ./cseshell. You should see all files compiled and the shell ran and immediately returned as follows:

Notice how you have a few binaries available under /bin. Those are your system programs. We will execute these system programs later on from our shell.

/bin/source

This directory contains the source file of your shell and also 7 system programs.

shell.h

Both files: /bin/source/shell.c and /bin/source/shell.h are the files containing the declaration and implementation for your shell program. The header file (shell.h) contains function declarations, imports, and macro definitions, while the .c file contains the implementation for these functions.

Open /bin/source/shell.h. The first few lines of #includes and #define are libraries and macro definitions.

Next, we have these array of pointers builtin_commands global constant that stores the strings of built-in commands that the user can type into the shell. These commands are implemented in the shell (instead of system programs).

/*
  List of builtin commands, followed by their corresponding functions.
 */
const char *builtin_commands[] = {
    "cd",    // calls shell_cd
    "help",  // calls shell_help
    "exit",  // calls shell_exit
    "usage", // calls shell_usage
};

Next, is the function declarations of the shell that you will implement in shell.c:

/*
The fundamental functions of the shell interface
*/
char *read_line_stdin(void);            // TASK 1
char **tokenize_line_stdin(char *line); // TASK 2
int process_command(char **args);       // TASK 3
void main_loop(void);                   // TASK 4

DO NOT modify ANY of these original functions declared in shell.h: not the arguments, not the names, nothing. Leave it as it is.

.c files

Other .c files inside /bin/source contains the implementation of your system programs. You will only need to modify three system programs: countline.c, summond.c, and checkdaemon.c. The others are already implemented for you.

Expected commands

The following commands are implemented within the shell, and are already done for you:

  cd
  help
  exit
  usage

This shell also supports: listdir, listdirall, summond, checkdaemon, find, and countline via the system programs in /pa1/bin/.

AGAIN, DO NOT modify any directories and file names.

Your Task

You are to implement:

  • Task 1-4: the fundamental functions inside shell.c (Task 1-4)
  • Task 5-7: three system program: countline (Task 5), summond (Task 6), and checkdaemon (Task 7).