+ CMPS 105 Systems Programming Prof. Darrell Long E2.371 darrell@ucsc.edu
+ Chapter 1
+ Introduction n Operating systems provide services for programs n Execute a program, open a file, read a file, allocate memory, get time of day, etc. n Most programming languages provide higher-level abstractions for these services n It can be almost impossible to write efficient programs at that level n This class will focus on programming right on top of the operating system n The operating system runs on bare metal (silicon) n Goals: Efficient, powerful programs that leverage the power of the operating system
+ Why Unix? n Unix is widely used and freely available n Linux, Android, FreeBSD, netbsd, OpenBSD, System V, Solaris, Mac OS, ios, etc. n Unix was designed for programmers by programmers n Low level Windows programming is a study in pain and suffering n Once you understand one system in detail, it is easy to learn others
+ Unix Operating System Structure
+ Logging In n Enter Username and Password n Username identifies you to the computer n Why does it care who you are? n Different users name different roles and priviledges n Password proves you are who you say you are n Why does it need proof? n Do you want people messing with your stuff? n Is this adequate proof? n It could be that someone stole your password! n Computer Security is a complex and fascinating topic, and we will discuss it in some detail this quarter.
+ The Shell n A shell is a program that: n Accepts inputs from the user n Runs/manages programs for the user n Supports limited programming (called shell scripting) n Shells: sh, csh, ksh, bash, tcsh, zsh, n We may write a shell later in the quarter as one of our projects.
+ Files and Directories n Files provide non-volatile data storage n What you write to a file stays there until you delete it n Files have names n That is how you refer to them, find them, etc. n Files are contained in directories (Windows calls them folders) n Directories contain files and other directories n Directories form a hierarchy n There is a root directory called / n Special files:. (this) and.. (parent)
+ Directories and Paths n Pathnames n Absolute pathname /a/b/c n Relative pathname a/b/c relative to the current working directory n Every process has a working directory n Called the current working directory n Home directory n This is where you start out in when you log in
+ Directory Trees
+ Listing a Directory (a simple version of ls)
+ Input and Output (I/O) n File descriptors n Small integers the kernel uses to identify open files in a process n Actually an index into a table maintained by the kernel n Standard Input (0), Output (1), and Error (2) n Default file descriptors for scanf, printf, etc. n Can be redirected n Unbuffered I/O n Default for open, read, write, lseek, and close
+ Copy Standard In to Standard Out
+ Copy using <stdio.h>
+ Programs and Processes n Program n An executable file n Process n A running program n Process ID n An identifier for a running program n Process control n fork, exec, wait
+ Read and Execute Commands (the core of a shell)
+ ANSI C Features n Function prototypes n <unistd.h> n Probably in /usr/include/unistd.h n Depends on the version of Unix There s more than one? n Generic pointers (not important) n Primitive System Data Types n End in _t (as in pid_t) n Defined in <sys/types.h>
+ Error Handling n Unix system calls and library functions return negative number to indicate an error (usually) n errno contains addition information n Defined in <errno.h>
+ User Identification n User ID n Unique identifier for each user that can use the computer n Why do we need this? n To keep other people from messing with your stuff! n Group ID n Unique identifier for the group the user is in (every process has a group associated with it) n Useful for sharing information
+ Signals n Signals allow processes to communicate (but just barely: a small integer only) n When a process receives a signal, it can n Ignore it n Let the default action occur n Handle it with a specified function (a signal handler) n You must own a process to send it a signal
+ A (slightly) better shell
+ Time n Calendar time n Stored as time_t n Recorded as the number of seconds since January 1, 1970 n The end of Unix time is 03:14:07 UTC on Tuesday, 19 January 2038 n Process time n The amount of time used by a process n Clock time, user CPU time, system CPU time
+ System Calls versus Library Functions n What s the difference? n System calls n Access points to call operating system functions n Not direct function calls n Limited number of well-defined functions n For details, take CMPS 111 n Library functions n A set of useful functions n May or may not invoke system calls
+ Example: malloc and sbrk
+ Accessing System Features