Operating Systems - Programming Assignment 2 | CS 570, Assignments of Operating Systems

Material Type: Assignment; Class: OPERATING SYSTEMS; Subject: Computer Science; University: San Diego State University; Term: Spring 2004;

Typology: Assignments

Pre 2010

Uploaded on 03/28/2010

koofers-user-nrp
koofers-user-nrp 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
The goal of this assignment is to learn about Win32 threads. A good description of the related system
services is given in "Win32 System Services" by Marshall Brain (2nd edition pages: 139-157, 191-193,
and 669-673; 3rd edition pages: 116-130, 151-155, and 549-555)
The assignment has three parts:
PART A - Create a simple thread
This part deals with a single thread, which has only one integer parameter. It simulates crawling of a turtle by
using a random number generator. (In part A there is only one turtle, but in parts B and C there will be several
turtles which participate in a race.)
Part A is entirely held in a single file, p21.cpp, which should have three sections of code:
1) Definition of global variables:
#define N 1 // Number of turtles (threads)
double distance[N]; // Current turtle distances from the goal [in]
double step; // Maximum step size of turtles
int dt; // Time needed for a turtle to make one step [ms]
2) Main program:
void main(int argc, char *argv[]){...}
3) Thread function:
void Turtle(DWORD id){...} // id is parameter passed to the thread
The main program gets the initial distance from the goal (distance[0]), the step size (step), and the step
time (dt) via command-line arguments. A typical invocation of the program should be like this:
p21 2 0.25 1000 // The turtle runs distance of 2 inches with steps
// no bigger than 1/4 inches, a step takes 1 sec
After data initialization, the main program creates a thread that emulates a slowly crawling turtle:
HANDLE h[N];
DWORD id[N] = {0};
DWORD ThreadID[N];
h[0] = CreateThread(0, 0,
(LPTHREAD_START_ROUTINE) Turtle,
(VOID *)id[0],
0, &ThreadID[0]);
The main program then enters an endless loop in which it outputs the current position of the turtle
(distance[0]) and goes to sleep for 900 ms (use the run time function Sleep(900) (after this call you
don’t have to check the success status). The loop should be terminated when the turtle reaches the goal, i.e.
when the thread exits. How the main program knows when the thread has exited? It uses system function
CS570 Operating Systems Spring 2004
Programming Assignment #2 Assigned: Feb 4 Due: Feb18
pf3

Partial preview of the text

Download Operating Systems - Programming Assignment 2 | CS 570 and more Assignments Operating Systems in PDF only on Docsity!

The goal of this assignment is to learn about Win32 threads. A good description of the related system services is given in "Win32 System Services" by Marshall Brain (2 nd^ edition pages: 139-157, 191-193, and 669-673; 3 rd^ edition pages: 116-130, 151-155, and 549-555)

The assignment has three parts:

PART A - Create a simple thread

This part deals with a single thread, which has only one integer parameter. It simulates crawling of a turtle by using a random number generator. (In part A there is only one turtle, but in parts B and C there will be several turtles which participate in a race.)

Part A is entirely held in a single file, p21.cpp, which should have three sections of code:

  1. Definition of global variables:

#define N 1 // Number of turtles (threads) double distance[N]; // Current turtle distances from the goal [in] double step; // Maximum step size of turtles int dt; // Time needed for a turtle to make one step [ms]

  1. Main program:

void main(int argc, char *argv[]){...}

  1. Thread function:

void Turtle(DWORD id){...} // id is parameter passed to the thread

The main program gets the initial distance from the goal (distance[0]), the step size (step), and the step time (dt) via command-line arguments. A typical invocation of the program should be like this:

p21 2 0.25 1000 // The turtle runs distance of 2 inches with steps // no bigger than 1/4 inches, a step takes 1 sec

After data initialization, the main program creates a thread that emulates a slowly crawling turtle:

HANDLE h[N]; DWORD id[N] = {0}; DWORD ThreadID[N]; h[0] = CreateThread(0, 0, (LPTHREAD_START_ROUTINE) Turtle, (VOID *)id[0], 0, &ThreadID[0]);

The main program then enters an endless loop in which it outputs the current position of the turtle (distance[0]) and goes to sleep for 900 ms (use the run time function Sleep(900) (after this call you don’t have to check the success status). The loop should be terminated when the turtle reaches the goal, i.e. when the thread exits. How the main program knows when the thread has exited? It uses system function

CS570 Operating Systems Spring 2004

Programming Assignment #2 Assigned: Feb 4 Due: Feb

WaitForSingleObject(). After exiting the loop, the main program announces that the turtle has reached the goal and exits.

The function Turtle() first sets up the seed of the random number generator:

srand((unsigned)time(NULL)+id); // will be discussed in class

then enters an endless loop in which it makes random steps:

distance[id] -= (step/RAND_MAX)*rand();

and goes to sleep for dt msec. The loop should be terminated when distance becomes zero (distance[0]

< 0.0). After that, function Turtle() returns, i.e. the thread terminates.

PART B - Create Multiple Threads with Parameters

(This part deals with any number of threads, which are passed multiple parameters grouped within a structure.)

Extend your program p21.cpp into p22.cpp with 5 threads (N=5). The endless loop in main program terminates when ANY of the threads exits (for this use system function: WaitForMultipleObjects()). In addition, the main program announces the winning turtle before it exits (The winning turtle is the one whose thread has terminated first).

In part A the turtle gets an ID number via thread parameter. In part B, all turtles get a unique ID (= 0,1,2,3,4) and a unique name. (You can give the turtles arbitrary names. Funny names allowed.) In order to pass two or more parameters to a thread, you need to pack them into a structure, and then pass the pointer to the structure as a single thread parameter:

typedef struct {DWORD id; // Turtle ID char name[10]; // Turtle name } TurtleParameters;

You will modify the code for the thread function, so that each newborn turtle issues the following message:

"Hi, my name is xxxx, my ID is x)"

The part B has an additional feature: external termination of program on CTRL-C key.

For this you need to add to p22.cpp the CTRL-C handler (see M. Brain’s book):

BOOL CtrlC (DWORD c) {...} // Will be discussed in class

This handler prints on the screen the message: "I am terminated by CTRL-C!", then returns FALSE. The reaction should be on c == CTRL_C_EVENT.

The handler should be registered at the beginning of the main program:

BOOL ok; ok = SetConsoleCtrlHandler ((PHANDLER_ROUTINE) CtrlC, TRUE) if (!ok) cout << GetLastError() << endl;