Transcription of UNIX Systems Programming I - Alan Dix
1 unix Systems Programming IShort Course NotesAlan Dix 1996 Course NotesAlan Dix 1996I/iUNIXS ystemsProgrammingICourseOutlineAlan 1 unix basicsthe unix API, system callsand manualsSession 2file I/O and filtersstandard input and output, read,write and openSession 3makefiles andargumentsmake, argc & argv andenvironment variablesSession 4file manipulationcreating and deleting files anddirectories, links and symboliclinksSession 5terminal I/Osimilarities to file I/O, tty drivers,stty & gtty, termcap and cursesSession 6signals and timecatching signals and problemsthat arise, delays and finding thetimeSession 7mixing C and scriptsshell scripts, getting informationbetween C and shell, puttingthem togetherUNIXS ystemsProgrammingIShort Course NotesAlan Dix 1996I/iiUNIXS ystemsProgrammingIReading The unix V Environment,Stephen R.
2 Bourne,Wiley, 1987, ISBN 0 201 18484 2 The author of the Borne Shell! A 'classic' which deals with systemcalls, the shell and other aspects of unix . unix For Programmers and Users,Graham Glass,Prentice-Hall, 1993, ISBN 0 13 061771 7 Slightly more recent book also covering shell and C Programming . BEWARE unix Systems differ in details,check on-line documentation unix manual pages:man creat of the system calls and functions are in section 2 and 3 of themanual. The pages are useful once you get used to reading them! The include files themselves/usr/ even more getting used to, but the ultimate reference tostructures and definitions on your Course NotesAlan Dix 1996I/1 UNIXS ystemsProgrammingISession 1 unix basics the nature of unix the unix API system calls and library calls system call conventions how they work unix manuals and other infoUNIXS ystemsProgrammingIShort Course NotesAlan Dix 1996I/2 UNIXUNIX is an operating systemfile storeprogramsnetworks manages.
3 Files and data running programs networks and other resourcesIt is defined by its behaviour (on files etc.) its application programmers interface APIUNIXS ystemsProgrammingIShort Course NotesAlan Dix 1996I/3 unix API the system calls ultimately everything works through system callsshellfile storeprogramsnetworks Course NotesAlan Dix 1996I/4first system call exitvoid exit(int status) program ends! its exit code is set to status available to shell:$? Bourne shell$status C shell actually not a real system call!
4 Mdoes some tidying upmreal system call is _exit example:mdoes some tidying upmprogram :main(){exit(3);}mrun it:$ cc -o test-exit $ test-exit$ echo $?3$UNIXS ystemsProgrammingIShort Course NotesAlan Dix 1996I/5system calls and library calls system callsmexecuted by the operating systemmperform simple single operations library callsmexecuted in the user programmmay perform several tasksmmay call system calls distinction blursmoften a thin layermcompatability with older unix calls ( pipe) kinds of library:mUNIX functions layers and O/S utilitiesmstdio & ANSI C libraries platform independent functionsmother libraries for specialist purposes ( NAG)UNIXS ystemsProgrammingIShort Course NotesAlan Dix 1996I/6system call conventions library functions often return pointersFILE * fp = fopen("harry","r"); NULL return for failure system calls usually return an integerint res = sys_call(some_args) return valuemres >= 0 OKmres < 0 failure opposite way round!
5 Cannot use as Boolean:if ( sys_call(some_args) ) { ..8 wrong see the global variable errno for more info many system calls take simple arguments but some take special structuresUNIXS ystemsProgrammingIShort Course NotesAlan Dix 1996I/7how they work program gets to the system call in the user s codeint res = sys_call(some_parameters) puts the parameters on the stack performs a system trap hardware dependentP P now in system mode P P operating system code may copy large datastructures into system memory starts operationif the operation cannot be completed immediatelyUNIX may run other processes at this point operation complete!}
6 If necessary copies result data structures back touser program s memory P P return to user mode P P user program puts return code into res program recommences unix tries to make it as cheap and fast as possible but system calls are still expensive (compared to ordinary functions)UNIXS ystemsProgrammingIShort Course NotesAlan Dix 1996I/8finding out don t expect to remember everything.. I don t! even if you did versions differ places to look:mmanuals paper or using man command may need to give man the man 2 statmapropos especially to find the right man apropos directorymlook at the source!
7 Read the include file find the right include file:fgrep dirent /usr/include/*.hfgrep dirent /usr/include/sys/*.hUNIXS ystemsProgrammingIShort Course NotesAlan Dix 1996I/9 unix manuals divided into sections1 shell mv, ls, cat2 system _exit, read, write3 library calls (including stdio) exit, printf4 device and network specific mv, ls, cat5 file passwd, termcap6 games and fortune, worms7 troff macros, ascii character map8 admin fsck, network daemons unix manual reading .. a bit of an artUNIXS ystemsProgrammingIShort Course NotesAlan Dix 1996I/10 UNIXS ystemsProgrammingISession 2file I/O and filters standard input and output filters using read and write opening and closing files low-level I/O vs.
8 Stdio mixing them+using itUNIXS ystemsProgrammingIShort Course NotesAlan Dix 1996I/11input and outputeach running program has numbered input/outputs:0 standard input often used as input if no file is given default input from the user terminal1 standard output simple program's output goes here default output to the user terminal2 standard error error messages from user default output to the user terminalthese numbers are called file descriptors used by system calls to refer to files012inputoutputerrorsUNIXS ystemsProgrammingIShort Course NotesAlan Dix 1996I/12redirecting from the shelldefault input/output is user's terminalredirection to or from files.
9 Mcommand <fred standard input from file 'fred'012outputerrors'fred'mcommand >harry standard output goes to file 'harry'012inputerrors'harry' file is created if it doesn't C shell prevents overwritingmcommand >>harry similar, but appends to end of 'harry'UNIXS ystemsProgrammingIShort Course NotesAlan Dix 1996I/13redirection of standard errormcommand 2>errlog standard error goes to file 'errlog'012inputoutput'errlog'mcommand 2>>errlog standard error appends to end of 'errlog'mcommand 2>&1 standard error goes to currentdestination of standard output012inputoutputerrorsUNIXS ystemsProgrammingIShort Course NotesAlan Dix 1996I/14filters some commands only work on copying cp from-file to-file many take standard input as defaultcat, head, tail, cut, paste, etc.
10 These are called filters very useful as part of pipes also very easy to program!4don t have to worry aboutmcommand line argumentsmopening or closing files just read-process-writeUNIXS ystemsProgrammingIShort Course NotesAlan Dix 1996I/15read & writeret = read(fd,buff,len)int fd a file descriptor (int), open for readingchar *buff buffer in which to put charsint len maximum number of bytes to readint ret returns actual number read ret is 0 at end of file, negative for error buff is not NULL terminatedleave room if you need to add \0 !