Chapter 1 U N i x Basic Knowledge
1.1 introduction
All operating systems provide services to programs they run. Typical services have new programs, open files, read text
Parts, allocate storage area, get current time, etc., this book focuses on the services provided by various versions of the U N i x operating system.
In the way, U N i X is almost impossible in terms of the terminology that has not been explained in a strict stepper method.
It will also be bored). This chapter quickly browses U N i x from the perspective of program designers, and some terms referenced in the book
And concepts are briefly explained and examples are given. These concepts will be described in more detail in the following chapters. This chapter is also
User designers who are not familiar with U N i X briefly introduce the various services provided by U N i x.
1.2 login
1.2.1 login name
When you log in to the U n i x system, type the login name and type the port. The system is in its password file, usually
/ e t C / P A S W D file View login name. The login item in the password file consists of 7 colonally separated fields: login name,
Encryption Pass, Digital User I D (2 2 4), Number Group I D (2 0), Comment Field, Start Directory (/ H O M E / S TET E V E N S), and S H E L L
Program (/ B i n / k s h).
Many comparison new systems have moved the encrypted port to another file. Chapter 6 will explain this file and access them
The function.
1.2.2 shell
After logging in, the system first displays some typical system information, and then you can type commands to the S H E L. S h e l L is a
Command line interpreter, it reads the user input, then executes the command, the user usually uses the terminal, sometimes through the file (called
SH E L L scripts) Enter the S H E L1. Common S h e l Ls:
• Bourne Shell, / Bin / SH
• c shell, / bin / csh
• Kornshell, / Bin / KSH
The system learned from the last field of the login item in the password file to which one should be executed which S h E L L should be executed.
Since V7, Bourne Shell has been widely used, and almost every existing U N i x system provides Bourne Shell.
C shell is developed in Berkeley, and all B S D versions provide this S H E L1. In addition, AT & T system V / 386 R3.2 and
S V R 4 also provides C shell (the next chapter will make more descriptions for these different U N i x versions). K o R n s h e l L is a Bourne Shell
The successor provided by S V R 4. K O R N S H E L1 runs on most U N i x systems, but usually it needs before S V R 4
It is necessary to purchase separately, so there is no other two S H E L1 popular.
This book will use a lot of S H E L L example to perform the developed program, which will apply Bourne Shell and K O R N S H E L L
Have functions.
Bourne Shell is developed in the Bell Lab, and its control flow structure remembers
Algol 68. C shell is done in Berkeley by Bill Joy, its foundation is the 6th version of S H E L L (not B O U R n E
S h e l. Its control structure is very like C language, which supports some features that bourne shells are not available, such as homework
Control, historical mechanism and command line editing. K O R N S H E L L is developed in the Bell Laboratory,
It is compatible with Bourne Shell and also contains some features that make C shell very popular, such as job control, command line compilation, etc.
This book will use this form of annotation to describe history and compare different U N i x implementations. when
We learned that after the history of history, we will become clarified by using certain specific implementation techniques.
1.3 files and directories
1.3.1 file system
U N i x file system is a hierarchical arrangement of directories and files. The starting point of the directory is called root (R o O t), and its name is a
character/.
Directory (D i r e c t o R y) is a file containing directory items, logically, can consider that each directory item contains one
The file name, which also contains information that describes the file attribute. The file attribute is: file type, file length, file owner,
The permission of the file (for example, whether other users can access the file), the final modification time, etc. S t a t and f s t a t function
Returns an information structure containing all file properties. Chapter 4 will detail the various properties of the files.
1.3.2 file name
The various names in the directory are called the file name (f i l e n a m e). There are only two characters in the file name, oblique (/)
And empty operator (N u L). The slash separated the file name (in the following description), the empty operator terminates a path
name. Despite this, good habits are only a subset of printed characters as a file name character (only the reason for using the subset is:
If some S H E L L special characters are used in the file name, you must use the quotation mechanism of S H E L1 to reference the file name).
When creating a new directory, two file names are created:. (Called point) and.. (Called point-point). Code quote
Record, point - point reference parent directory. In the highest level root directory, the point-point is the same as the point.
Some U N i x file system limit file names have a maximum length of 1 4 characters, and the B S D version is extended to 2 5 5
Character.
1.3.3 path name
0 or more file name sequences separated by oblique lines (may optionally be started with a slash) (p A t h n a m e),
The path name at the beginning of the slash is an absolute path name, otherwise it is called a relative path name (R e l A T i v e
P a t h n m e).
Example
It is not difficult to list the names of all files in a directory, and the programs 1 - 1 are the main implementation of the L s (1) command.
Program 1-1 lists all files in a directory
2 u n i x Environment Advanced Programming
download
L s (1) This representation is the usual method of u n i x to reference a specific item in the U N i X manual. It references the first
Part of the L s in part, each portion typically uses numbers 1 to 8, and each part is arranged in alphabetical order. assumed
You have a manual for the U N i x system used.
Early U N i x system puts 8 parts in a manual, and now the trend is to put these parts.
Arranged in different manuals separately: there is a user-specific manual, programmer-specific manual, system administrator dedicated
Handbook, etc..
Some U N i x systems further divide a list of homes in a given section further into a number of uppercase letters
A small portion, for example, all standard I / O functions in AT & T [1 9 9 9 0 E] are indicated in the 3 S section, for example
F o p e n (3 s).
Some U N i x systems, such as XE N i X-based systems, not using numbers to divide manual into several
Part, but use C to represent commands (Part 1), and s represents the service (usually the second, 3), and the like.
If you have an online manual, you can use the following command to view the L S command man page:
Man 1 LS
Program 1 - 1 Print only the name of each file in a directory, do not display additional information, if the source file name is M y L s. C,
The following command can be compiled, the result of the compiled result is sent to the system default name a. O U t's executable file name:
CC MYLS.C
Some sample output is:
$ a.out / dev
.
.
M a k e d e v
C O N S O L E
T t y
M e m
K m e m
N u L
This is slightly multi-line
Chapter 1 U N i X Basics 3
download
P R i n t e r
$ a.out / var / spool / mqueue
Can't open / var / spool / mqueue: permission denied
$ a.out / dev / tty
Can't open / dev / Tty: NOT A DIRECTORY
This book will indicate the input command in this way and its output: The input character is represented by bold, and the program output is another.
A font representation. If you want to add an comment to the output, the Chinese Song is indicated by the Chinese Song, and the previous dollar sign ($) is S H E L L
The printed prompt, this book shows the S H E L promise as $.
Note that the listings listed are not arranged in alphabetical order, and the L s command generally lists the directory items in alphabetical order.
In these 20 rows of procedures, there are many details to consider:
• First, in which one header file O U R H D r. H is included. Almost every program in this book contains this header file. It contains
Some standard system header files define many constants and function prototypes, which will be used in various instances of this book, appendix
B lists the common header file.
• The main function describes the new style supported by the ANSI C standard (the next chapter will more explain the ANSI C).
• The first parameter A RG V [1] of the command line is used as the listed directory name. Chapter 7 will explain how the m a i n function is called,
How can I access command line arguments and environment variables.
• Because the actual format of the directory items of various U N i x systems is different, use the function o p e n d I r, readdir
And C L O S e d I r processes the directory.
• The OpenDir function returns a pointer to the D i R structure and pass the pointer to the R e a D D I r function. We don't care about D i r
What is included in the structure. Then, RE A D I r is called in the loop to read each directory item. It returns a point to D i r e n t
The pointer of the structure, and the N u L pointer is returned when the directory item is readable. Examples taken in the D i r e n t structure
The name of the catalog item (D_ N a M e). With this name, you can call the S t a t function (see 4.2) to determine all the files.
Attributes.
• Two self-compiled functions are called to handle errors: E R R _ S Y S and E R R _ Q u i t. It can be seen from the output from above.
E R r_ S Y S function prints a message ("Permission Denied" or "Not A Directory"
Record) ")"), indicating what type of error encountered. These two error handages are described in Appendix B, 1. Section 7 will more narrative
Mroutive process. These two error handling functions illustrate in Appendix B1. Section 7 will narrate more detail.
• When the program will end, it calls the function e x i t in parameter 0. Function E x I t terminates the program. According to the convention, parameter 0 means that the end is ended, and the parameter value 1 to 2 5 5 indicates an error. 8. 5 Section will explain a program (such as S H E L L or the program we have written)
How to get the e x i t state of another program it execute.
1.3.4 Work Contents
Each process has a working directory (Working Directory, sometimes called the current working directory (Current Working)
D i r e c t o r y)). All relative pathnames begin to explain from the working directory. The process can change its working directory with the C H D I r function.
For example, relative path name D o c / m e m o / j o E refers to file J o E, which in directory M e m o, and M e m O is in directory D o C
In D O c should be a directory item in the working directory. It can be seen from this path name, and D O C and M E M O should be a directory.
But it is unclear that J O E is a file or a directory. Path Name / U R S / L i B / L i N t is an absolute path name, which refers to a file (or purpose)
Recording) LINT, and L i n t in directory L i B, L i b, in the directory U S R, U S R is in the root directory.
1.3.5 start directory
When logging in, the work directory is set to the Home Directory, which is from the password file (see 1. 2)
4 u n i x Environment Advanced Programming
download
The login item is obtained.
1.4 input and output
1.4.1 file descriptor
The text descriptor is a small non-negative integer, the kernel to identify a file that a particular process is being deposited. Kernel
Open an existing file or create a new file, it returns a file descriptor. When reading, write the file, you can make
Use it.
1.4.2 Standard input, standard output and standard error
Press convention, whenever you run a new program, all S H E L L opens three file descriptors for it: standard input, standard
Quasi-output and standard error. If there is no special treatment like a simple command L S, these three descriptors are connected to the end
end. Most S H E L1 provide a method that allows any one or all three descriptors to realligate to a file,
E.g:
LS> file.list
Execute the L s command, the standard output is reached on the file named f i l e. L i s t.
1.4.3 I / O without cache
Functions O P E n, R E A D, W R I T E, L S E E K, and C L O S e provide I / O without cache. These functions use file descriptors
working.
Example
If you are willing to read from the standard, you will be written to the standard output, the program 1 - 2 can be used to copy any U N i x file.
Program 1-2 copy standard input to standard output
Header file (O U R H D r. H contains this header file) and two constant S t D i n _ f i l e n o and S T D O u T _
F i l E N o is part of the P O S i X standard (the next chapter will make more descriptions). Many U N i x system services function
Type, for example, RE A D and W R I t e we call are in this header file. Function prototype is also part of the ANSI C standard, this chapter
Chapter 1 U N i X Basics 5
download
This will be more described later.
Two constant S t D i n _ f i l E N O and S T D O U t _ f i l E N o defines in the header file, they specify the standard
Quasi-input and standard output file descriptors. They typically are 0 and 1, but for portability, we will use these new names.
3. Section 9 will discuss the B U f F S I z E constant, indicating how a variety of different values will affect the efficiency of the program. But regardless of this
What is the value of the constant, this program always copies any U N i x file.
The R e a d function returns the number of bytes read, which is used as the number of bytes to be written. When the end of the file is reached, R e a d returns 0,
The program stops execution. If a read error occurs, R e a d returns -1. Most system functions return -1 when an error occurs.
If you compile the program, the result is sent to the standard A. O U T file, and execute it in the following:
a.out> data
Then, the standard input is the terminal, and the standard output is redirected to the file D A t a, and the standard error is also the terminal. If this output
The part does not exist, then S H E L is created it.
Chapter 3 will explain the I / O function that does not have cached in more detail.
1.4.4 Standard I / O
The standard I / O function provides an interface with a cache that does not have the cached I / O function. Use standard I / O, you don't have to worry about how
Select the best cache length, such as the B u f f s i z e constant in programs 1 - 2. Another advantage and place using standard I / O functions
Treat input line (often in the application of U N i x). For example, the F g e t s function reads a complete line, and on the other hand, R e a d
Function reads specified bytes.
Our most familiar standard I / O function is P R i n t f. In the program that calls P R i n t f, it always includes (usually included
O U R H D r. h in the H, because this header includes prototypes for all standard I / O functions.
Example
The function of the program 1 - 3 is similar to the previous programs 1 - 2, 5. 8 sections will be more detailed in the program 1 - 3.
Bright. It copies the standard input to standard output, so you can copy any U N i x file.
Program 1-3 uses standard I / O to copy standard input to standard output
Function g e t C reads 1 characters at a time, then P U t C write this character to the standard output. When you read the last one byte of the input,
G e t C returns constant E o F. Standard input, output constant S t D i n and S t D O U T define in header file , they respectively
Standard input and standard output files.
6 u N i x Environment Advanced Programming
download
1.5 procedures and processes
1.5.1 program
Program (P R O G R A M) is an executable file stored in a disk file. One of the 6 e x e c functions will be used by the kernel
The program reads into the memory and executes it. 8. 9 section will explain these e x e c functions.
1.5.2 Process and Process I D
The execution of the program is called a process (P R O C E S S). This term is almost all of this book. Certain operation
The system uses tasks to represent the program being executed.
Each u n i x process must have a unique numeric identifier called process I d (Process ID). Process I d is always
One non-negative integer.
Example
Program 1 - 4 for printing processes I D.
Program 1-4 Print Process I D
If you want to compile the program, the result is sent to a. O U t file, then execute it, then:
$ a. o U t
Hello World from Process ID 851
$ a. o U t
Hello World from Precess ID 854
When this program runs, it calls the function g e t p i d to get its process I D. 1.5.3 process control
There are three major functions for process control: F O r k, e x e c and W A I t p i d (e x e c function has six variants, but often
They are collectively referred to as e x e c functions).
Example
Program 1-5 Enter the command from the standard and execute
Chapter 1 U N i X Basics 7
download
U N i x Process control function can be explained with a simpler program (see programs 1 - 5), the program is read from the standard input
Command, then execute these commands. This is a basic implementation section similar to the S H E L program. In this 300 line program,
There are many features need to think:
• Use the standard I / O function F g e t s to read a line from the standard, when you type file end characters (usually C T r L - D) as
When the first character of the row, F g e t s returns a N u L pointer, so the cycle is terminated, the process is terminated. Chapter 11 will explain all
Special Terminal characters (file end, roster characters, deepel erase, etc.), and how to change them.
• Because each row returned by F G e t s is terminated with a new line, then calculate this with the standard C function S t r l e n with a standard C function S t r l e n.
The length of the string is then replaced with a N u L byte. The purpose of this operation is because the E x E C L P function is required.
The parameters ended with N u L1 rather than the parameters ending with the new line.
• Call F o R K Create a new process. The new process is a replica called the process, so that the modification process is the parent process, new creation
The process of built is a child process. F O R K returns the parent process to the new sub-process for the new sub-process, and the sub-process returns 0. Because F o R k
Built a new process, so it is called once (by the parent process), but returns twice (in the parent process and in the child process).
• In the child process, the E x E C L p is called to execute the command read from the standard input. This is replaced with the new program file.
process. F o r k and the combination following the subsequent E x E C are called a new process in some operating systems. In u N i x, these two
Part is divided into two functions. Chapter 8 will make more of these functions.
• The child process calls e x e c lp to execute a new program file, and the parent process hopes to wait for the sub-process to terminate, this requirement is called
W A I T P i D implementation, its parameter specifies the process to wait (here, the P i D parameter is the child process I d). W a i t p i d function also returns a child
The termination state of the process (S t a t u s variable). This is not used in this simple program. Use this value to be accurate if needed
Determine how the child process is terminated.
• The most important limit for this program is that the parameters cannot be passed to the executed command. For example, you can't specify the list of directory names, you can only
Execute the L s command for the working directory. In order to pass parameters, first analyze the input line, then separate the parameters with some agreement (very likely
Use spaces or tabs), and then pass the individual parameters to the E x E C L P function. Despite this, this procedure can still be used
Description U N i X process control function.
If you run this program, get the following results. Note that the program uses a different prompt (%).
8 u n i x Environment Advanced Programming
download
$ a. o U t
% D A t e
Fri Jun 7 15:50:36 MST 1991
% W H o
Stevens console jun 5 06:01
Stevens TTYP0 JUN 06:02
% P w D
/ h o m E / S T E V E N S / D O C / A P U E / P R O C% L S
M a k e f i l e
a. o U t
S h e l l. c
% D Type file end character
$ Output Conventional S H E L L prompt
1.6 ANSI C
All instances in this book are written in ANSI C.
1.6.1 function prototype
Header file contains a number of function prototypes for many U N i x system services, such as RE A D, W R I T E, and W R I T E,
G e t p i d function. Function original is an integral part of the ANSI C standard. These function prototypes are as follows:
SSIZE_T READ (int, void *, size_t);
SSIZE_T WRITE (INT, Const Void *, SIZE_T);
PID_T getpid (void);
The last thing means: g e t P i D does not have a parameter (V O I d), and the data type of the return value is P i d_ t. Provided these functions
After the type, the compiler can check if the correct parameter is used when the call function is used. In the program 1 - 4, such as
If the g e t p i d (such as G E T P i d (1)) with parameters, the ANSI C editor will give the following forms of error information:
Line 8: Too Many Arguments to Function "getpid"
In addition, because the compiler knows the data type of the parameter, if it is possible, it will force the parameters to convert into the required
Data type.
1.6.2 class pointer
From the function prototype shown above, it can notice another difference: RE A D and W R I t e's second parameter is now VOID *
Types of. All early U N i x systems use char * this pointer type. The reason for this change is: ANSI C.
Void * is used instead of CHAR * as a class pointer.
The combination of function origin and class pointer eliminates the explicit type forced conversion of many non-ANSI C editing programs.
For example, after the W R I t e prototype is given:
FLOAT DATA [1 0 0];
Write (FD, DATA, SIZEOF (DATA);
If you use a non-A n S i compiler, or no function prototype is given:
Write (FD, (Void *) Data, Sizeof (DATA));
Void * pointer can also be used for M a L O C function (see Section 7 8). The prototype of M a L L o C is:
Chapter 1 U N i x Basics 9
download
Void * malloc (size_t);
This makes it possible to have the following block:
INT * PTR;
PTR = Malloc (1000 * sizeof (int));
It does not need to convert the returned pointer to an int * type.
1.6.3 Original system data type
The prototype of the g e t p i d in the previously shown defines its return value of P i D_ T type, which is also a new specified in P O S i x.
U N i x Early versions specify that this function returns an integer. Similarly, R E A D and W R I TE E return to the value of the type S s I z e _ T, and
The type of third parameter is required is S i z e_ t.
These data types ended in _ T are called the original system data type. They usually in the header file
Righteousness (header ) should already include the header file). They are usually defined in CTYPEDEF. T y p e d e f Illustrated in C
The language has exceeded for more than 1 5 years (so this does not require ANSI C), and its purpose is to prevent the program from using specialized data types (such as int, short or long) to allow each implementation of a particular system. The required data type. In need storage
The place where the process I d, the allocation type is a variable of P i D_ T (note, the program 1 - 5 has made the variable named P i D). in
A variety of different implementations, this definition of this data type may be different, but this difference now only appears in a header
Part in the part. We only need to re-edit the app on another system.
1.7 error handling
When the u n i x function is wrong, a negative value is returned, and the integer variable e r R N O is usually set to have a specific information.
Value. For example, the O P e n function returns a non-negative file descriptor such as an error, if an error is returned. Error in O P e n
At about 1 5 different E r R N o (files do not exist, permission issues, etc.). Some functions do not return negative values but use
Another agreement. For example, returning most of the functionality of a pointer to an object, and a NULL pointer will be returned when an error.
The variable E R R N O and various constants that can be assigned to it can be assigned to file
U N i x Manual Page 1 of Part 2, INTRO (2) lists all of these error constants. For example, if e r R N o is equal to constant
E a c c e s, which represents an authority issue (for example, without opening the required file). P O S i X definition E R R N o is:
EXTERN INT errno;
P O S i x. 1 The definition of E R R N O is more demanding than the definition of the C standard. The C standard allows E R R N o to be a macro,
It expands to modify the constant left value (such as a function of returning a pointer to the wrong number).
Two rules should be known for E R R N O. The first rule is: If there is no error, its value will not be cleared by one routine.
Therefore, only when the return value of the function indicates an error, the value is inspected. The second is: any function does not set the E r R N o
It is set to 0, and all constants defined in
The C standard defines two functions that help print the error message.
#include
CHAR * STRERROR (Int E RR N u m);
Returns: Pointer to the message string
This function maps E RR N u m (which is usually E R R N o) into an error information string and returns the pointer to this string.
1 0 u n i x Environment Advanced Programming
download
The P e r R O R function generates an error message on the standard error (based on the current value of e r R N O) and then returns.
#include
Void Perror (const char * m s g);
It first outputs the string pointing by M s G, then a colon, a space, then corresponding to the E r R N o value
Wrong information, then a new line.
Example
Program 1 - 6 shows how the two error functions are used.
Program 1-6 Example S T R E R R O R and P E R R OR
If this program is compiled, the result is sent to the file a. O U t, there is:
$ a. o U t
Eacces: permission denied
A.out: No Such File or Directory
Note that we will pass the program name (A RG V [0], which is a.out) as a parameter to P E R R OR. This is a standard U N i x practice. Using this method, if the program is performed as part of the pipeline, such as:
PROG1
Then we can distinguish from which one of the three programs produces a specific error message.
All instances in this book basically call S T R E R R r or P E R R O R, but use the error function in Appendix B. Should
The error function in the appendix uses the variable parameter table facility of ANSI C, and the error condition can be handled with a C statement.
1.8 user ID
1.8.1 User I D
The user I d (User ID) in the password file login is a value, which identifies different users to the system. System administrator
While determining the login name of a user, determine its user I D. Users cannot change their users I D. Usually there is one for each user
Unique user i D. Here will be described below how the kernel uses user I d to verify that the user has appropriate permissions to perform some operations.
The user I d is 0 is 0 (R O O T) or a super user (S u p e r u s e r). In the password file, there is usually a login.
It logs in to R o O t, which we call this user privilege as super user privileges. We will see in Chapter 4, if one
The process has superuser privileges, and most file permission checks are no longer done. Some operating system functions are limited to super
Chapter 1 U N i X Basics 1 1
download
Level users provide, super users have free dominance for the system.
Example
Program 1 - 7 is used to print user I d and group I d (in the following description).
Program 1-7 Print User I D and Group I D
Call g e t u i d and g e t g i d to return user I d and group i D. Run the program, produce:
$ a. o U t
UID = 224, GID = 20
1.8.2 group I d
The password file login also includes the user's group I d (Group ID), which is also a value. Group I D is also managed by system management
Members assigned when determining the user login name. In general, there are multiple record items in the password file with the same group I D. in
U N i x, the group is used to set several users to the topic or department. This mechanism allows a total of various members of the same group
Enjoy resources (such as files). 4. 5 Sections will explain that the license to set the file has enabled all members in the group to access the file, and the group
Outer users cannot.
Group files map the group name into numeric group I d, which is usually / e T C / G R O u P.
For permissions use value users I D and numerical group I d are formed in history. The directory item of each file in the system contains the
File owner's user I d and group i D. To store these two values in the catalog item (assuming each of each byte
Set value storage). If you use the 8-byte login name and the 8-byte group name, there is a lot of disk space. But for the user
Words, the name of the name is convenient than the value, so the password file contains the mapping relationship between the login name and the user I d, and the group is
The piece contains the mapping relationship between group name and group I d. For example, UNIX LS-L command uses a password file to map numerical users I D
For the login name, printed the login name of the file owner.
1.8.3 Add group I d
In addition to specifying a group I D in the password file, some U N i x version also allows one user to belong to another
Some groups. This is from 4.2 BSD, which allows a user to belong to up to 1 6 additional groups. Read the file when logging in
/ E T C / G R O u P, looking for the first 1 6 registration items of the user as its member to get the addition group I D
(Supplementary Group ID). 1.9 signal
Information is a technique that notices a certain condition has occurred. For example, if a process is performed, its divisor is
0, send a signal named I g f P E to this process. How to handle signals with three options:
(1) Ignore the signal. Some signals represent hardware abnormalities, for example, in addition to units other than 0 or access process address space, etc.
1 2 U N i x Environment Advanced Programming
download
This processing method is not recommended because the consequences of these exceptions are uncertain.
(2) Process it by default by system. For 0 divisions, the system default is to terminate the process.
(3) Provide a function that calls the function when the signal occurs. Using this way, we will know when
Birth signal and handle it according to the desired way.
Many conditions generate signals. There are two keyboard methods, called interrupt keys (Interrupt Key, usually D e l e t e-key
Or C T r L - C) and exit key (Quit Key, usually C T r L - /), which are used to interrupt the current running process. Another production signal
The method is to call a function named k i l L. To call this function in a process, you can send a signal to another process. when
There is also some restrictions on this: When sending a signal to a process, we must be the owner of the process.
Example
Recall the basic S H E L program (see programs 1 - 5). If this program is called, then type the interrupt key, do this
The process is terminated. The reason for producing this consequence is: the default action for this signal (S i g i n t) is to terminate this process. This
The process did not tell the system to process this signal, so the system will terminate the process by default.
In order to change this program, it can capture the signal, it needs to call the S i g n a l function, specify when the S i g i n t signal is generated
The function name to be called. Therefore, a function called S i g _ I n t is written. When it is called, it just prints a message, then
Print a new prompt. In the program 1 - 5, 1 2 lines constitute procedures 1 - 8 (added 1 2 lines to the first sign indication).
Program 1-8 Enter the command from the standard and execute
Chapter 1 U N i X Basics 1 3
download
Because most important applications will use the signal, the signal will be described in detail in Chapter 1 0.
1.10 UNIX time value
For a long time, U N i x system has always used two different time values:
(1) Calendar time. This value is tired of the number of seconds passing by the international standard time (U t c) since January 1, 970 0: 0: 0: 0:0 0
Value (earlier manual, U T C is the GMT). These time values can be used to record files for the most recent modifications
Time, etc.
(2) Process time. This is also known as C P U time to measure the central processing machine resources used by the process. Process time
The clock is calculated for many years, and it takes 5 0,6 0 or 1 0 0 to 02. System basic data type C L o c k _ t Save this
Time value. In addition, P O s i x defines constant C L K _ T C K, and it will illustrate the number of ticks per second. (Constant C L K _ T c k now no
reuse. 2. Section 4 will show how to get the number of clock ticks per second with the S Y s c o n F. )
When a measure of a process is executed (see 3. 9), the U N i x system uses three processes time values:
• Clock time.
• User C P U time.
• System C P U time.
Clock time is also known as wall clock time (Wall Clock Time). It is the time total amount of process running, and its value is related to the number of processs running simultaneously in the system. When we report clock time, it is measured when there is no other activity in the system.
User C P U time is the amount of time used to perform user instructions. System C P U time is to perform the kernel for the process
time. For example, as long as one process executes a system service, such as R E A D or W R I T, the service is performed within the kernel.
The time spent is included in the system C P U time. User C P U time and system C P U time is often called
C P U time.
To get the clock time of any process, the user time and system time is easy - as long as the command t i m e (1),
The number is to measure the command of its execution time, for example:
$ cd / usr / include
$ TIME GREP _POSIX_SOURCE * / *. H> / dev / null
Real 0m19.81s
User 0m0.43s
SYS 0M4.53S
The output format of the T i m e command is related to the SHE L1 used.
8. 1 5 Section will explain how a running process gets these three times. See 6. 9 section on time and date.
1. 11 System call and library function
All operating systems provide a variety of service entry points, which serve the kernel request. Various versions of U N i x
Both provide a limited number of entry points that have been well defined, and through these entry points into the kernel, these entry points are called system adjustment
System Call. System call is a U N i x feature that cannot be changed. U N i x 7th edition provides approximately 5 0 system calls,
4. 3 B S D provides approximately 110, while S V R 4 provides approximately 1 2 0.
1 4 u N i x Environment Advanced Programming
download
The system call interface is always explained in Part 2 of the U N i X Programmer Manual. Its definitions are also included in the C language.
This is different from many early operating systems that define kernel entry points in the traditional manner in the assembly language of the machine.
U N i x The technique used is to set a function with the same name to each system call in a standard C library. User into
The standard C call sequence calls these functions, and then the function is called to call the corresponding kernel service with the technology required by the system.
For example, a function can send one or more C parameters to a universal register, and then perform a machine finger generating soft interrupt into the kernel.
make. From the application perspective, the system call can be considered as a C function.
Part 3 of the U N i X Programmer Manual defines the general function that the programmer can use. Although these functions may
Calling a system call for one or more kernels, but they are not the entry point of the kernel. For example, the P R I n t f function is called
W R I T E system tuning for output operation, but function S t R C P y (copy one string) and A t o i (transform A S c i i is an integer)
Any system call.
From the executor's perspective, the system calls and library functions
There is a major difference, but from the perspective of the user, the difference is not very
important. In this book, the system call and the library function are shaped in c functions.
The array, both provide services, but we
It should be understood that if you want, we can replace the library function,
But usually cannot replace system calls.
Take the memory allocation function m a L o c as an example. There are many ways
Collection operations for memory allocation and useless areas related to them (most
Good adaptation, first adaptation, etc.), there is no best to all programs
One technology. U N i x system call processing memory allocation is
S B R K (2), it is not a universal memory manager. It increases
Or reduce the process address space of the specified byte number. How to manage the ground
The site space depends on the process. Memory Allocation Function M A L O C (3) implements a specific type of allocation. If we don't like its operator
, You can define your own M a L O C function, it may use
S B R k system call. In fact, there are many packages that they are implemented.
Your own memory allocation algorithm, but still calls S B R K system.
Figure 1 - 1 shows the application, M a L O C function and S B R K system adjustment
The relationship between use.
It can be seen from it, the duties are different, and the system calls in the kernel are allocated to the process, and the library
The function M a L O c managed this space.
Another example in which the system call and library function is, U N i x provides a decision of the current time and date.
interface. Some operating systems provide a system call to return time, while the other returns the date. Any special treatment,
For example, the conversion between normal timers and summer time is handled by kernel or requires human intervention. U n i x is different, it only provides one
Strip system call, the system call returns the number of seconds of international standard time 1 977 January 1. For this value
Any explanation, such as transforming it into readable, using the time and date of the local time zone, all left to the user process.
In the standard C library, several routines are provided to handle most situations. These library functions handle a variety of details, such as various summer time
Algorithm.
The application can call the system call or library function, and many library functions call the system call. This is in Figure 1 - 2
display.
Another difference between system calls and library functions is: System calls usually provide a minimum interface, and library functions
Chapter 1 U N i X Basics 1 5
download
Figure 1-1 Malloc function and S B R K system call
Application code
User process
Kernel
Storage assignment function
M a L L o c
SBRK system call
It is often provided with more complex functions. We use the S B R K system
Call and M a L o C library function can be seen
At this point, in the future, it is more than a cached I / O letter.
Number (see Chapter 3) and Standard I / O functions (see Chapter 5)
This difference will also be seen.
Process control system call (fork, exec and w a i t)
It is usually called directly by the user's application (please recall
Basic S H E L1 in Program 1 - 5. But in order to simplify some
Some common situations, the U N i x system also provides some libraries
Functions; such as S Y S, E M and P O P e n. 8. 1 2 will explain
A implementation of the S Y S Y t e m function, it uses basic progress
Cheng control system call. 1 0. 1 8 Festival will also strengthen this
Example to process the signal correctly.
In order to make the reader understand the application of most programmers
U N i x system interface, we have to explain system adjustment
Use, only describe some library functions. For example, if only
S B R K system call, then there will be many applications
The M a L O C library function used in the order.
In addition to the two, this book will use the term function (F u n c t i o n) to refer to the system call and library function.
Both.
1.12 small knot
This chapter quickly browses U N i x. It describes some basic terms that will be used multiple times, introduces some small U N i X processes.
Or examples of the order, it can be perceived that the rest of this book will be further described.
The next chapter is about the standardization of u N i x, as well as this work on the current system. Standard, especially A n s i
C Standard and P O S i x. 1 The standard will affect the remainder of the book.
exercise
1. 1 In the system, except the root directory, the directory., .. 1. 2 In the output of the analysis program 1 - 4, what can process I d as the process of 8 5 2 and 8 5 3?
1. 3 In 1. 7, the parameters of P e r R O R are defined by the attribute C O N s T of ANSI C, and the integer parameters of R E R R O R are not
Use this attribute to define, why?
1. 4 In Appendix B Contains the error handler E R R _ S Y S, when the function is called, the value of E R R N o is saved, why?
1. 5 In the 32-bit integer number of symbols, then it will overflow?
1. 6 When the process time is stored in the 3 2-bit integer number of symbols, and the time is 1 0 0, then how many days have been
Will this time value overflow?
1 6 U N i x Environment Advanced Programming
download
Figure 1-2 Differences between C library functions and system calls
Application code
User process
Kernel
C library function
System call