200 likes | 316 Views
Systems Programming and UNIX. What is an OS? Operating Systems provide basic services to applications Hardware interface (disk drives, printers, terminals, etc.) CPU time, memory allocation and management Basic OS processes combined under heading ‘kernel’ Loads first
E N D
Systems Programming and UNIX • What is an OS? • Operating Systems provide basic services to applications • Hardware interface (disk drives, printers, terminals, etc.) • CPU time, memory allocation and management • Basic OS processes combined under heading ‘kernel’ • Loads first • Remains in main memory during system operation
What is UNIX? • What is UNIX? • Multiuser, Multitasking OS • Multiple processes share CPU time, ‘core’ memory • Systems functions provide interface to hardware control and process management
Logging In • Login establishes user environment and privileges • Password file or authentication database provides information to system on user
Standard Password File Info • Standard password file includes seven fields • username • encrypted password • user ID # • user group # • gecos • home directory • default shell
Shell operation • The login shell provides a command line interpreter for interaction with the system • Different login shells can be designated by the user • bash (default, extension to Bourne) • csh (patterned on C language) • ksh (similar to Bash) • zsh (similar to Bash • cat /etc/shells for list
File system • UNIX file system heirarchical (tree) • Beginning of tree is ‘root’, / • Forward slash designates new directory node (/etc/mail) • Filenames can include any character other than null and /, special chars must be quoted • Paths are absolute if begun with / (indicating root), relative to CWD ( current working directory) otherwise
File descriptors • File descriptors are numeric identifiers of files open to processes • Three standard FD’s (stdin, stdout, stderr) are opened by all shells • All three standard FD’s connect to terminal unless redirected
Buffered and Unbuffered IO • Unbuffered IO • Programmer managed buffers • open, read, write, lseek, close • Buffered IO • Function managed buffers • printf, scanf, putc, getc, etc.
Programs and Processes • A program is an executable file • A process is an instance of a program • Processes all identified by unique number • Three main process control functions • fork • Copy process • exec • Replace process • waitpid • Wait for process to exit
ANSI C • Function prototypes • Describe the structure of functions • Generic pointers (void) • Reduce type casting • Type primitives • Portable, abstracted definitions of types • /usr/include/sys/types.h
Error Handling • Functions typically return negative or null values to indicate errors • Many functions set global var errno to reflect conditions of failure • var errno can be mapped to string • strerror, perror • errno does NOT clear on no error
User and Group ID • Users are identified by unique numbers • superuser (root) identified as 0 • Has unlimited system control • processes are associated with user ID’s and share their privileges • real ID is inherited from the parent • effective ID is set by the file set ID bit • Groups are identified by unique numbers • Groups generally used for group file privileges
Signals • Signals are a means of interprocess communication • User processes can generate signals to other processes, if permissions allow • There are default ‘reactions’ to specific signals • Defaults can be overridden internally
Time Values • Calendar time • # of seconds since midnight, 1/1/70 • Coordinated Universal Time • Process time • Clock ticks (cpu time) • 50, 60, 100 ticks per second
Clock, User and System Time • clock time • process “real time” to run • user CPU time • process time used in instructions • system CPU time • process time used in kernel • user + system = CPU time
System Calls and Library Functions • System calls are entry points to kernel • In UNIX, implemented as C functions • Man section 2 describes • Library Functions may or may not invoke system calls • System related lib functions documented in section 3 • Applications can use either
Manual Tips • Manual pages exist for most system related functions • Section 2 of the manual is used for most system function descriptions • If multiple man entries correspond to a particular keyword, such as ‘read’, you can • use ‘man -a’ to print all entries, rather than just the first • use the ‘whatis’ command, as in ‘whatis read’ to determine what entries exist for the keyword
C Standards • ANSI C • American National Standards Institute • Posix • IEEE (Institute of Electrical and Electronics Engineers)
UNIX Implementations • SVR 4 • AT&T, Sun OS, and 4.3 BSD • POSIX compliant • Solaris major implementation • BSD • AT&T and Berkeley code • POSIX compliant • Available to public as NETBSD and derivatives
Write a program that will do the following: • Print to standard out the associated descriptive text of any given error number input as an argument, or a list of all possible error numbers and descriptions if given a particular flag • Input will be either an error number, or a flag as follows: • –help • print a usage statement to standard out • –all • print a full list of possible error numbers and their descriptions to standard out • Any input NOT within the range of valid error numbers will generate an error message (including a list of defined numbers), and the usage message to standard out. Your program will need to determine all defined error numbers. For purposes of this project, you need not check for definition beyond the range 0-1000.