PDF

Announcements
z
Program #4
– Due Friday at 8:00 AM
– Extended 12 hours due to machine down time
z
z
Program #5 on the Web later today
Reading
– Notes (Today)
– Bryant & O’Hallaron 10.10 (Tuesday)
CMSC 212 – S07 (lect 20)
1
Project #5
z
Two Goals
– Implement a tree API
– Develop test cases for it
z
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
CMSC 212 – S07 (lect 20)
2
Events
z
Many programs need to respond to external activity
–
–
–
–
z
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?
z
Events are a way to represent these activities
– can also includes other things (such as timer alarms)
CMSC 212 – S07 (lect 20)
3
Event Driven Programming
z
z
Program consists of a main loop waiting for events
Define set of functions to handle each event
– process event, and then return
z
Often uses function pointers to define events and
their handlers
CMSC 212 – S07 (lect 20)
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 – S07 (lect 20)
5
Handling Multiple Events - Select
z
Often events are coming from file descriptors
– keyboard input
– file I/O
– network communication
z
Select system call
–
–
–
–
blocks waiting for I/O to be ready on a set of file descriptor
returns when one or more file descriptors have I/O ready
does NOT perform the I/O
can include a timeout
• prevent blocking forever
• allows time based events
CMSC 212 – S07 (lect 20)
6
Select System Call
z
int select(int n, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
–
–
–
–
z
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);
• is 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)
7
Why select for writing?
z
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.
z
Why select for errors?
– learn about I/O problems
– learn when a network connection closes down
CMSC 212 – S07 (lect 20)
8
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)
9
Handling I/O Events
z
Reading from Standard input
void readStandardIO() {
ret = read(0, line, sizeof(line));
/* process line */
}
CMSC 212 – S07 (lect 20)
10
Coordinating Events
z
Sometimes Events come from sources other than file
descriptors
– other threads
z
Solution:
– use a queue of events
– threads wishing to communicate en-queue events
main loop for event queue paradigm:
while (1) {
event = myEventQueue->dequeue();
/* process event */
anotherEventQueue->enqueue(response);
}
z
CMSC 212 – S07 (lect 20)
11
How do event-driven programs maintain
state?
z
Ordinary programs, and threads, use variables on the
stack.
– Thread's stack persists across thread context switches
z
Event-Driven programs can't keep state on the stack.
– Since each callback must return immediately.
– We want to associate data/state with each callback.
z
Solution:
– 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
CMSC 212 – S07 (lect 20)
12
How to Keep Programs Modular
z
Problem:
– Multiple parts of the system want to process events
– Everyone wants to write to a main loop
z
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)
13
Example Using Callbacks
typedef int (*handlerFunc)(int fd, void *arg);
typedef struct {
handlerfunc handler;
int fd;
void *arg;
} eventHandler;
CMSC 212 – S07 (lect 20)
14
Midterm #2
ave Changes
z
z
Exams will be returned now
Overall Stats:
1AB
1CD
2A
2B
2C
3
4
5
Total
Minimum
3
4
0
0
0
5
2
0
44
Maximum
8
8
13
15
15
15
16
10
98
12.1
8.7
10.9
11.9
6.7
75.8
3.0
4.9
2.7
3.4
2.6
13.4
Mean
7.6
7.2 10.9
Std Dev
1.0
1.2
CMSC 212 – S07 (lect 20)
2.7
15