Docsity

Introduction to Event Driven Program - Exam 2 | CMSC 212, Exams of Computer Science

Toggle sidebar
bg1
1
1
1
CMSC 212 โ€“ F05 (lect 18)
Announcements
โ—Exam #2 Regrade Requests
โ€“ Due next Monday
โ—Reading
โ€“ Notes (Today)
โ€“ Notes (Tuesday)
2
CMSC 212 โ€“ F05 (lect 18)
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)
pf3
pf4
pf5

Partial preview of the text

Download Introduction to Event Driven Program - Exam 2 | CMSC 212 and more Exams Computer Science in PDF only on Docsity!

CMSC 212 โ€“ F05 (lect 18)^1

Announcements

l Exam #2 Regrade Requests

  • Due next Monday

l Reading

  • Notes (Today)
  • Notes (Tuesday)

CMSC 212 โ€“ F05 (lect 18)^2

Events

l Many programs need to respond to external activity

  • keyboard
  • mouse
  • network
  • sensors

l 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?

l Events are a way to represent these activities

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

CMSC 212 โ€“ F05 (lect 18)^3

Event Driven Programming

l Program consists of a main loop waiting for events

l Define set of functions to handle each event

  • process event, and then return

l Often uses function pointers to define events and

their handlers

CMSC 212 โ€“ F05 (lect 18)^4

Structure of an Event Driven Program

while (1) {

event = getNextEvent();

switch (event.type) {

case MOUSE_EVENT:

processMouse(event));

break;

case KEYBOARD_EVENT:

processKeyboard(event);

break;

CMSC 212 โ€“ F05 (lect 18)^7

Why select for writing?

l 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.

l Why select for errors?

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

CMSC 212 โ€“ F05 (lect 18)^8

Select Example

int main() { int ret, myServerFD, FD2, maxFd = 0; FD_SET set; myServerFD = connectToServer(); FD2 = connectToServer(); while (1) { maxFd =0 ; FD_ZERO(&set); FD_SET(0, &set); FD_SET(myServerFD, &set); if (myServerFD > maxFd) maxFd = myServerFD; FD_SET(FD2, &set); if (FD2 > 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 / } if (FD_ISSET(FD2, &set)) { / read from other net */ } } } }

CMSC 212 โ€“ F05 (lect 18)^9

Alternative Strategy โ€“ Queue Paradigm

l Sometimes Events come from sources other than file

descriptors

  • other threads

l Solution:

  • use a queue of events
  • threads wishing to communicate en-queue events

l main loop for event queue paradigm:

while (1) {

event = myEventQueue->dequeue();

/* process event */

anotherEventQueue->enqueue(response);

CMSC 212 โ€“ F05 (lect 18)^10

l (),+.-0/21.)#3542),6879):1<;=?>@1./A+5BDC8)FE81<+G=?>IHA=@E5JA1.)K- 1 .L8MNEI=O6./*BDC2E

=@BF1<PIQAR

  • Thread's stack persists across thread context switches

l STJAE./AB#UV)K- JAE9/W4<):6G7.),1.;=P@19/2X B.QYE8E.4=@BZ1GB#E[6./BDCAE=@B#18PIQAR

  • Since each callback must return immediately.
  • We want to associate data/state with each callback.

l ]$6.M0HAB,-N6./9^

  • Keep data in heap allocated structure
  • associate structure instance with file descriptor
  • can include functionPointer+arg
    • function pointer defines what to call
    • arg defines state for that function