cgs 3763 - process and threads
TRANSCRIPT
-
8/12/2019 CGS 3763 - Process and Threads
1/3
CGS 3763Operating Systems Concepts
Spring 2013
HW 2Process and Threads
Due date: Jun. 19 2013
The purpose of this assignment is to get you more familiar with threads, by implementing a simple
multithreading environment. The code shown below demonstrates how can one find out the range
of some dataset (defined as |maxmin|) using a single for loop to iterate through the data.
int FindRange(int* dataset, int size){
int i;
int max = dataset[0];
int min = dataset[0];
for(i = 1; i < size; ++i){
if(dataset[i] < min){
min = dataset[i]; //this will be executed by one thread
}
if(dataset[i] < max){max = dataset[i]; //this will be executed by another thread
}
}
return fabs(maxmin);
}
You are to write a program which uses two threads: one for finding the maximum and the other for
the minimum of some dataset. A skeleton code is provided such that you may finish your
assignment (please use this code since the output is known):
#include #include
#include
#include
#include
#include
using namespace std;
struct ThreadData{
int total;
int min;
int max;
int* values;
};
void* getMin(void* args);
void* getMax(void* args);
-
8/12/2019 CGS 3763 - Process and Threads
2/3
int getRange(struct ThreadData* data){
return fabs(data->max - data->min);
}
int main(int argc, char** argv){
srand(100);
int N = 32000;
struct ThreadData* data = (struct ThreadData*)malloc(sizeof(struct ThreadData*) +
N*sizeof(int));
data->total = N;
data->values = (int*)calloc(N, sizeof(int));
for(int i = 0; i < N; ++i){
data->values[i] = rand();
}
//TODO
//create both threads, one to find min and the other find max
//REMEMBER: If you change the value of something in a pointer from a function,//the change persists after the function (parameter by reference)
//wait for all threads to finish
//END TODO
cout
-
8/12/2019 CGS 3763 - Process and Threads
3/3
Linux GCC/G++ compiling (in command line, you can use IDEs also e.g., Eclipse):
To compile a program which includes the library pthread.h you must first ensure that you have the
library installed (and you should since most likely is inside of libc). Then upon compiling, you must
tell the linker that it needs to include the library:
g++ -pthread -o binary_nameg source_file.cpp
Thepthread will tell the linker to search for the pthread library, -o tell the compiler to name the
binary file (executable) as binary_name in this example, theg flag will generate information that
you can use with the gdb debugger, and finally you feed the source file.
Submit your code on webcourses, with the necessary comments. A discussion board will be created
for this assignment in case you have questions.