CS242: Object-Oriented Design and Programming

Similar documents
Common Misunderstandings from Exam 1 Material

de facto standard C library Contains a bunch of header files and APIs to do various tasks

Programming Assignment Multi-Threading and Debugging 2

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

ECE264 Fall 2013 Exam 1, September 24, 2013

Announcements. Lecture 05a Header Classes. Midterm Format. Midterm Questions. More Midterm Stuff 9/19/17. Memory Management Strategy #0 (Review)

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

Intermediate Programming, Spring 2017*

Today s Learning Objectives

CSE 12 Spring 2016 Week One, Lecture Two

Intermediate Programming, Spring 2017*

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

ECE264 Spring 2013 Final Exam, April 30, 2013

Come and join us at WebLyceum

COMP 2355 Introduction to Systems Programming

CSE030 Fall 2012 Final Exam Friday, December 14, PM

CS 0449 Sample Midterm

CS61, Fall 2012 Section 2 Notes

Linked List using a Sentinel

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

CSCE 110 PROGRAMMING FUNDAMENTALS

Linked lists, stacks, queues

CS 223: Data Structures and Programming Techniques. Exam 2

Short Notes of CS201

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

CSE 333 Midterm Exam July 24, Name UW ID#

CS201 - Introduction to Programming Glossary By

cc -o build/test_translate build/test_translate.o b... $

Lecture No.04. Data Structures

CSE 12 Spring 2018 Week One, Lecture Two

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Processes. Processes (cont d)

Multiple choice questions. Answer on Scantron Form. 4 points each (100 points) Which is NOT a reasonable conclusion to this sentence:

Data Structures and Algorithms

CA341 - Comparative Programming Languages

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

Lecture 15a Persistent Memory & Shared Pointers

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Second Examination Solution

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

Understanding Pointers

CSE 333 Midterm Exam Nov. 3, 2017 Sample Solution

CS302 - Data Structures using C++

Lab 4: Tracery Recursion in C with Linked Lists

SYSC 2006 C Winter 2012

Borland 105, 278, 361, 1135 Bounded array Branch instruction 7 break statement 170 BTree 873 Building a project 117 Built in data types 126

CS201 Some Important Definitions

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

Binary Search Tree 1.0. Generated by Doxygen Mon Jun :12:39

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

Financial computing with C++

NYU SCPS X Section 1 Unix Tools. Fall 2004 Handout 9

Recitation 2/18/2012

A brief introduction to C++

CS 261 Fall Mike Lam, Professor. Structs and I/O

One of Mike s early tests cases involved the following code, which produced the error message about something being really wrong:

CSE 333 Midterm Exam Sample Solution 7/28/14

CMSC 341 Lecture 10 Binary Search Trees

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Review for Test 1 (Chapter 1-5)

Name: I. 20 II. 20 III. 20 IV. 40. CMSC 341 Section 01 Fall 2016 Data Structures Exam 1. Instructions: 1. This is a closed-book, closed-notes exam.

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

Computer Systems Assignment 2: Fork and Threads Package

Armide Documentation. Release Kyle Mayes

C++ (Non for C Programmer) (BT307) 40 Hours

ECE 264 Advanced C Programming 2009/01/16

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

CS250 Final Review Questions

Ch. 12: Operator Overloading

CS250 Final Review Questions

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

Name Section Number. CS210 Exam #3 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

Implementing an abstract datatype. Linked lists and queues

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

CS-211 Fall 2017 Test 2 Version A November 29, Name:

CS 104 (Spring 2014) Final Exam 05/09/2014

CS4514 HELP Session 2 Simulation of Datalink Layer Communication

Dynamic memory. EECS 211 Winter 2019

PIC 10B Lecture 1 Winter 2014 Homework Assignment #3

CMSC 341 Leftist Heaps

Chapter 19: Program Design. Chapter 19. Program Design. Copyright 2008 W. W. Norton & Company. All rights reserved.

Pointers as Arguments

