注:图片插入杯具了,有时间再搞。
Chapter 1. UNIX System Overview
1.2 UNIX Architecture
Architecture of the UNIX operating system
1.3 Logging In
Login Name
Password file: /etc/passwd
It’s composed of seven colon-separated fields:
The login name, encrypted password, numeric user ID(205), numeric group ID(105), a comment field, home directory(/home/sar), and shell program(/bin/ksh).
sar:x:205:105:Stephen Rago:/home/sar:/bin/ksh
Shells
A shell is a command-line interpreter that reads user input and executes commands.
1.4 Files and Directories
File System
The UNIX file system is a hierarchical arrangement of directories and files.
Filename
The only two characters that cannot appear in a filename are the slash character(/) and the null character. The slash separates the filenames that form a pathname and the null character terminates a pathname
Two filenames are automatically created whenever a new directory is created: . (called dot) and .. (called dot-dot). Dot refers to the current directory, and dot-dot refers to the parent directory. In the root directory, dot-dot is the same as dot.
Pathname
A pathname that begins with a slash is called an absolute pathname; otherwise, it’s called a relative pathname.
Working Directory
Every process has a working directory, sometimes called the current working directory. A process can change its working directory with chdir function.
Home Directory
Our home directory is obtained from our entry in the password file.
1.5 Input and Output
File Descriptors
File descriptors are normally small non-negative integers that the kernel uses to identify the files being accessed by a particular process.
Standard Input, Standard Output, and Standard Error
By convention, all shells open three descriptors whenever a new
program is run: standard input, standard output, and standard error.
Unbuffered I/O
Unbuffered I/O is provided by the functions open, read, write, lseek, and close. These functions all work with file descriptors.
Standard I/O
The standard I/O functions provide a buffered interface to the unbuffered I/O functions.
1.6 Programs and Processes
Program
A program is an executable file residing on disk in a directory. A program is read into memory and is executed by the kernel as a result of one of the six exec functions.
processes and Process ID
An executing instance of a program is called a progress. The UNIX System guarantees that every process has a unique numeric identifier called the process ID. The process ID is always a non-negative integer.
Process Control
there are three primary functions for process control: fork, exec, and waitpid.
Threads and Thread IDs
All the threads within a process share the same address space, file descriptors, stacks, and process-related attributes. Because they can access the same memory, the threads need to synchronize access to shared data among themselves to avoid inconsistencies.
1.7 Error Handling
There are two rules to be aware of with respect to errno. First, its value is never cleared by a routine if an error does not occur. Therefore, We should examine its value only when the return value from a function indicates that an error occurred. Second, the value of errno is never set to 0 by any of the functions, and none of the constants in <errno.h> has a value of 0.
Two functions are defined by the C standard to help with printing error messages.
#include <string.h>
char *strerror(int errnum);
This function maps errnum, which is typically the errno value, into an error message stirng and returns a pointer to the string.
The perror function produces an error message on the standard error, based on the current value of erron, and returns.
#include <stdio.h>
void perror(const char *msg);
`It outputs the string pointed to bu msg, followed br a colon and a space, followed by the error message corresponding to the value of errno, followed by a newline.
Error Recovery
1.8 User Identification
User ID
The user ID from our entry in the password file is a numeric value that identifies us to the system. This user ID is assigned by the system administrator when our login name is assigned, and we cannot change it. The user ID is normally assigned to be unique for every user.
We call the user whose user ID is 0 either root or the superuser.
Group ID
Groups are normally used to collect users together into projects or departments. This allows the sharing of resources, such as files, among members of the same group.
There is also a group file that maps group names into numeric group IDs. The group file is usually /etc/group.
The ls –l command prints the login name of the owner of a file, using the password file to map the numeric user ID into the corresponding login name.
1.9 Signals
Signals are a technique used to notify a process that some condition has occurred.
The process has three choices for dealing with the signal.
1. Ignore the signal.
2. Let the default action occur.
3. Provide a function that is called when the signal occurs.
1.10 Time Values
UNIX systems have maintained two different time values:
1. Calendar time.
2. Process time.
1.11 System Calls and Library Functions
Difference between C library functions and system calls