CS 355 - Systems Programming:
Unix/Linux with C
Event-Driven Programming
Reference: Molay, Understanding Unix/Linux Programming, Chapter 7.1-7.6
Video games and operating systems
Space programming: the curses library
Allows the programmer to set set the position of the cursor and control the appearance of text on a terminal screen. The screen is treated as a 2D array. Each location is identified by the (row, column) pair of values with (0,0) at the upper left corner of the screen. Uses a virtual screen where all changes are made. Function refresh() is used to apply these changes to the actual screen.
Basic curses functions:
Time programming: sleep()
Function sleep(unsigned int s) suspend thread execution for an interval of s seconds.
#include <stdio.h> #include <curses.h> main() { int i; initscr(); clear(); for(i=0; i<LINES; i++ ){ move( i, i+i ); addstr("Hello, world"); refresh(); sleep(1); move(i,i+i); /* move back */ addstr(" "); /* erase line */ } endwin(); }
Time programming: alarm()
sleep(n) works as follows:
signal(SIGALRM, handler); /* install a handler for alarm */ alarm(n); /* set the alarm */ pause(); /* suspend the execution */
Time programming: interval timers
int getitimer(int which, struct itimerval *val);
int setitimer(int which, const struct itimerval *newval, struct itimerval *oldval);
Use the set_ticker() function described on p. 213 to set an interval timer.