The C++ Object Lifecycle. EECS 211 Winter 2019

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

18. Inheritance and Polymorphism

Solution for Data Structure

ch = argv[i][++j]; /* why does ++j but j++ does not? */

Project 2: Shell with History1

CSC 210, Exam Two Section February 1999

Program template-smart-pointers-again.cc

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

Transcription:

CS242: Object-Oriented Design and Programming Program Assignment 5 Part 1 (Linked List Timer Queue) Due Tuesday, March 18 th ; 1997 Part 2 (Heap Timer Queue) Due Tuesday, April 1 st ; 1997) A Timer Queue is an Abstract Data Type (ADT) that allows applications to manage a set of timers. Common operations on a Timer Queue include schedule, cancel, andexpire. This part of your programming assignment focuses upon building the following two implementations of a Timer Queue: Timer List Anunbounded implementation of Timer Queue using the Ordered List implementation from your previous assignment. Timer Heap An implementation of Timer Queue using a heap data structure. The interface for the abstract base class Timer Queue is defined as follows: class Timer_Queue // Provides an interface to timers. // // = DESCRIPTION // This is an abstract base class that provides hook for // implementing specialized policies such as <Timer_List> // and <Timer_Heap>. // = Initialization and termination methods. Timer_Queue (void); // Default constructor. virtual Timer_Queue (void); // Destructor - make virtual for proper destruction of inherited // classes. virtual int is_empty (void) const = 0; // True if queue is empty, else false. virtual const Time_Value &earliest_time (void) const = 0; // Returns the time of the earlier node in the Timer_Queue. virtual int schedule (Event_Handler *event_handler, const void *arg, const Time_Value &delay) = 0; // Schedule an <event_handler> that will expire after <delay> amount // of time. If it expires then <arg> is passed in as the value to // the <event_handler> s <handle_timeout> callback method. This method // returns a <timer_id> that uniquely identifies the <event_handler> // in an internal list. This <timer_id> can be used to cancel an // <event_handler> before it expires. The cancellation ensures that // <timer_ids> are unique up to values of greater than 2 billion // timers. As long as timers don t stay around longer than this // there should be no problems with accidentally deleting the wrong // timer. Returns -1 on failure (which is guaranteed never to be a // valid <timer_id>). virtual int cancel (int timer_id, const void **arg = 0) = 0; // Cancel the single <Event_Handler> that matches the <timer_id> 1

// value (which was returned from the <schedule> method). If arg is // non-null then it will be set to point to the magic cookie // argument passed in when the <Event_Handler> was registered. This // makes it possible to free up the memory and avoid memory leaks. // Returns 1 if cancellation succeeded and 0 if the <timer_id> // wasn t found. virtual int expire (void); // Run the <handle_timeout> method for all Timers whose values are // <= <Time_Value::gettimeofday>. Returns the number of Timers // expired. As shown above, the following are a number of classes associated with the Timer Queue: class Time_Value // Operations on "timeval" structures. // = Useful constants. static const Time_Value zero; // Constant "0". // = Initialization methods. Time_Value (long sec = 0, long usec = 0); // Constructor. Time_Value (const Time_Value &tv); // Copy constructor. // = The following are accessor/mutator methods. long sec (void) const; // Get seconds. void sec (long sec); // Set seconds. long usec (void) const; // Get microseconds. void usec (long usec); // Set microseconds. // = Helper method static Time_Value gettimeofday (void); // = The following are arithmetic methods for operating on // Time_Values. void operator += (const Time_Value &tv); // Add <tv> to this. void operator -= (const Time_Value &tv); // Subtract <tv> to this. friend Time_Value operator + (const Time_Value &tv1, const Time_Value &tv2); // Adds two Time_Value objects together, returns the sum. friend Time_Value operator - (const Time_Value &tv1, const Time_Value &tv2); // Subtracts two Time_Value objects, returns the difference. 2

