Programming Homework Help

Programming Homework Help. 2 level C assignment about threading and multithreading

Check the description file that I uploaded.

2 tasks: 4.1Theards and 4.2 Pool

Write a Makefile for each task, it should be compiled in linux system by make.

There are expected output in Lab9files/CI/objects, which are in the zip files that I uploaded. Pls compare them after running your code

4.1 Threads

4.1.1 Problem

You will create three programs for testing various types of thread features.

4.1.2 Preconditions

You are required to write three programs for creating and testing threads:

1. threads: You will create a program which accepts an array and squares each value in the array using threads.

2. unsafe: You will create a program which attempts to increment and print a variable without the use of

semaphores.

3. safe: You will create a program which attempts to increment and print a variable by protecting your critical

section with semaphores.

Each program must include a relevant .c le, which should contain all of your function implementations, and a rele-

vant .h le, which should contain your structure denitions, any necessary typedefs, and all of your forward function

declarations. When you compile, you will need to include the source le in your command in order to ensure the

functions exist during the linking process. You may include any additional helper functions as you see t. Since you

are dealing with pointers, you will need to check all of your pointers to ensure they are not null. Trying to perform

operations on null will lead to segmentation faults or other program crashes at run-time.

Details on threads and semaphores can be found in the Multithreading section of this document. The bool type

referenced in this contract is found in <stdbool.h>. You are expected to do basic error checking (such as checking

for null pointers and correct index boundaries).

Your threads program must include the following structs (typedef-ed appropriately):

Structure Name Fields Functionality

Args (typedef Args) int* arr A pointer to an array that you will recalculate the values of.

int start The starting index to perform calculations from.

int end The ending index to perform calculations to (non-inclusive).

Your threads program must include the following functions:

Requirement Conditions

Function void* ll(void*)

Input Parameters A void pointer to an Args struct.

Return Value A void pointer (to t the thread function requirement). NULL in practice.

Notes This should be passed into the thread creation function. This should return NULL. When

executed, this function should iterate through the [start; end) in the provided array

and square each value before returning null.

Requirement Conditions

Function void ll memory(int*, int)

Input Parameters An int pointer to an integer array, and an integer representing the number of threads to make.

Return Value None.

Notes This function should create the correct number of threads N, create an Args struct for every

thread, then divide the array into N equal ranges and execute a thread on each of those ranges.

The provided integer array is guaranteed to have 10,000,000 values.

Your unsafe program must include the following structs (typedef-ed appropriately):

Structure Name Fields Functionality

Count (typedef Count) int counter A value for counting upward to 1000.

Your unsafe program must include the following functions:

Requirement Conditions

Function void* counting(void*)

Input Parameters A void pointer to a Count struct.

Return Value A void pointer (to t the thread function requirement). NULL in practice.

Notes This should be passed into the thread creation function. This function should

continue to process as long as the counter value is < 1000. If it is < 1000,

increment the counter value by 1, then print it to the screen on its own line.

Requirement Conditions

Function void count variable(int)

Input Parameters An integer representing the number of threads to create.

Return Value None.

Notes This function should create a single Count struct and create the correct number of threads,

providing the same Count struct as an argument to each thread.

Your safe program must include the following structs (typedef-ed appropriately):

Structure Name Fields Functionality

Count (typedef Count) int counter A value for counting upward to 1000.

4.2 Pool

4.2.1 Problem

You are required to design and implement a thread pool.

4.2.2 Preconditions

You are required to write a program for creating a simple thread pool. This consists of pool.c, which should con-

tain all of your function implementations, and pool.h, which should contain your structure denitions, any necessary

typedefs, and all of your forward function declarations. When you compile, you will need to include the source le

in your command in order to ensure the functions exist during the linking process. You may include any additional

helper functions as you see t. Since you are dealing with pointers, you will need to check all of your pointers to

ensure they are not null. Trying to perform operations on null will lead to segmentation faults or other program

crashes at run-time.

To create a thread pool, you will follow a similar method as presented in the calculator contract of Lab 4. To start,

you must create an Operation struct which will contain a function pointer to a math function, an integer value a,

and an integer value b. You will then create an Args struct which contains a pointer to an Operation and a boolean

for determining if the thread has completed execution.

In order for your Operation to successfully process, you will also need to write a function for add, sub, and mul.

Each of those functions accepts two integer values and returns an integer value. The operations are always performed

a op b.

The structure of the operation list le is such that there are 10,000 operations contained in the le, each on their

own line. The lines are each represented as three integers, where the rst integer is the opration code (op), the second

number is the rst operand (a), and the third number is the second operand (b) for that calculation. The op codes

are mapped such that 0=add, 1=sub, and 2=mul. Thus an operation 0 42 15 would perform 42+15, while another

operation 2 10 12 would perform 10 12.

The main point of execution in this program is the execute thread pool function. This function will create all

necessary data structures, initialize an array of thread ID values (your thread pool), and then read in an operations

le. Reading in operations is performed by the read operations function and will read the operations into a queue

data structure. Once the Operations have been stored in the queue, you can repeatedly dequeue the next Operation

from the queue, then create an Args struct. Utilizing your current data structures in any way you choose, you should

create threads for executing operations until the thread pool is full.

Once the thread pool is full, constantly iterate through the threads and check if any have completed processing their

operation. If they have, you can retrieve the return value from the thread and store it in an array list. Once that is

done, set up an Args struct, dequeue the next Operation, and start a new thread in the current thread pool position.

If the queue no longer has any Operations in it, you can stop iterating through the pool. Wait for the nal return

values, retrieve them, then return the array list.

Your compute function should accept an Args struct, extract the Operation stored inside, then run the operation

function on the two values stored in the Operation itself. Set any necessary completion ags and return the calculated

value.

The print sorted function should accept a pointer to an ArrayList, sort the values stored in the ArrayList, then

print them in order from smallest to largest. You may nd it benecial to use a data structure from a previous lab.

The main function will expect compareInt and printInt functions to be implemented, even if you don’t use them.

Your pool program must include the following structs (typedef-ed appropriately):

Structure Name Fields Functionality

Operation (typedef Operation) int (*op)(int, int) A function for performing a math operation on a and b.

int a The rst value passed to the op function.

int b The second value passed to the op function.

Args (typedef Args) Operation* operation A pointer to a valid Operation structure.

bool is complete A boolean showing if the operation has been calculated.

Your pool program must include the following functions:

Requirement Conditions

Function void* compute(void*)

Input Parameters A void pointer to an Args struct.

Return Value A void pointer to the integer result of the Operation performed.

Notes None.

Requirement Conditions

Function bool read operations(char*, Queue*)

Input Parameters A char* (string) representing a le name, and a pointer to a Queue.

Return Value True if the operations in the le were successfully read and stored in the Queue. Otherwise

false.

Notes None.

11

Requirement Conditions

Function ArrayList* execute thread pool(char*, int)

Input Parameters A char* (string) representing a le name, and an integer representing the thread pool size.

Return Value A pointer to an array list of calculated Operation values.

Notes None.

Requirement Conditions

Function void print sorted(ArrayList*)

Input Parameters A pointer to an array list of integers.

Return Value None.

Notes This function should sort the values in the array list and print them to stdout.

Programming Homework Help

 
"Our Prices Start at $11.99. As Our First Client, Use Coupon Code GET15 to claim 15% Discount This Month!!"