Docsity

Event Driven Programming - Lecture Slides | CMSC 212, Study notes of Computer Science

Toggle sidebar
bg1
1
1
1
CMSC 212 – S07 (lect 20)
Announcements
Program #4
– Due Tomorrow morning
– Extension becuase Scary was down last night
– Baby is back up – use linux.grace.umd.edu
Program #5 on the Web later today
Reading
– Notes (Today)
– Bryant & O’Hallaron 10.10 (Tuesday)
2
CMSC 212 – S07 (lect 20)
Project #5
Two Goals
– Implement a tree API
– Develop test cases for it
Grading
– Do your test cases find bugs?
• We have many buggy versions of the API
• Will your tests report bugs when encountered
– Do your tests have good code coverage?
• Score for % of lines in your implementation that your tests cover
– Private tests for correctness of you API implementation
– multiple parts
• tests of our implementation with your test cases
• tests of your implementation with our test cases
• tests of your implementation with your test cases
– Start by writing test cases (for the public points)
• but you will probably find you want to do it concurrently
pf3
pf4
pf5

Partial preview of the text

Download Event Driven Programming - Lecture Slides | CMSC 212 and more Study notes Computer Science in PDF only on Docsity!

CMSC 212 – S07 (lect 20)^1

Announcements

 Program

  • Due Tomorrow morning
  • Extension becuase Scary was down last night
  • Baby is back up – use linux.grace.umd.edu

 Program #5 on the Web later today

 Reading

  • Notes (Today)
  • Bryant & O’Hallaron 10.10 (Tuesday)

CMSC 212 – S07 (lect 20)^2

Project

 Two Goals

  • Implement a tree API
  • Develop test cases for it  Grading
  • Do your test cases find bugs?
  • We have many buggy versions of the API
  • Will your tests report bugs when encountered
  • Do your tests have good code coverage?
  • Score for % of lines in your implementation that your tests cover
  • Private tests for correctness of you API implementation
  • multiple parts
  • tests of our implementation with your test cases
  • tests of your implementation with our test cases
  • tests of your implementation with your test cases
  • Start by writing test cases (for the public points)
  • but you will probably find you want to do it concurrently

CMSC 212 – S07 (lect 20)^3

Events

 Many programs need to respond to external activity

  • keyboard
  • mouse
  • network
  • sensors

 Many programs can expect input from multiple sources

  • often don't know where the next input will come
  • will the user press a key or move the mouse next?

 Events are a way to represent these activities

  • can also includes other things (such as timer alarms)

CMSC 212 – S07 (lect 20)^4

Event Driven Programming

 Program consists of a main loop waiting for events

 Define set of functions to handle each event

  • process event, and then return

 Often uses function pointers to define events and

their handlers

CMSC 212 – S07 (lect 20)^7

Select System Call

 int select(int n, fd_set *readfds, fd_set *writefds,

fd_set *exceptfds, struct timeval *timeout);

  • n - highest numbered file descriptor in any set
  • readfds, writefds, exceptfds - sets of file descriptors
  • timeout - how long to wait for events
  • return
    • value is number of descriptors in sets
    • sets modified to indicate which file descriptors are active

 set routines

  • FD_ISSET(int fd, fd_set *set);
    • returns integer indicating if fd in the set
  • FD_SET(int fd, fd_set *set);
    • added fd to set
  • FD_ZERO(fd_set *set);
    • clear out the set

CMSC 212 – S07 (lect 20)^8

Why select for writing?

 Why select for writing?

  • Output buffers have finite size.
    • All communication fd's have buffered output.
  • To keep devices busy while application does something else.
  • write() ordinarily blocks if buffer is full; waits for drain.
  • Easy for select() to tell if there is space.
  • So it makes sense to select() for writing.

 Why select for errors?

  • learn about I/O problems
  • learn when a network connection closes down

CMSC 212 – S07 (lect 20)^9

Select Example

int main() { int ret, myServerFD, maxFD = 0; FD_SET set; myServerFD = connectToServer(); while (1) { FD_ZERO(&set); FD_SET(0, &set); FD_SET(myServerFD, &set); if (myServerFD > maxFD) maxFd = myServerFD; ret = select(maxFd+1, &set, NULL, NULL, NULL); if (ret) { if (FD_ISSET(0, &set)) { /* read from standard input / } if (FD_ISSET(myServerFD, &set)) { / read from net */ } } } }

CMSC 212 – S07 (lect 20)^10

Handling I/O Events

 Reading from Standard input

void readStandardIO() {

ret = read(0, line, sizeof(line));

/* process line */

CMSC 212 – S07 (lect 20)^13

How to Keep Programs Modular

 Problem:

  • Multiple parts of the system want to process events
  • Everyone wants to write to a main loop

 Solution:

  • Define an interface to allow event handlers to register
  • Single common event loop
  • Callback functions to specific event handlers

CMSC 212 – S07 (lect 20)^14

Example Using Callbacks

typedef int (*handlerFunc)(int fd, void *arg);

typedef struct {

handlerfunc handler;

int fd;

void *arg;

} eventHandler;