friend int operator < (const Time_Value &tv1, const Time_Value &tv2); // True if tv1 < tv2. friend int operator > (const Time_Value &tv1, const Time_Value &tv2); // True if tv1 > tv2. friend int operator <= (const Time_Value &tv1, const Time_Value &tv2); // True if tv1 <= tv2. friend int operator >= (const Time_Value &tv1, const Time_Value &tv2); // True if tv1 >= tv2. friend int operator == (const Time_Value &tv1, const Time_Value &tv2); // True if tv1 == tv2. friend int operator!= (const Time_Value &tv1, const Time_Value &tv2); // True if tv1!= tv2. private: void normalize (void); // Put the timevalue into a canonical form. long tv_sec_; // Seconds. long tv_usec_; // Microseconds. class Event_Handler // Provides an interface to an Event Handler; virtual int handle_timeout (const Time_Value &, const void *arg) = 0; The Timer List Implementation The following class provides a specialization of Timer Queue that uses an Ordered List of absolute times. Therefore, in the average- and worst-case, scheduling and canceling Event Handler timers is O(N ) (where N is the total number of timers) and expiring timers is O(K) (where K is the total number of timers that are < the current time of day). #include "Timer_Queue.h" class Timer_List : public Timer_Queue // Provides a simple implementation of timers using a linked list. // = Initialization and termination methods. Timer_List (void); // Default constructor. virtual Timer_List (void); // Destructor virtual int is_empty (void) const; // True if queue is empty, else false. 3

virtual const Time_Value &earliest_time (void) const; // Returns the time of the earlier node in the <Timer_List>. virtual int schedule (Event_Handler *event_handler, const void *arg, const Time_Value &delay); // Schedule an <event_handler> that will expire after <delay> amount // of time. If it expires then <arg> is passed in as the value to // the <event_handler> s <handle_timeout> callback method. This method // returns a <timer_id> that uniquely identifies the <event_handler> // in an internal list. This <timer_id> can be used to cancel an // <event_handler> before it expires. The cancellation ensures that // <timer_ids> are unique up to values of greater than 2 billion // timers. As long as timers don t stay around longer than this // there should be no problems with accidentally deleting the wrong // timer. Returns -1 on failure (which is guaranteed never to be a // valid <timer_id>). virtual int cancel (int timer_id, const void **arg = 0); // Cancel the single <Event_Handler> that matches the <timer_id> // value (which was returned from the <schedule> method). If arg is // non-null then it will be set to point to the magic cookie // argument passed in when the <Event_Handler> was registered. This // makes it possible to free up the memory and avoid memory leaks. // Returns 1 if cancellation succeeded and 0 if the <timer_id> // wasn t found. private: // You fill in here. The Timer Heap Implementation The followingclass provides another specializationof Timer Queue that uses a Heap of absolute times. A Heap is an almost complete, partially ordered binary tree. Heaps are very efficient since in the averageand worst-case, scheduling and canceling Event Handler timers is O(logN ) (where N is thetotal number of timers) and expiring timers is O(log N ) (where N is the total number of timers that are < the current time of day). Note that this is substantially faster than the Timer List implementation. Please refer to your handouts for an explanation of how to implement a Timer Heap. #include "Timer_Queue.h" class Timer_Heap : public Timer_Queue // Provides an optimization implementation of timers using a heap. // = Initialization and termination methods. Timer_Heap (size_t size); // Default constructor, which creates a heap with <size> elements. virtual Timer_Heap (void); // Destructor virtual int is_empty (void) const; // True if queue is empty, else false. virtual const Time_Value &earliest_time (void) const; // Returns the time of the earlier node in the <Timer_Heap>. 4

