0% found this document useful (0 votes)
13 views3 pages

C Programming Assignment: Process Management

This document provides instructions for Assignment 2, which has two parts. Part A involves writing a C program called myshell.c that functions like a basic command line shell, allowing the user to run programs and redirect output. It also involves writing a program called duckhunt.c that uses two processes and semaphores to calculate scores and ranks from a binary scores file for a duck hunting game. Part B involves answering three questions about threads and processes. Students are instructed to submit Part A as a zip folder containing the two C programs, and Part B as a single text file containing the answers. Files must be named according to the provided format and submitted to the correct links on myElearning by the due date.

Uploaded by

Daniel McDougall
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

C Programming Assignment: Process Management

This document provides instructions for Assignment 2, which has two parts. Part A involves writing a C program called myshell.c that functions like a basic command line shell, allowing the user to run programs and redirect output. It also involves writing a program called duckhunt.c that uses two processes and semaphores to calculate scores and ranks from a binary scores file for a duck hunting game. Part B involves answering three questions about threads and processes. Students are instructed to submit Part A as a zip folder containing the two C programs, and Part B as a single text file containing the answers. Files must be named according to the provided format and submitted to the correct links on myElearning by the due date.

Uploaded by

Daniel McDougall
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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 > [Link]
 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 "[Link]" 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

Common questions

Powered by AI

In multithreaded applications, the register set is considered a per-thread item because each thread operates independently and manages its execution context, which includes the register values. Although the machine physically has a single set of registers, each thread must maintain its own logical view of registers to ensure accurate control flow and data processing. This separation is crucial for correct parallel processing where threads operate concurrently .

A single-threaded server might outperform a multithreaded server in environments where the overhead of context switching and thread management outweighs the benefits of concurrent processing. For instance, in applications where the server handles lightweight tasks that require minimal I/O and are not CPU-bound, the simplicity and reduced resource consumption of a single-threaded approach can lead to better performance .

In the 'myshell.c' program, the PATH environment variable plays a crucial role by specifying the directories the shell should search to locate executable programs. The program should utilize getenv() to retrieve the PATH variable values and iterate over these directories to check for the existence of the user-specified executables using the stat() function. This ensures the shell can execute commands not present in the current directory by appropriately searching the specified paths .

Processes A and B in 'DuckHunting' collaborate to update player statistics by performing specific tasks: Process A calculates the total points for each player by awarding two points for each duck killed and deducting one point for each duck missed. Process B then calculates the rank of each player based on their scores. To ensure correct task completion and maintain data integrity during concurrent access, semaphores are used to synchronize these processes and protect the critical region, ensuring no race conditions occur .

Semaphores provide critical benefits for process synchronization in the 'DuckHunting' game's score calculation. They ensure that processes A and B do not access shared resources, such as player data structures, simultaneously, thus preventing race conditions. Semaphores manage access to these critical sections by controlling the sequence in which the processes execute, ensuring data consistency and integrity throughout their concurrent operations .

Beyond executing simple commands, the 'myshell.c' program should support advanced functionalities such as output redirection and command piping. Output redirection is implemented by detecting the '>' character, allowing the command’s output to be directed to a specified file. Command piping requires parsing the '|' character to connect the output of one command as input to another, facilitating process chaining. These functionalities should be implemented using system calls like execv() for execution and careful management of file descriptors for redirection and pipes .

Using a procedure like create_global to allocate storage for global variables instead of working directly with the values has significant implications for memory management and thread safety. It allows for centralized control over memory allocation, reducing duplication and promoting consistency across threads. Furthermore, it eases the enforcement of synchronization protocols, as access to global variables through pointers can be better managed. Direct value manipulation could lead to synchronization challenges, data inconsistency, and inefficient memory use .

The fundamental purpose of implementing a custom shell program like 'myshell.c' is to test understanding of basic process manipulation in C. This program needs to execute commands specified by the user within a Linux environment, by parsing command inputs and handling functionalities such as searching for executables in the PATH, executing programs with specified parameters, and implementing features like output redirection and piping .

Context switching, the process of storing and restoring the state of threads, affects server performance by introducing overhead. Each switch requires CPU time to save the current state and load the new one, which can degrade performance, especially in systems with heavy concurrency demands. In models with many lightweight threads requiring frequent context switches, the cumulative context-switching overhead may outweigh the performance benefits of concurrency, making single-threaded or simplified threading models more efficient .

While the procedures for handling global variables in threads could theoretically work with values instead of pointers, the implications would include increased memory consumption and potential inefficiencies. Using values directly copies data, which could lead to higher memory usage especially with large data structures, and possibly introduce synchronization issues if not managed properly. Pointers allow referencing shared data efficiently, crucial for maintaining thread safety and performance .

You might also like