You are on page 1of 3

The University of the West Indies, St.

Augustine
Department of Computing and Information Technology
COMP 2604 - Semester II, 2022/2023
Assignment 2 – 8%
Date Due: March 10th 2023 @ 5 pm

Part A – 15 marks

The purpose of this assignment is to test your understanding of basic process manipulation in
C.

1. You are required to write a C program called myshell.c that performs some of the functions
of a shell command line interpreter for the Linux operating system.
The program should prompt the user with:
[Enter command]>
Your program should execute whatever programs are specified by the user's command, and
once complete, it should again prompt the user. Continue prompting the user until he/she types
“exit".
When the user types in a line, your program should parse the command line to determine what
program to run, and also to determine the sequence of parameters that should be sent to the
program.

The program that a user wishes to run may not be in the current directory; thus, it may be
necessary to search for it elsewhere. You should search the directories specified in the PATH
environment variable for the program that the user wants to execute. Use the getenv()
function to determine the contents of the PATH environment variable, and the stat() function
to determine if a file exists.

If the file is found, then it should be executed with any parameters that the user may have
specified. Use an execv() system call to execute the file that contains the command.

In addition to executing simple commands with parameters, your program should also be able
to perform output redirection and piping. The user will specify output redirection by placing
the character > following the program (and parameters) to be executed and placing the name
of the file to which output will be redirected following the >.
The user will specify that a pipe should be created between two programs by inserting the
character | in between the two programs to be executed.

Here are some examples of commands that your program should be able to deal with.
 ls
 ls -l
 ls -l > file_list.txt
 ls -l | wc -l
 ls -l | wc -l > numfiles.txt
 ps -e ––forest
 ps -ef > process_info.txt
[10 marks]

Page 1 of 3
2. In a game called DuckHunting, players must shoot the ducks as they appear. A binary file
called "scores.dat" contains a sequence of 4 player's scores. Each record (a score) consists of
the C structure:
struct score{
int ducksKilled;
int ducksMissed;
int points;
int rank;
}
Write a C-program called duckhunt.c that calculates the total points gained and the rank of each
player. Your program should use two processes, A and B, where:
Process A calculates the number of points each player scored, and updates points in each data
structure. Two points are awarded for each duck killed and 1 point is taken away for each duck
missed.
Process B calculates the rank of each of the 4 players, and updates rank in each data structure
Use semaphores to ensure that the processes complete the task correctly, that is, both
synchronization of the processes and protection of the critical region are required. The use of
pipes are not permitted.
[5 marks]

Part B – 9 marks

1. In the text, we described a multithreaded Web server, showing why it is better than a
single-threaded server and a finite-state machine server. Are there any circumstances
in which a single-threaded server might be better? Give an example.
[3 marks]

2. In Fig. 2-12 the register set is listed as a per-thread rather than a per-process item.
Why? After all, the machine has only one set of registers.
[3 marks]

3. In the discussion on global variables in threads, we used a procedure create_global


to allocate storage for a pointer to the variable, rather than the variable itself. Is this
essential, or could the procedures work with the values themselves just as well?
[3 marks]

Page 2 of 3
Submission Instructions:
Part A: Zip your files myshell.c and duckhunt.c files. Name the zip folder
“ID#Number_A2PartA.zip”, e.g. “808000326_A2PartA.zip”. Please upload the zip folder to
the submission link on myElearning. Only 1 submission is allowed, so be careful that your file
submitted is the final version.

Part B: Submit one single text file containing the answers to all three questions. Name the text
file “ID#Number_A2PartB.docx”, e.g. “808000326_A2PartB.docx”. Please upload the text file
to the Turnitin submission link on myElearning. Only 1 submission is allowed, so be careful
that your file submitted is the final version.

Note: Include your Plagiarism declaration form in your submission.


Properly name all the files and submit in the proper link.

Page 3 of 3

You might also like