virtual int schedule (Event_Handler *event_handler, const void *arg, const Time_Value &delay); // Schedule an <event_handler> that will expire after <delay> amount // of time. If it expires then <arg> is passed in as the value to // the <event_handler> s <handle_timeout> callback method. This method // returns a <timer_id> that uniquely identifies the <event_handler> // in an internal list. This <timer_id> can be used to cancel an // <event_handler> before it expires. The cancellation ensures that // <timer_ids> are unique up to values of greater than 2 billion // timers. As long as timers don t stay around longer than this // there should be no problems with accidentally deleting the wrong // timer. Returns -1 on failure (which is guaranteed never to be a // valid <timer_id>). virtual int cancel (int timer_id, const void **arg = 0); // Cancel the single <Event_Handler> that matches the <timer_id> // value (which was returned from the <schedule> method). If arg is // non-null then it will be set to point to the magic cookie // argument passed in when the <Event_Handler> was registered. This // makes it possible to free up the memory and avoid memory leaks. // Returns 1 if cancellation succeeded and 0 if the <timer_id> // wasn t found. private: // You fill in here. Test Driver Code The following code implements a test driver to test your Timer Queue implementation: #include "stdio.h" #include "stdlib.h" #include "assert.h" #include "Timer_List.h" #include "Timer_Heap.h" // Number of iterations for the performance tests. static int max_iterations = 100; // Default size of the heap. static int max_heap = 100; class Example_Handler : public Event_Handler virtual int handle_timeout (const Time_Value &, const void *arg) assert ((int) arg == 42); return 0; static void test_functionality (Timer_Queue *tq) Example_Handler eh; 5

assert (tq->is_empty ()); assert (Time_Value::zero == Time_Value (0)); int timer_id1; timer_id1 = tq->schedule (&eh, (const void *) 1, Time_Value::gettimeofday ()); assert (timer_id1!= -1); assert (tq->schedule (&eh, (const void *) 42, Time_Value::gettimeofday ())!= -1); assert (tq->schedule (&eh, (const void *) 42, Time_Value::gettimeofday ())!= -1); assert (tq->cancel (timer_id1) == 1); assert (tq->is_empty () == 0); assert (tq->expire () == 2); timer_id1 = tq->schedule (&eh, (const void *) 4, Time_Value::gettimeofday ()); int timer_id2 = tq->schedule (&eh, (const void *) 5, Time_Value::gettimeofday ()); assert (timer_id1!= -1 && timer_id2!= -1); assert (tq->is_empty () == 0); void *arg = 0; assert (tq->cancel (timer_id2, &arg) == 1); assert ((int) arg == 5); assert (tq->cancel (timer_id1, &arg) == 1); assert ((int) arg == 4); assert (tq->expire () == 0); struct Timer_Queues Timer_Queue *queue_; // Pointer to the subclass of <Timer_Queue> that we re testing. const char *name_; // Name of the Queue that we re testing. static Timer_Queues timer_queues[] = new Timer_List, "Timer_List", 0, "Timer_Heap", 0, 0, int main (int argc, char *argv[]) if (argc > 1) max_iterations = ::atoi (argv[1]); else if (argv > 2) max_heap = ::atoi (argv[2]); timer_queues[1].queue_ = new Timer_Heap (max_heap); for (int i = 0; timer_queues[i].name_!= 0; i++) fprintf (stderr, "**** starting test of %s\n", timer_queues[i].name_); test_functionality (timer_queues[i].queue_); delete timer_queues[i].queue_; 6

return 0; Getting Started You can get the shells and Makefile for part one of the program from your account on cec. These files are stored in /project/adaptive/cs242/assignment-5/. Here s a script that shows you how to set everything up and get these files: % cd /cs242 % mkdir assignment-5 % cd assignment-5 % cp -r /project/adaptive/cs242/assignment-5/*. % ls Makefile timer-test.c Event_Handler.h Time_Value.C Time_Value.h Timer_Heap.C Timer_Heap.h Timer_List.C Timer_List.h Timer_Queue.C Timer_Queue.h % make The Makefile and various header files are written for you. All you need to do is edit the *.C files to add themethodsthat implementthetimer Queues. Notethat you llneedtocopytheordered List files from your previous assignment and add them to the Makefile. 7