Closed iainworkman closed 8 years ago
Some notes on Windows threads:
#include "common.h"
#include <time.h>
#include <stdio.h>
/* calculates the squares */
int square();
/* */
int getSystemTime() {
clock_t clockTime;
int currTimeInms;
double milliSeconds;
clockTime = clock();
milliseconds = ((double)clockTime / CLOCKS_PER_SEC) * 1000;
currTimeInMs = (int) milliseconds;
if(returnTime != NULL) {
*returnTime = currTimeInMs;
} else {
return -1;
}
return 0;
}
Write a WINDOWS program that creates multiple threads that each perform a specific task until a deadline, without synchronization between the processes/threads of any kind. The number of child threads (threads) and the deadline (deadline) and the maximum integer (size) are to be command-line parameters. Each thread does the same task. The task is given below which utilizes the third command-line parameter (size). The usage is as follows: partA1 threads deadline size. Parent thread task:
Child thread task: Once created, each child then executes a procedure to calculate (but not print out) the squares of each of the first size positive integers in a loop (i.e., 12, 22, 32,42, 52). The restriction is that procedure Square() cannot use any multiplication operations (I want a simple operation that will take a long time to complete and a lot of stack space). Instead, use the following algorithm:
An example invocation of this program is the following: partA1 4 3 200000. This creates 4 child threads, and has each thread compute Square(1), Square(2), Square(3) ... Square(200000) with a deadline of 3 seconds from the time the parent thread starts running.
You may need to have size be a very large number and deadline to be a fairly small number to get interesting results from this operation.
Upon exit, Each child should print the following: elapsed real time since the child began. number of invocations of the Square() procedure by this thread. You will need to come up with some strategy for counting the number of recursive calls that does not change the parameter list for Square(). Consider an array of counters, indexed by a parameter (threadId) passed to the thread upon creation. The parent and child threads would both have access to this counter. You will need to become familiar with the following Win32 System calls: CreateThread(), GetSystemTime(), and Sleep().