csc 552.201 - advanced unix programming, fall, 2008 welcome back to unix system programming! monday,...

14
CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome back to UNIX System Programming! Monday, September 15, class 4

Upload: gabriel-james

Post on 12-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome back to UNIX System Programming! Monday, September 15, class 4

CSC 552.201 - Advanced Unix Programming, Fall, 2008

Welcome back to UNIX System Programming!

Monday, September 15, class 4

Page 2: 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/.

Page 3: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome back to UNIX System Programming! Monday, September 15, class 4

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

Page 4: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome back to UNIX System Programming! Monday, September 15, class 4

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.

Page 5: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome back to UNIX System Programming! Monday, September 15, class 4

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

Page 6: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome back to UNIX System Programming! Monday, September 15, class 4

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.

Page 7: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome back to UNIX System Programming! Monday, September 15, class 4

xboard under X11 on Sun

Page 8: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome back to UNIX System Programming! Monday, September 15, class 4

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

Page 9: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome back to UNIX System Programming! Monday, September 15, class 4

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

Page 10: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome back to UNIX System Programming! Monday, September 15, class 4

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

Page 11: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome back to UNIX System Programming! Monday, September 15, class 4

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?

Page 12: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome back to UNIX System Programming! Monday, September 15, class 4

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.

Page 13: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome back to UNIX System Programming! Monday, September 15, class 4

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.

Page 14: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome back to UNIX System Programming! Monday, September 15, class 4

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.