Threads
Due: 11:00pm, Tuesday, September 26, 2023
Get the Assignment Code
Login to your university Unix account and copy the assignment code to a location under your home directory:
cp -r /export/home/public/schwesin/csc328/assignments/project3 LOCATION
where LOCATION is a directory of your choosing.
Description
For this project you will write a C program that creates 1 to 10 child
processes using the pthread library.
Program Outline
I. Declare variables
- Declare a global integer variable named
X. - Declare an integer variable named
numin the main function. - Declare an integer pointer variable,
numpin the main function; the memory for the pointed to value must be in the heap.
- Process command line arguments
Accept four command-line arguments:
- The number of processes to create between 1 and 10 inclusive.
- Initial value to assign to
X. - Initial value to assign to
num. - Initial value to assign to
numps dereferenced value.
- Initialize the variables declared in part I.
- Assign the initial value of
X. - Assign the initial value of
num. - Assign the initial value that
numppoints to.
- Create threads
Print a statement indicating the program is creating threads, the number of threads to create, the current value of
X, the current value ofnum, and the current value ofnump.Create a number threads corresponding to the first command line argument. Each sub-thread must perform processing as specified below.
V. End the program
- Print a statement indicating the program has completed processing.
- Clean up any necessary resources.
Thread Processing
The main thread must:
- Coordinate the sub-threads
Each sub-thread must do the following:
Print the following information: the text string “BEFORE increment”, thread number, thread ID, value of
X, value ofnum, and value ofnump.Add 50 to the value of
X.Increment the value of
num.Increment the value that
numppoints to.Print the following information: the text string “AFTER increment”, thread number, thread ID, value of
X, value ofnum, and value ofnump.End the thread.
The print statements for a sub-thread should follow this format assuming the
command line arguments for X, num, and nump are 500, 5, and 10
respectively. (note some of the values may be different):
BEFORE increment: Thread number 1 with TID of 3795580672: X = 500, num = 5, nump = 10
AFTER increment: Thread number 1 with TID of 3795580672: X = 550, num = 6, nump = 11
Makefile Rules
The following rules must be added to the provided Makefile:
Add a rule to build an executable named
project3from your C program. This must be the default action of the Makefile. Note that the pthread library needs to be linked in order for the program to compile.Add a rule called
cleanto remove any temporary files and theproject3executable.
Turning in the Assignment
To turn in the assignment, execute the submit script by running the command
make submit
from within the project3 directory.
Grading Criteria
You must use the C programming language for this assignment.
You must follow the department documentation standards.
Correct implementation of the specification.
The program must follow good general programming practices. For example, utilize functions, error handling, free memory, etc.. For error handling, be sure to utilize
perror()and/orstrerror()appropriately and include a user-friendly error message.The program must follow recommend programming for creating threads, which means ending all threads and the process appropriately.
Note: there is no need to use mutual exclusion (locks) for this project.
If your code crashes due to a segmentation fault, then you will receive a failing grade for this assignment.