140 likes | 266 Views
CSC 552.201 - Advanced Unix Programming, Fall, 2008. Welcome back to UNIX System Programming! Monday, September 15, class 4. UNIX random access files. lseek(int fd, int offset, int whence) seeks within a file, offset is location to which to go
E N D
CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome back to UNIX System Programming! Monday, September 15, class 4
UNIX random access files • lseek(int fd, int offset, int whence) • seeks within a file, offset is location to which to go • whence is one of SEEK_SET (from start), SEEK_CUR (from current), SEEK_END (from end). • Do not use O_APPEND! • It seeks to the end of file on appends. • See ~parson/UnixSysProg/lecture4/seeklock/.
fcntl() (file control) is a catch all, like ioctl() (I/O control) • int fcntl(int fildes, int cmd, /* arg */ ...); • F_GETLK, F_SETLK, F_SETLKW manipulate file locks. • Third argument to F_SETLK[W] must be a * struct flock. short l_type; /* lock operation type */ short l_whence; /* SEEK_SET, SEEK_CUR, or SEEK_END */ off_t l_start; /* starting offset from base */ off_t l_len; /* lock length; l_len == 0 means until end of file */ int l_sysid; /* system ID running process holding lock */ pid_t l_pid; /* process ID of process holding lock*/ • Struct l_type field gets F_RDLCK (shared), F_WRLCK (exclusive), or F_UNLCK (unlock).
A multi-process (and eventually networked) game framework • /export/home/faculty/parson/gchess • GNU chess is a terminal I/O chess game • http://ftp.gnu.org/gnu/chess/ • http://www.gnu.org/software/chess/chess_faq.html • gzcat gnuchess-5.07.tar.gz | tar xvf – • cd gnuchess-5.07 ; ./configure ; gmake • cd src ; export PATH=“`pwd`:${PATH}” • OR cd src ; setenv PATH =“`pwd`:${PATH}” • gnuchess builds in the src/ directory.
Play terminal I/O chess and examine its process • gnuchess • show board, e2 e4, etc. • ps -flu parson – inspect PID and PPID 0 O parson 14114 11421 gnuchess • pfiles 14114 • It is using only stdin, stdout and stderr! • They are all type S_IFCHR. • “quit” or “exit” to get out
xboard is a GUI that runs and displays gnuchess • http://www.tim-mann.org/xboard.html • cd /export/home/faculty/parson/gchess • gzcat xboard-4.2.7.tar.gz | tar xvf – • cd xboard-4.2.7 ; ./configure ; gmake • export PATH=“`pwd`:${PATH}” • OR setenv PATH =“`pwd`:${PATH}” • xboard builds in the xboard-4.2.7 / directory. • Invoking “xboard” requires having Hummingbird -> Exceed or another X11 display server running on your terminal.
Processes under xboard • ps –flu shows three processes • 0 S parson 16365 11421 ./xboard • 0 S parson 16366 16365 /bin/sh /export/home/faculty/parson • 0 O parson 16367 16366 gnuchess xboard • 16367 shows stdin, stdout and stderr open • All 3 are type S_IFIFO
Processes under xboard continued • 16366 shows four open file descriptors • fd 0, 1 and 2 are S_IFIFO • fd 19: S_IFREG, O_RDONLY|O_LARGEFILE FD_CLOEXEC • 16365 shows 8 open file descriptors • It is good that we don’t have to analyze X11 communication protocols in order to get started! • S_IFCHR, S_IFSOCK, S_IFDOOR, S_IFIFO
Assignment: Spy on GNU chess! • Your initial assignment is to write an interceptor that sits between xboard and gnuchess and logs interactions while you play chess. Use your trace log plus source code and docs to understand the xboard <-> gnuchess command and results protocol. • xboard <-> interceptor <-> gnuchess xboard • stdin log, stdout log, stderr log for gnuchess
The bigger assignment: Sound • Install ChucK on a machine with some speakers. http://chuck.cs.princeton.edu/ • My chess program, linked to my web page, as a ChucK directory, with a README. • Open Sound Control (OSC) over UDP datagrams a ChucK process running my sound generator speakers • But, Professor Parson, where do the OSC/UDP datagrams comes from?
A Python chess-to-music game • One or more Python game GUIs Python game server that takes commands from command line (stdin / stdout), and also accepts GUI commands from UDP datagrams. • This Python program generates OSC/UDP ChucK. • The command line server, like gnuchess, is a stdin / stdout / stderr process.
Project goals • Allow Python chess-to-music program to play a human against the machine (gnuchess). • Support alternative, networked GUI (in Python) for gnuchess. • Have some fun while learning fork, exec, file IO, sockets, networking and multithreading. • Live long and prosper.
Summary • Ultimately you are creating a game framework to integrate two or more running games. • An ideal goal would be to allow support for different protocols using a plugin parser. • The immediate goal is to intercept, redirect, etc. commands passing between xboard <-> gnuchess, and also my Python game server <-> Python GUI, and to translate between them.