Assignment 6: Trust the process
Due Friday, November 21st, before midnight
This assignment has a check-in requirement with the TAs or instructor during office hours on Nov 13th or 14th
The goals for this assignment are:
-
Gain experience working with
fork(). -
Use OS system calls in your C program.
-
Implement multi-process programs
1. Personal Shell
In the file, shell.c, implement a program that implements a simple shell.
Requirements:
-
Your program should print a prompt. At minimum, your prompt should show the current working directory and look distinct from the lab machine prompts. In other words, it should be easy to tell what shell if running from the terminal.
-
Use the
readline()function to get user input. This function supports backspace, tab completion, and more. Documentation for readline can be found here. -
Use the
add_history()function to save user history. This will allow arrow keys to work. Documentation for readline can be found here. -
Your program should quit if the users types
exit. -
When the user gives a command, split it into a command and arguments and then fork a command to execute it.
-
Your terminal should wait for the command to finish. If the command terminates with an error, report the error.
readline is a package that is installed on goldengate and in our labs. However, you may need to install it on your own machine. On Unix systems, this can be done by running sudo apt install libreadline-dev
|
You can print with colors to the console using ANSI escape sequences (see below).
For example, printf(ANSI_COLOR_GREEN "Green Text" ANSI_COLOR_RESET); will print green text. Here is a good guide!
|
// Helpful macros for working with color #define ANSI_COLOR_RED "\x1b[31m" #define ANSI_COLOR_GREEN "\x1b[32m" #define ANSI_COLOR_YELLOW "\x1b[33m" #define ANSI_COLOR_BLUE "\x1b[34m" #define ANSI_COLOR_MAGENTA "\x1b[35m" #define ANSI_COLOR_CYAN "\x1b[36m" #define ANSI_COLOR_RESET "\x1b[0m"
Most prompts print helpful information. Above, we use the
system commands gethostname, getcwd, and getpwuid(geteuid()) to get the
machine name, the current working directory, and current user respectively.
|
2. Grep
In the file, grep.c, implement a program that uses N processes to search for a keyword
in a set of files.
Your program implements a simplified version of the bash command grep. Grep searches a list of files
for a given keyword or expression. For example, the following command searches for the keyword public
in a list of source files. We use the find command to generate the list of source files to search.
For example:
Make sure you include the backticks, e.g. ` . This adds the filenames found by find as command line arguments to grep.
|
$ make grep
gcc -g -Wall -Wvla -Werror grep.c -o grep
$ ./grep
usage: ./grep <NumProcesses> <Keyword> <Files>
$ ./grep 4 class ` find code -type f `
Searching 12 files for keyword: class
Process [20208] searching 3 files (3 to 6)
20208) code/Seal.h:class Seal : public Animal
20208) code/Walk.h:class Walk : public Locomotion
20208) code/Animal.h:class Animal
Process [20208] found 3 lines containing keyword: class
Process [20209] searching 3 files (6 to 9)
20209) code/Duck.h:class Duck : public Bird
20209) code/Locomotion.h:class Locomotion
Process [20209] found 2 lines containing keyword: class
Process [20210] searching 3 files (9 to 12)
20210) code/Fly.h:class Fly : public Locomotion
20210) code/Bird.h:class Bird : public Animal
20210) code/Swim.h:class Swim : public Locomotion
Process [20210] found 3 lines containing keyword: class
Process [20208] found 3 lines containing of keyword: class
Process [20209] found 2 lines containing of keyword: class
Process [20210] found 3 lines containing of keyword: class
Process [20211] searching 3 files (12 to 15)
20211) code/Fish.h:class Fish : public Animal
20211) code/Whale.h:class Whale : public Fish
Process [20211] found 2 lines containing keyword: class
Process [20211] found 2 lines containing of keyword: class
Total occurances: 10
Elapsed time is 0.001403
Requirements:
-
Your program should take the number of processes, keyword, and a list of files as command line arguments
-
Subdivide the files among the N processes. Note that the number of files may not divide evenly between processes.
-
Print the totals and sub-division of files similarly to the output above
-
Use the
fgetsfunction to process the lines in each file -
Use the function
strstrdefined instring.hto search lines of text for the keyword. Documentation is here -
To compute the total count, each child process can return the number matching lines using its exitcode. Recall that the parent can use the status reported by
waitpidto get the children’s exit code. -
Your program should report the amount of time needed to search using
gettimeofday
You can print with colors to the console using ANSI escape sequences (see below).
For example, printf(ANSI_COLOR_GREEN "Green Text" ANSI_COLOR_RESET); will print green text. Here is a good guide!
|
// Helpful macros for working with color #define ANSI_COLOR_RED "\x1b[31m" #define ANSI_COLOR_GREEN "\x1b[32m" #define ANSI_COLOR_YELLOW "\x1b[33m" #define ANSI_COLOR_BLUE "\x1b[34m" #define ANSI_COLOR_MAGENTA "\x1b[35m" #define ANSI_COLOR_CYAN "\x1b[36m" #define ANSI_COLOR_RESET "\x1b[0m"