Operating system (comparison between UNIX and Linux kernel information output) [1]

zhaozj2021-02-16  40

The operating system needs to output information to the console at the beginning of the startup, or when an internal error is detected. It is conceivable that this is a potential common process in the operating system. Whether it is UNIX, or Linux, as the kernel part of the operating system, it is particularly paying that the execution efficiency of the program. The process of our choice, their charm, can use the words "Li Li"; the meaning of Qing Li, is the natural beauty, and the connotation of the charm is clear. We are in detail to see their charm and different.

I. UNIX section (Printf)

In UNIX, it is the process Printf in the file printf to complete this work. It calls other processes, the procedures are as follows:

/ * Unix 6 --- prf.c

* The process we choose is Printf and Printn. The program part of Leon's original 2340 to 2378 lines.

* /

001 Printf (FMT, X1, X2, X3, X4, X5, X6, X7, X8, X9, XA, XB, XC)

002 char fmt [];

003 {

004 register char * s;

005 Register * ADX, C;

006

007 ADX = & x1;

008 loop:

009 While ((c = * fmt )! = '%') {

010 IF (c == '/ 0')

011 return;

012 Putchar (C);

013}

014 c = * fmt ;

015 IF (c == 'd' || c == 'L' || C == 'o')

016 Printn (* ADX, C == 'o'? 8: 10);

017 IF (c == 's') {

018 s = * ADX;

019 While (C = * S )

020 PUTCHAR (C);

021}

022 ADX ;

023 goto loop;

024}

025 / * -------------------------- * /

026

027 Printn (N, B)

028 {

029 register a;

030

031 IF (A = LDIV (N, B))

032 Printn (A, B);

033 PUTCHAR (LREM (N, B) '0');

034}

035 / * -------------------------- * /

036

037 / * Putchar (c); * /

038

Simply explained the procedure. First of all, you note that the C language is the old style, and it can be seen from the Parameter FMT of Printf. In addition, the process PUTCHAR involves hardware-related knowledge. Similarly, process LDIV and LREM are the process of assembly language. We will not analyze the relevant code for simplicity, but only provide the following tips:

For Printn, assume n = a * b b, then

l a = ldiv (n, b), and

L B = LREM (N, B), 0 <= B

Now you can go back and try to taste this code.

Let's first look at the process Printf.

007 007 Register Variable ADX records the address of the first parameter X1, pay attention to X1 occupies the stack unit, and the time is not expressed.

Formula. From 008 to 023, it is a big loop and is related to all of our work.

009 009 to 013 a while loop, the message string is in the FMT, and here, the output message is output, and the detection can be

"% D", "% L", "% o" or "% s" appear, indicating that there are decimal numbers in the back parameters, eight inputs or words

The string content needs to be output. Returns if the end is encountered.

014 014 can be seen in the same part of 009. Concise in C language, it can be achieved. When 009, if the register variable C is removed

When the character '%', the FMT will be moved backward and turn to 014. And here is the FMT content first, then move, then

C can be 'd', 'L', 'o' or 's'. If there is no '%' in the FMT, the entire string is sent out.

Return immediately, no possibility of performing 014 and latter procedures, there is no danger of FMT superior. This is the charm of logical strength

force.

015 and 016 call the recursive process Printn. The detailed description will be described later.

017 017 to 021 Treat string s. The whole process is relatively clear, it seems to be very easy to understand.

022 022 ADX moves one bit. Whose address is originally ADX store? The first parameter x1. Where is the rear shift pointing? Carefully

At first, other parameters such as X2, X3, etc. are not used in the program. Are you somewhat confused?

C Language Program Design When the process call and procedure statement, the parameters between the two do not match the match. The parameters are put on the stack in reverse, as shown in the figure:

The reason is because this version of UNIX running machine PDP11 is grown downward, that is, to the low address direction. So the address of "X1" is higher than "FMT", but it is below "x2", and other classes are pushed.

In this way, ADX has increased from 1, in fact, pointing to the next parameter.

023 goto statement, back to the loop.

Look at the Printn process below. From the prompt, you may be able to analyze, the process converts a binary to the base of the second parameter B into a set of numeric characters. A small algorithm is designed here.

031 and 032 This recursive call is the subject part of the algorithm. If n = a * b c, the recursive call is

"A (m) = a (m 1) * b c (m), 0 <= c (m)

A (0) = n, start return when A (M 1) == 0

The calculation process.

033 033 Return, output digital characters C (M), ..., C (0). The arithmetic value of the numbers, adding '0' convenient to obtain the corresponding character value.

This algorithm itself is better, and some of the assembly code is implemented, the efficiency is very high. However, the philosophy of UNIX is "as far as possible to simplify", using recursive calls make algorithms unclear, and it is slightly damaged in efficiency. Do not do track, you can understand the first parameter of the 033 line LREM n, why is C (M), ..., c (0)? If you change slightly, it may be better. Readers can try.

But this is also white jade, now look back and read the entire program, can you have new gains?

转载请注明原文地址:https://www.9cbs.com/read-25412.html

New Post(0)