-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.xml
364 lines (174 loc) · 295 KB
/
search.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>Elegant interaction with server: Git solution</title>
<link href="2022/elegant-interaction-with-server-git-solution.html"/>
<url>2022/elegant-interaction-with-server-git-solution.html</url>
<content type="html"><![CDATA[<h1 id="background">Background</h1><hr /><h2 id="common-development-paradigms">Common Development Paradigms</h2><hr /><ul><li>local: development</li><li>remote: running</li></ul><h2 id="tools">Tools</h2><hr /><ul><li>Git</li><li><code>Vscode</code> with extensions for remote development</li><li>ssh</li><li>docker</li></ul><h1 id="object">Object</h1><hr /><h2 id="local">local</h2><hr /><ul><li>Convenience with IDE<ul><li>bad case: development only with shell</li></ul></li><li>Local side and remote side build up well-organized versioncontrol</li></ul><h2 id="remote">Remote</h2><hr /><ul><li>Convenient version control<ul><li>after push from local side, remote side will automatically sync withpushed branch.</li></ul></li><li>Create virtual environment<ul><li>Well isolated</li><li>Portable</li><li>Friendly rebuild</li></ul></li></ul><h1 id="plan">Plan</h1><hr /><h2 id="git">Git</h2><hr /><h3 id="remote-1">Remote</h3><hr /><ul><li>Build up <code>repo</code> .</li></ul><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">git --bare init [仓库名].git<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><ul><li>Configure authority.</li></ul><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">git config receive.denyCurrentBranch ignore<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><ul><li>Record current directory path.</li><li>Enter into <code>hooks</code> directory.</li></ul><pre class="line-numbers language-Bash" data-language="Bash"><code class="language-Bash">cd hooks vim post-receive <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><ul><li>File <code>post-receive</code> help us sync our code.</li><li>File content, <code>git-dir</code> usually will be ignored.</li></ul><pre class="line-numbers language-Bash" data-language="Bash"><code class="language-Bash">#!/bin/shgit --work-tree=[Which directory to sync server-side code to] --git-dir=[Server-side repository address] checkout -f<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><ul><li>Explanation for <code>post-receive</code> .</li></ul><p><ahref="http://www.songqiaolin.com/index/resource/view/id/5.html">使用git在服务器上部署git仓库并实现提交代码时同步代码到生产环境- 服务器运维 – 宋巧林的博客</a></p><ul><li>Remember add <code>x</code> flag for file.</li></ul><h3 id="local-1">Local</h3><hr /><ul><li>Most important information for local side is address for remote<code>repo</code> : <code>ssh://[user@ip]:[``server`` filepath]</code></li></ul><p><ahref="https://www.jianshu.com/p/821ff301cbed">用git在服务器部署你的代码、同步文件</a></p><h1 id="reference">Reference</h1><hr /><ul><li><ahref="http://www.songqiaolin.com/index/resource/view/id/5.html">使用git在服务器上部署git仓库并实现提交代码时同步代码到生产环境- 服务器运维 – 宋巧林的博客</a></li><li><ahref="https://auroralzdf.github.io/2017/09/23/线上服务器搭建GIT服务器,实现本地代码上传并同步到服务器/">Aurora-龙振巅峰的博客</a></li><li><ahref="https://cidoliu.github.io/2020/12/01/NewGitServer/">搭建自己的Git服务器,实现团队协作</a></li><li><ahref="https://www.jianshu.com/p/821ff301cbed">用git在服务器部署你的代码、同步文件</a></li></ul>]]></content>
<categories>
<category> Tricks </category>
</categories>
</entry>
<entry>
<title>MIT 6.828 Lab3</title>
<link href="2021/mit-6-828-lab3.html"/>
<url>2021/mit-6-828-lab3.html</url>
<content type="html"><![CDATA[<h1 id="lab-3-user-environment">Lab 3 User Environment</h1><hr /><h2 id="overview">Overview</h2><hr /><p><strong>Process</strong> is one of the most important concept inoperating system. And it helps us manage our program with the guaranteein lots of aspects such as security, independency and etc. Naturally,operating system divide running state into different modes: user modeand kernel mode. And this is what we gonna do in this lab: implement thebasic kernel facilities required to get a protected user-mode processrunning. We should take a note here that JOS mangle its process as"environment"(<em>We introduce the term "environment" instead of thetraditional term "process" in order to stress the point that JOSenvironments and UNIX processes provide different interfaces, and do notprovide the same semantics.</em>, quoted from 6.828).</p><p>We will enhance the JOS kernel to set up the data structures to keeptrack of user environments, create a single user environment, load aprogram image into it, and start it running. These data structures helpsus run our system in perfect order.</p><p>What's more, we will also have the chance to make the JOS kernelcapable of handling any system calls the user environment makes andhandling any other exceptions it causes.</p><h2 id="preliminary">Preliminary</h2><hr /><h3 id="pcb">PCB</h3><hr /><p>CPU only run one process at a time until its end is far beyond ourdemand. Instead, we'll manage our processes running in turn. So it'sreally necessary to have a record for "living" process. There is nodoubt this job fell to operating system.</p><p>We need to design a data structure for the most key information ofprocess. And here is our PCB:</p><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token comment">// PCB for JOS</span><span class="token keyword">struct</span> <span class="token class-name">Env</span> <span class="token punctuation">{</span><span class="token keyword">struct</span> <span class="token class-name">Trapframe</span> env_tf<span class="token punctuation">;</span><span class="token comment">// Saved registers</span><span class="token keyword">struct</span> <span class="token class-name">Env</span> <span class="token operator">*</span>env_link<span class="token punctuation">;</span><span class="token comment">// Next free Env</span><span class="token class-name">envid_t</span> env_id<span class="token punctuation">;</span><span class="token comment">// Unique environment identifier</span><span class="token class-name">envid_t</span> env_parent_id<span class="token punctuation">;</span><span class="token comment">// env_id of this env's parent</span><span class="token keyword">enum</span> <span class="token class-name">EnvType</span> env_type<span class="token punctuation">;</span><span class="token comment">// Indicates special system environments</span><span class="token keyword">unsigned</span> env_status<span class="token punctuation">;</span><span class="token comment">// Status of the environment</span><span class="token class-name">uint32_t</span> env_runs<span class="token punctuation">;</span><span class="token comment">// Number of times environment has run</span><span class="token comment">// Address space</span><span class="token class-name">pde_t</span> <span class="token operator">*</span>env_pgdir<span class="token punctuation">;</span><span class="token comment">// Kernel virtual address of page dir</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>And here's what the <code>Env</code> fields are for:</p><blockquote><p><strong>env_tf</strong>:</p><p>This structure, defined in <code>inc/trap.h</code>, holds the savedregister values for the environment while that environment is<em>not</em> running: i.e., when the kernel or a different environmentis running. The kernel saves these when switching from user to kernelmode, so that the environment can later be resumed where it leftoff.</p><p><strong>env_link</strong>:</p><p>This is a link to the next <code>Env</code> on the<code>env_free_list</code>. <code>env_free_list</code> points to thefirst free environment on the list.</p><p><strong>env_id</strong>:</p><p>The kernel stores here a value that uniquely identifiers theenvironment currently using this <code>Env</code> structure (i.e., usingthis particular slot in the <code>envs</code> array). After a userenvironment terminates, the kernel may re-allocate the same<code>Env</code> structure to a different environment - but the newenvironment will have a different <code>env_id</code> from the old oneeven though the new environment is re-using the same slot in the<code>envs</code> array.</p><p><strong>env_parent_id</strong>:</p><p>The kernel stores here the <code>env_id</code> of the environmentthat created this environment. In this way the environments can form a“family tree,” which will be useful for making security decisions aboutwhich environments are allowed to do what to whom.</p><p><strong>env_type</strong>:</p><p>This is used to distinguish special environments. For mostenvironments, it will be <code>ENV_TYPE_USER</code>. We'll introduce afew more types for special system service environments in laterlabs.</p><p><strong>env_status</strong>:</p><p>This variable holds one of the following values:</p><ul><li><p><code>ENV_FREE</code>:</p><p>Indicates that the <code>Env</code> structure is inactive, andtherefore on the <code>env_free_list</code>.</p></li><li><p><code>ENV_RUNNABLE</code>:</p><p>Indicates that the <code>Env</code> structure represents anenvironment that is waiting to run on the processor.</p></li><li><p><code>ENV_RUNNING</code>:</p><p>Indicates that the <code>Env</code> structure represents thecurrently running environment.</p></li><li><p><code>ENV_NOT_RUNNABLE</code>:</p><p>Indicates that the <code>Env</code> structure represents a currentlyactive environment, but it is not currently ready to run: for example,because it is waiting for an interprocess communication (IPC) fromanother environment.</p></li><li><p><code>ENV_DYING</code>:</p><p>Indicates that the <code>Env</code> structure represents a zombieenvironment. A zombie environment will be freed the next time it trapsto the kernel. We will not use this flag until Lab 4.</p></li></ul><p><strong>env_pgdir</strong>:</p><p>This variable holds the kernel <em>virtual address</em> of thisenvironment's page directory.</p></blockquote><p>The data structure <code>Trapframe</code> has been mentioned aboveholds user-mode register state. <strong>Note</strong>: In JOS,individual environments do not have their own kernel stacks as processesdo in xv6. There can be only one JOS environment active in the kernel ata time, so JOS needs only a <em>single</em> kernel stack.</p><h3 id="basics-of-protected-control-transfer">Basics of ProtectedControl Transfer</h3><hr /><p>Exceptions and interrupts are both "protected control transfers,"which cause the processor to switch from user to kernel mode (CPL=0)without giving the user-mode code any opportunity to interfere with thefunctioning of the kernel or other environments. In Intel's terminology,an <em>interrupt</em> is a protected control transfer that is caused byan asynchronous event usually external to the processor, such asnotification of external device I/O activity. An <em>exception</em>, incontrast, is a protected control transfer caused synchronously by thecurrently running code, for example due to a divide by zero or aninvalid memory access.</p><p>In order to ensure that these protected control transfers areactually <em>protected</em>, the processor's interrupt/exceptionmechanism is designed so that the code currently running when theinterrupt or exception occurs <em>does not get to choose arbitrarilywhere the kernel is entered or how</em>. Instead, the processor ensuresthat the kernel can be entered only under carefully controlledconditions. On the x86, two mechanisms work together to provide thisprotection:</p><ol type="1"><li><p><strong>The Interrupt Descriptor Table.</strong> The processorensures that interrupts and exceptions can only cause the kernel to beentered at a few specific, well-defined entry-points <em>determined bythe kernel itself</em>, and not by the code running when the interruptor exception is taken.</p><p>The x86 allows up to 256 different interrupt or exception entrypoints into the kernel, each with a different <em>interrupt vector</em>.A vector is a number between 0 and 255. An interrupt's vector isdetermined by the source of the interrupt: different devices, errorconditions, and application requests to the kernel generate interruptswith different vectors. The CPU uses the vector as an index into theprocessor's <em>interrupt descriptor table</em> (IDT), which the kernelsets up in kernel-private memory, much like the GDT. From theappropriate entry in this table the processor loads:</p><ul><li>the value to load into the instruction pointer (<code>EIP</code>)register, pointing to the kernel code designated to handle that type ofexception.</li><li>the value to load into the code segment (<code>CS</code>) register,which includes in bits 0-1 the privilege level at which the exceptionhandler is to run. (In JOS, all exceptions are handled in kernel mode,privilege level 0.)</li></ul></li><li><p><strong>The Task State Segment.</strong> The processor needs aplace to save the <em>old</em> processor state before the interrupt orexception occurred, such as the original values of <code>EIP</code> and<code>CS</code> before the processor invoked the exception handler, sothat the exception handler can later restore that old state and resumethe interrupted code from where it left off. But this save area for theold processor state must in turn be protected from unprivilegeduser-mode code; otherwise buggy or malicious user code could compromisethe kernel.</p><p>For this reason, when an x86 processor takes an interrupt or trapthat causes a privilege level change from user to kernel mode, it alsoswitches to a stack in the kernel's memory. A structure called the<em>task state segment</em> (TSS) specifies the segment selector andaddress where this stack lives. The processor pushes (on this new stack)<code>SS</code>, <code>ESP</code>, <code>EFLAGS</code>, <code>CS</code>,<code>EIP</code>, and an optional error code. Then it loads the<code>CS</code> and <code>EIP</code> from the interrupt descriptor, andsets the <code>ESP</code> and <code>SS</code> to refer to the newstack.</p><p>Although the TSS is large and can potentially serve a variety ofpurposes, JOS only uses it to define the kernel stack that the processorshould switch to when it transfers from user to kernel mode. Since"kernel mode" in JOS is privilege level 0 on the x86, the processor usesthe <code>ESP0</code> and <code>SS0</code> fields of the TSS to definethe kernel stack when entering kernel mode. JOS doesn't use any otherTSS fields.</p></li></ol><h3 id="types-of-exceptions-and-interrupts">Types of Exceptions andInterrupts</h3><hr /><p>All of the synchronous exceptions that the x86 processor can generateinternally use interrupt vectors between 0 and 31, and therefore map toIDT entries 0-31. For example, a page fault always causes an exceptionthrough vector 14. Interrupt vectors greater than 31 are only used by<em>software interrupts</em>, which can be generated by the<code>int</code> instruction, or asynchronous <em>hardwareinterrupts</em>, caused by external devices when they needattention.</p><h2 id="experiment">Experiment</h2><hr /><h3 id="basic-work">Basic work</h3><hr /><p>Just follow what we've done to handle <code>pages</code> array, andfollow the instruction in comment.</p><p><img src="../images/lab3/exec_1_0.png" /></p><p>Here we allocate memories for this array to manage ourenvironments.</p><p><img src="../images/lab3/exec_1_1.png" /></p><p>Finish the map follow the comment.</p><h3 id="creating-and-running-environments">Creating and RunningEnvironments</h3><hr /><p>You will now write the code in <code>kern/env.c</code> necessary torun a user environment. Because we do not yet have a filesystem, we willset up the kernel to load a static binary image that is <em>embeddedwithin the kernel itself</em>. JOS embeds this binary in the kernel as aELF executable image.</p><p>The Lab 3 <code>GNUmakefile</code> generates a number of binaryimages in the <code>obj/user/</code> directory. If you look at<code>kern/Makefrag</code>, you will notice some magic that "links"these binaries directly into the kernel executable as if they were<code>.o</code> files. The <code>-b binary</code> option on the linkercommand line causes these files to be linked in as "raw" uninterpretedbinary files rather than as regular <code>.o</code> files produced bythe compiler. (As far as the linker is concerned, these files do nothave to be ELF images at all - they could be anything, such as textfiles or pictures!) If you look at <code>obj/kern/kernel.sym</code>after building the kernel, you will notice that the linker has"magically" produced a number of funny symbols with obscure names like<code>_binary_obj_user_hello_start</code>,<code>_binary_obj_user_hello_end</code>, and<code>_binary_obj_user_hello_size</code>. The linker generates thesesymbol names by mangling the file names of the binary files; the symbolsprovide the regular kernel code with a way to reference the embeddedbinary files.</p><p>This is a really fancy trick to handle the situation we standing atthat file system isn't established. In a word, this method extract theprogram we want to run, and using intermediate symbols to embed theprogram into kernel.</p><p>Let's check out the function I've implemented.</p><p><img src="../images/lab3/env_init.png" /></p><p>Just the same as what we do in <code>lab2</code> about theinitialization of page and corresponding link list, isn't it?</p><p><img src="../images/lab3/env_setup_vm.png" /></p><p>Every independent environment has its own "virtual memory indextable", more specifically page directory. The process of setting up issimilar to what we do in <code>lab2</code> in kernel, <strong>exceptfor</strong> priority problem. The place lower then <code>UTOP</code> isvacant. And there is no need to call <code>page_alloc</code> again.<code>pp_ref</code> higher than <code>UTOP</code> don't have tomaintain, because the only owner of it it's kernel <strong>exceptfor</strong> <code>env_pgdir</code>.</p><p><img src="../images/lab3/region_alloc.png" /></p><p>This is how we allocate memory for user-mode environment.</p><p><img src="../images/lab3/load_icode.png" /></p><p>This is similar to what we've done in Lab 1: check out the format of<code>elf</code> and follow it. Step by step, we can find how to mapfrom a binary file(in <code>elf</code> format) to user's virtual addressspace.</p><p><img src="../images/lab3/env_create.png" /></p><p>Here is our first environment.</p><p><img src="../images/lab3/env_run.png" /></p><p>Here is a context switch function from current environment to theappointed environment <code>e</code>.</p><p>Here we can make a summary. Here is machine's "timeline":</p><ul><li><p>start (kern/entry.S)</p></li><li><p>i386_init (kern/init.c)</p><ul><li>cons_init</li><li>mem_init</li><li>env_init</li><li>trap_init(still incomplete at this point)</li><li>env_create</li><li>env_run<ul><li>env_pop_tf</li></ul></li></ul></li></ul><h3 id="handling-interrupts-and-exceptions">Handling Interrupts andExceptions</h3><hr /><p>Our first job is to set up the IDT. We can read the information aboutthe interrupts we need to set in <code>kern/trap.h</code>.</p><p>Here we gonna trick macro to achieve this marvelous and excitingjob.</p><p>At first, we have a look at where IDT is defined.</p><p><img src="../images/lab3/idt_pos.png" /></p><p>It's a <code>struct Gatedesc</code> array.</p><p>Let's have a look at my final job, maybe you will be the sameconfused about these macros as me, but we'll figure out later:</p><p><img src="../images/lab3/trap_init.png" /></p><p>This is what I've done in <code>trap_init</code>.</p><p><img src="../images/lab3/use_macro2init.png" /></p><p>And this corresponds to what I've done in <code>trap_init</code>,term by term. It's located at <code>kern/trapentry.S</code> .</p><p><img src="../images/lab3/alltrap.png" /></p><p>With in the same assemble source file, we've finish the last stepswhich is the preparatory work of calling trap as above.</p><p>To understand what we are doing here, let's examine the macros we'veused.</p><p><img src="../images/lab3/macro4init.png" /></p><p>In <code>kern/trapentry.S</code>, we will find this 2 macros. One isfor the trap with error code, the other is for none. Combined with whatI've done using this macros, it's easy to figure out that it definestrap handler for every specific trap and it's in a repeatable way. Thereare two parameters in these macros. One is for function name, the otheris the number of the trap, which is also the index in IDT. Check outwhat we've done in <code>trap_init</code>, we declare a bunch of traphandler function but without the definition! These macros using thisdeclaration and expand to special codes for each of them. This isexactly where we provide definition for those declaration. These macrostag each special code with the function's declaration using<code>.global</code>. It offers a symbol for connecting the declarationin <code>trap.c</code> and the definition in<code>trapentry.S</code>.</p><p>If you look at these macros clearly enough, you will see it finallygoes to the same destination: <code>_alltraps</code>.</p><p><img src="../images/lab3/alltrap.png" /></p><p>Why this? Look what <code>_alltraps</code> does in the end, it'sexactly what its final purpose, calling <code>trap</code> function.</p><p><img src="../images/lab3/func_trap.png" /></p><p>Just focus on the parameter used in this function, it need<code>Trapframe</code> pointer. As mentioned in preliminary knowledge,when an x86 processor takes an interrupt or trap that causes a privilegelevel change from user to kernel mode, it also switches to a stack inthe kernel's memory. A structure called the <em>task state segment</em>(TSS) specifies the segment selector and address where this stack lives.The processor pushes (on this new stack) <code>SS</code>,<code>ESP</code>, <code>EFLAGS</code>, <code>CS</code>,<code>EIP</code>, and an optional error code. Then it loads the<code>CS</code> and <code>EIP</code> from the interrupt descriptor, andsets the <code>ESP</code> and <code>SS</code> to refer to the new stack.It's naturally to design this mechanism to record the current contextinto the kernel' stack which is used for recover the context of currentenvironment later. So it just push every field in <code>Trapframe</code>follow the inverse order. Some were pushed by machine, some were pushedby those 2 macros, the rest part need to be done in<code>_alltraps</code> by ourselves.</p><p>Last question, what is <code>SETGATE</code> used in<code>trap.c</code>?</p><p><img src="../images/lab3/SETGATE.png" /></p><p>Ah hah, it's a tool macro help us to set up the entry in IDT withstrict format.</p><h3 id="handling-page-faults-and-the-breakpoint-exception">Handling PageFaults and The Breakpoint Exception</h3><hr /><p>Since we've set up our IDT, the next question is how to use them?</p><p><img src="../images/lab3/func_trap.png" /></p><p>After we track what this function did, we can naturally find<code>trap_dispatch</code> is</p><p>suspicious.</p><p><img src="../images/lab3/trap_dispatch.png" /></p><p>Consider the comment, now we figure out what is this functionspurpose: handle different trap indexed by there interruption number. So,I just use switch case to handle different situation. Attention about<code>T_BRKPT</code> here. It's really important to be careful about theprivilege problem, formally its called <strong>DPL(descriptor privilegelevel)</strong>.</p><p><img src="../images/lab3/trap_init.png" /></p><p>It's level is set as 3 which represent user level. When exceptionoccur, it requires the CPL and RPL of current environment is not biggerthan DPL of the exception. It's a protection that prevent low privilegelevel code to share high level privilege.</p><h3 id="system-calls">System Calls</h3><hr /><p>We use functions such as <code>printf</code> naturally in Cprogramming. And we take it for granted. But if we think about whathappen under the phenomenon. Things get interesting.</p><p>There are some actions we have to achieve it with the help ofoperating system. Such as modify or read some data on the device andetc. In the case of <code>printf</code>, we perform some action on theterminal, which is regarded as a file in UNIX. All these help we getfrom operating system is requested by system calls.</p><p>User processes ask the kernel to do things for them by invokingsystem calls. When the user process invokes a system call, the processorenters kernel mode, the processor and the kernel cooperate to save theuser process's state, the kernel executes appropriate code in order tocarry out the system call, and then resumes the user process. The exactdetails of how the user process gets the kernel's attention and how itspecifies which call it wants to execute vary from system to system.</p><p>In the JOS kernel, we will use the <code>int</code> instruction,which causes a processor interrupt. In particular, we will use <code>int$0x30</code> as the system call interrupt. We have defined the constant<code>T_SYSCALL</code> to 48 (0x30) for us. We will have to set up theinterrupt descriptor to allow user processes to cause that interrupt.Note that interrupt 0x30 cannot be generated by hardware, so there is noambiguity caused by allowing user code to generate it.</p><p>If we check about <code>inc/syscall.c</code></p><p><img src="../images/lab3/inc_syscall.png" /></p><p>This function use the technique of inline assemble. We can writeassemble language in C programming. In <code>asm volatile...</code>part, we can check those registers meaning.</p><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">asm</span> <span class="token keyword">volatile</span><span class="token punctuation">(</span><span class="token string">"int %1\n"</span> <span class="token comment">// num</span> <span class="token operator">:</span> <span class="token string">"=a"</span> <span class="token punctuation">(</span>ret<span class="token punctuation">)</span> <span class="token comment">// return value store at eax and set to ret</span> <span class="token operator">:</span> <span class="token string">"i"</span> <span class="token punctuation">(</span>T_SYSCALL<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">// random</span> <span class="token string">"a"</span> <span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">// syscall number to eax</span> <span class="token string">"d"</span> <span class="token punctuation">(</span>a1<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">// 1st parameter edx</span> <span class="token string">"c"</span> <span class="token punctuation">(</span>a2<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">// 2nc parameter ecx</span> <span class="token string">"b"</span> <span class="token punctuation">(</span>a3<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">// 3rd parameter ebx</span> <span class="token string">"D"</span> <span class="token punctuation">(</span>a4<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">// 4th parameter edi</span> <span class="token string">"S"</span> <span class="token punctuation">(</span>a5<span class="token punctuation">)</span> <span class="token comment">// 5th parameter esi</span> <span class="token operator">:</span> <span class="token string">"cc"</span><span class="token punctuation">,</span> <span class="token string">"memory"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// if necessary, change condition registers and memroy</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>So, what we need to do in our code is offer a wrapper.</p><p><img src="../images/lab3/kern_syscall.png" /></p><p>And <code>syscall</code> is essentially a trap. So in<code>trap_dispatch</code>, we can add corresponding term for it.</p><p><img src="../images/lab3/trap_dispatch4syscall.png" /></p><p>Attention for the order of parameter.</p><h3 id="user-mode-start-up">User-mode start-up</h3><hr /><p>A user program starts running at the top of <code>lib/entry.S</code>.After some setup, this code calls <code>libmain()</code>, in<code>lib/libmain.c</code>. <code>libmain()</code> then calls<code>umain</code>, which, in the case of the hello program, is in<code>user/hello.c</code>. Note that after printing "<code>hello,world</code>", it tries to access <code>thisenv->env_id</code>.<code>libmain()</code> need to initialize the global pointer<code>thisenv</code> to point at the environment's <code>structEnv</code> in the <code>envs[]</code> array we've allocated before.However, how to get it?</p><p>Check the definition about the type of <code>env_id</code> , lowerten bits(0-9) offer our the index in <code>envs[]</code> array! And wecan use macro to help us extract that index.</p><p><img src="../images/lab3/libmain.png" /></p><h3 id="page-faults-and-memory-protection">Page faults and memoryprotection</h3><hr /><p>Memory protection is a crucial feature of an operating system,ensuring that bugs in one program cannot corrupt other programs orcorrupt the operating system itself.</p><p>Operating systems usually rely on hardware support to implementmemory protection. The OS keeps the hardware informed about whichvirtual addresses are valid and which are not. When a program tries toaccess an invalid address or one for which it has no permissions, theprocessor stops the program at the instruction causing the fault andthen traps into the kernel with information about the attemptedoperation. If the fault is fixable, the kernel can fix it and let theprogram continue running. If the fault is not fixable, then the programcannot continue, since it will never get past the instruction causingthe fault.</p><p>As an example of a fixable fault, consider an automatically extendedstack. In many systems the kernel initially allocates a single stackpage, and then if a program faults accessing pages further down thestack, the kernel will allocate those pages automatically and let theprogram continue. By doing this, the kernel only allocates as much stackmemory as the program needs, but the program can work under the illusionthat it has an arbitrarily large stack.</p><p>System calls present an interesting problem for memory protection.Most system call interfaces let user programs pass pointers to thekernel. These pointers point at user buffers to be read or written. Thekernel then dereferences these pointers while carrying out the systemcall. There are two problems with this:</p><ol type="1"><li>A page fault in the kernel is potentially a lot more serious than apage fault in a user program. If the kernel page-faults whilemanipulating its own data structures, that's a kernel bug, and the faulthandler should panic the kernel (and hence the whole system). But whenthe kernel is dereferencing pointers given to it by the user program, itneeds a way to remember that any page faults these dereferences causeare actually on behalf of the user program.</li><li>The kernel typically has more memory permissions than the userprogram. The user program might pass a pointer to a system call thatpoints to memory that the kernel can read or write but that the programcannot. The kernel must be careful not to be tricked into dereferencingsuch a pointer, since that might reveal private information or destroythe integrity of the kernel.</li></ol><p>In <code>page_fault_handler</code>, we have to check about the modeof current environment. How to get the knowledge about that? Actually,low 2 bits of CS segment selector are used to store the privilege level.0 for kernel, 3 for user. What we add in <code>page_fault_handler</code>shown as below.</p><p><img src="../images/lab3/page_fault_handler.png" /></p><p>And we need to check where the access to memory is legal or not.</p><p><img src="../images/lab3/user_mem_assert.png" /></p><p>It lead us to check <code>user_mem_check</code>.</p><p><img src="../images/lab3/user_mem_check.png" /></p><p>Here is my implementation. It's quite simple. A user program canaccess a virtual address has two condition:</p><ul><li>The address is below ULIM</li><li>The page table gives it permission</li></ul><p>We just check about the address from the beginning to the end.</p><h2 id="misc">MISC</h2><hr /><p>This is my final result of this lab</p><p><img src="../images/lab3/grade.png" /></p><p>And attention here, maybe I should put his at the very front of mypost, before you <code>make grade</code>, there are some<code>panic</code> function in the original code must be deleted. Iguess its for getting rid of some big trouble. But it really troubled meuntil I find out these "guardian".</p>]]></content>
<categories>
<category> OS </category>
</categories>
<tags>
<tag> 6.828 </tag>
</tags>
</entry>
<entry>
<title>MIT 6.828 Lab2</title>
<link href="2021/mit-6-828-lab2.html"/>
<url>2021/mit-6-828-lab2.html</url>
<content type="html"><![CDATA[<h1 id="lab-2-memory-management">Lab 2: Memory Management</h1><hr /><h2 id="overview">Overview</h2><hr /><p>After a long time I've finished Lab 2's task, here I take the notesabout my experiment process. This post mainly shows some subtleprinciple behind the we programmer can use limited memory freely withoutworrying about causing security trouble. In my opinion, this is totallyan art of space management.</p><h2 id="entire-layout-of-virtual-address-space">Entire Layout Of VirtualAddress Space</h2><hr /><p>We can check the design about layout that JOS uses in<code>inc/memlayout.h</code>. And there are lots of useful Macro whichhelp us locate the position in virtual address space.</p><p><img src="..\images\lab2\mem_layout.png" /></p><p>The region above <code>UTOP</code> is the main stage in lab 2. As for<code>Invalid Memory</code> mentioned in the picture. According to thecomment is function <code>mem_init</code>, which is the main characterin this lab, it's a "guard page" to monitor.</p><p>## Experiment Process</p><hr /><p>Nowadays, operating system using virtual memory to manage the limitphysics resources to satisfy the demand from the real situation inapplication that required by multi-process. In JOS, the specific methodto achieve protected-mode memory management is the combination of<strong>segmentation and page translation</strong>. Here is a <ahref="https://pdos.csail.mit.edu/6.828/2018/readings/i386/c05.htm">Link</a>introduce details about this method. Honestly, I was deeply amazed byits subtle design at first sight, although it's also really confused tome.</p><p><img src="../images/lab2/seg_pg_overview.png" /></p><p>As shown above, the entire translation divided into two steps.</p><p>First, with the <strong>Selector</strong> in the visible part ofsegment registers and <strong>Offset</strong> which is used as <strong>CPointer</strong> by programmer, we can fetch a <strong>LinearAddress</strong>. <strong>GDT & LGDT</strong> are the maincharacters at segmentation translation part. It's detail had beenhandled pretty well at lab 1. (In <code>boot/boot.S</code>, we installeda Global Descriptor Table (GDT) that effectively disabled segmenttranslation by setting all segment base addresses to 0 and limits to<code>0xffffffff</code>. Hence the "selector" has no effect and thelinear address always equals the offset of the virtual address. )</p><p>The second is what we have to care about. As mentioned in the upperlink, 80386 use Two levels of tables to address a page of memory.<strong>cr3</strong> act similar to what GDT & LGDT act insegmentation translation but not the same. More details, the upper linkshowed pretty well. We aren't wasting time at here.</p><p><img src="../images/lab2/seg_pg_overview1.png" /></p><h3 id="part-1-before-paging-physical-page-management">Part 1 BeforePaging: Physical Page Management</h3><hr /><p>Before we gonna using page table, where exactly it was? The answer iswe have to allocate that part by ourselves at the very beginning.</p><p>Let's check about our main character: <code>mem_init</code></p><p><img src="../images/lab2/mem_init_part1.png" /></p><p>The code calling <code>i386_detect_memory</code> needs to read thisdevice hardware in order to figure out how much physical memory thereis, but that part of the code is done for you: you do not need to knowthe details of how the CMOS hardware works.</p><p>The operating system must keep track of which parts of physical RAMare free and which are currently in use. JOS manages the PC's physicalmemory with <em>page granularity</em> so that it can use the MMU to mapand protect each piece of allocated memory.</p><p>We'll now write the physical page allocator. It keeps track of whichpages are free with a linked list of <code>struct PageInfo</code>objects (which, unlike xv6, are <em>not</em> embedded in the free pagesthemselves), each corresponding to a physical page. You need to writethe physical page allocator before you can write the rest of the virtualmemory implementation, because your page table management code will needto allocate physical memory in which to store page tables.</p><p>Because we haven't allocate page table yet, so before we can<em>allocate page</em>, we have to use some primal method:<code>boot_alloc</code>. As shown in comment, after we use this methodallocate space for <em>kernel page directory</em> and an array of<code>struct PageInfo</code> which is used to record information aboutphysical pages, we must abandon this method and using the operationfamily related to pages management.</p><p><img src="../images/lab2/boot_alloc.png" /></p><p>Follow the guidance in the comment, my solution about this functionwere shown above. The tricky part is <code>end</code> array, you canfind more details in the <code>*.ld</code> file. This trick assign ourbeginning of free memory to the kernel's end of <code>.bss</code>segment.</p><p><img src="../images/lab2/mem_init_part1_alloc.png" /></p><p>Our <code>boot_alloc</code> help us allocate space for <em>kernelpage directory</em> and an array of <code>struct PageInfo</code>. Then,let's initialize our allocated array.</p><p><img src="../images/lab2/page_init.png" /></p><p>Although we're manipulating an array, however, for more flexibleallocate pages, we use a link list to manage this array. And payattention to the comments, it gave us the detailed instructions aboutwhich pages were free, which pages were occupied.</p><p>Naturally, we can use page as manipulation unit. My solution wasshown below.</p><p><img src="../images/lab2/page_alloc_free.png" /></p><p>I recommend you use <code>assert</code> to alert too.</p><h3 id="part-2-virtual-memory">Part 2: Virtual Memory</h3><hr /><p>As mention before, modern operation system use this mechanism tomanage the memory. This part is the highlight in this lab, we're gonnaachieve lots of tool function used for this management by ourselves. Andthis part requires very clear knowledge about the upper tutorial in thelink about <strong>segmentation and page translation</strong>.</p><p>Recall that in part 3 of lab 1, we installed a simple page table sothat the kernel could run at its link address of 0xf0100000, even thoughit is actually loaded in physical memory just above the ROM BIOS at0x00100000. This page table mapped only 4MB of memory. In the virtualaddress space layout you are going to set up for JOS in this lab, we'llexpand this to map the first 256MB of physical memory starting atvirtual address 0xf0000000 and to map a number of other regions of thevirtual address space.</p><p>Here are some details have to be noticed. The JOS kernel often needsto manipulate addresses as opaque values or as integers, withoutdereferencing them, for example in the physical memory allocator.Sometimes these are virtual addresses, and sometimes they are physicaladdresses. To help document the code, the JOS source distinguishes thetwo cases: the type <code>uintptr_t</code> represents opaque virtualaddresses, and <code>physaddr_t</code> represents physical addresses.Both these types are really just synonyms for 32-bit integers(<code>uint32_t</code>), so the compiler won't stop you from assigningone type to another! Since they are integer types (not pointers), thecompiler <em>will</em> complain if you try to dereference them.</p><p>The JOS kernel can dereference a <code>uintptr_t</code> by firstcasting it to a pointer type. In contrast, the kernel can't sensiblydereference a physical address, since the MMU translates all memoryreferences. If you cast a <code>physaddr_t</code> to a pointer anddereference it, you may be able to load and store to the resultingaddress (the hardware will interpret it as a virtual address), but youprobably won't get the memory location you intended.</p><p>We can check this by using <code>gdb</code> and<code>qemu</code>.</p><p><img src="../images/lab2/lab2_ex3.png" /></p><p>And in future labs we will often have the same physical page mappedat multiple virtual addresses simultaneously (or in the address spacesof multiple environments). We will keep a count of the number ofreferences to each physical page in the <code>pp_ref</code> field of the<code>struct PageInfo</code> corresponding to the physical page. Whenthis count goes to zero for a physical page, that page can be freedbecause it is no longer used. In general, this count should be equal tothe number of times the physical page appears <em>below<code>UTOP</code></em> in all page tables (the mappings above<code>UTOP</code> are mostly set up at boot time by the kernel andshould never be freed, so there's no need to reference count them).We'll also use it to keep track of the number of pointers we keep to thepage directory pages and, in turn, of the number of references the pagedirectories have to page table pages.</p><p>At first, let's implement two "searching funciton"</p><p><img src="../images/lab2/pgdir_walk.png" /></p><p>These function return the page table entry for inquired virtualaddress(more specifically, it's linear address). It will create a newpage table if <code>create</code> is set and there didn't existcorresponding page table entry before asking.</p><p>And with the help of <code>pgdir_walk</code> we can implement theother "searching function"</p><p><img src="../images/lab2/page_lookup.png" /></p><p>This function returns <code>struct PageInfo</code> which contains theinformation about the physical page corresponding to the virtualaddress, and if necessary, it will fetch page table entry by<code>pte_store</code>.</p><p>Before we design a "insert" function, we have to design a "remove"function, because we have to prepare a space for the page we have toinsert.</p><p><img src="../images/lab2/page_remove.png" /></p><p>Check about <code>page_decref</code> function, it helps update<code>pp_ref</code> for us.</p><p>And here is the function needs to be careful: "insert".</p><p><img src="../images/lab2/page_insert.png" /></p><p>As mentioned in the comments, we've considered the corner casementioned in it. And handle everything in one code path way. However, wemust update <code>pp_ref</code> before we remove existed page. Because,if the order is reversed, after <code>page_remove</code>, that physicalpage might be recycled incorrectly.</p><p>Next function used for allocate space for kernel part in the virtualaddress space.</p><p><img src="../images/lab2/boot_map_region.png" /></p><p>Just follow the advice in comment, and remember, do<strong>not</strong> update <code>pp_ref</code> field in this function.It should be done by its sub-function.</p><h3 id="part-3-kernel-address-space">Part 3: Kernel Address Space</h3><hr /><p>Since kernel and user memory are both present in each environment'saddress space, we will have to use permission bits in our x86 pagetables to allow user code access only to the user part of the addressspace. Otherwise bugs in user code might overwrite kernel data, causinga crash or more subtle malfunction; user code might also be able tosteal other environments' private data. Note that the writablepermission bit (<code>PTE_W</code>) affects both user and kernelcode!</p><p>The user environment will have no permission to any of the memoryabove <code>ULIM</code>, while the kernel will be able to read and writethis memory. For the address range <code>[UTOP,ULIM)</code>, both thekernel and the user environment have the same permission: they can readbut not write this address range. This range of address is used toexpose certain kernel data structures read-only to the user environment.Lastly, the address space below <code>UTOP</code> is for the userenvironment to use; the user environment will set permissions foraccessing this memory.</p><p>In a word, we have to achieve permissions and fault isolation.</p><p><img src="../images/lab2/set_kernel.png" /></p><p>Follow the advice in comment, we can achieve the easiest task in lab2.</p><h2 id="others">Others</h2><hr /><p>Attention here, before use <code>make grade</code>, be sure you'veremove <strong>panic</strong> lines mentioned in <code>mem_init</code>.It seems like this:</p><p><img src="../images/lab2/attention.png" /></p><p>And this is the grade if you've correctly achieve the tasks inlab2:</p><p><img src="../images/lab2/grade.png" /></p>]]></content>
<categories>
<category> OS </category>
</categories>
<tags>
<tag> 6.828 </tag>
</tags>
</entry>
<entry>
<title>MIT 6.828 Lab1</title>
<link href="2021/mit-6-828-lab1.html"/>
<url>2021/mit-6-828-lab1.html</url>
<content type="html"><![CDATA[<h1 id="booting-a-pc">Booting a PC</h1><hr /><h2 id="overview">Overview</h2><hr /><p>This post records my experiment process which was based on <code>MIT6.828</code>. Basically, the order of the introduction in this postfollows the sequence of the implementation periods.</p><p>What's more, I think one of my best friend achieve a better reviewjob. This is <ahref="https://elliotqi.top/2021/03/03/1-lab-1-booting-a-pc/">hispost</a></p><h2 id="recommended-books">Recommended Books</h2><hr /><ul><li>CSAPP</li><li>汇编语言(王爽)</li><li>程序员的自我修养:装载、链接和库</li></ul><h2 id="preliminary-knowledges">Preliminary Knowledges</h2><hr /><h3 id="layout-of-physical-address-space">Layout of physical addressspace</h3><hr /><p><img src="../images/lab1/pc_physics_layout.png" /></p><ul><li>The first PCs, which were based on the 16-bit Intel 8088 processor,were only capable of addressing 1MB of physical memory.(0x00000000~0x000FFFFF)</li><li>"Low Memory" was the <em>only</em> random-access memory (RAM) thatan early PC could use</li><li>The 384KB area from 0x000A0000 ~0x000FFFFF was reserved by thehardware for special uses such as video display buffers and firmwareheld in non-volatile memory. The most important part is the BasicInput/Output System (BIOS), which occupies the 64KB region from0x000F0000~0x000FFFFF. In early PCs the BIOS was held in true ROM, butcurrent PCs store the BIOS in updateable flash memory.</li><li>The BIOS is responsible for performing basic systeminitialization</li><li>After performing this initialization, the BIOS loads the operatingsystem from some appropriate location such as floppy disk, hard disk,CD-ROM, or the network, and passes control of the machine to theoperating system.</li><li>Modern PCs have a "hole" in physical memory from0x000A0000~0x00100000, dividing RAM into "low" or "conventional memory"(the first 640KB) and "extended memory" (everything else). (ensurebackward compatibility with existing software.)</li><li>In addition, some space at the very top of the PC's 32-bit physicaladdress space, above all physical RAM, is now commonly reserved by theBIOS for use by 32-bit PCI devices.</li><li>Because of design limitations JOS will use only the first 256MB of aPC's physical memory anyway, so for now we will pretend that all PCshave "only" a 32-bit physical address space.</li></ul><h3 id="gdt">GDT</h3><hr /><p><img src="../images/lab1/GDT_as.png" /></p><ul><li>SEG is used to construct GDT, which is defined in<code>mmu.h.</code></li><li>At the boot loader part in the experiment process, we look back tothis.</li></ul><h3 id="cr-registers-family">CR registers family</h3><hr /><h2 id="process">Process</h2><hr /><p><img src="../images/lab1/CR_fm.png" /></p><ul><li>Layout picture about CR registers family.</li><li>CR family is one of the important themes about booting process, wewill meet the member of <code>CR</code> family many times.</li></ul><h3 id="bios">BIOS</h3><hr /><p>After we enter into JOS(experiment platform used in this class), wecan walk around and check what we got.</p><p><img src="../images/lab1/bootloader_begin.png" /></p><ul><li>We can read these instructions to figure out what had happened atthe very beginning of booting a PC. At this stage, BIOS is our maincharacter.</li><li>There are so many complicated jobs. And I just have look throughseveral beginning steps roughly, then I jumped over this process.</li><li>To jump over this process, set a breakpoint at <code>0x7c00</code>,then let it go, it will head to the entry point of next stage.</li></ul><h3 id="the-boot-loader">The Boot Loader</h3><hr /><ul><li>The main tasks of the boot loader can be concluded as two parts:switch mode and load the kernel.<ul><li>First, the boot loader switches the processor from real mode to32-bit protected mode, because it is only in this mode that software canaccess all the memory above 1MB in the processor's physical addressspace.</li><li>Second, the boot loader reads the kernel from the hard disk bydirectly accessing the IDE disk device registers via the x86's specialI/O instructions.</li></ul></li><li>Boot loader is loaded by the former process and called by the formermain character: BIOS.</li><li>Here are some basic knowledges:<ul><li>Floppy and hard disks for PCs are divided into 512 byte regionscalled <em>sectors</em>. A sector is the disk's minimum transfergranularity: each read or write operation must be one or more sectors insize and aligned on a sector boundary.</li><li>If the disk is bootable, the first sector is called the <em>bootsector</em></li></ul></li><li>Here is my experiment result at this stage:</li></ul><p><img src="../images/lab1/entry_boot_loader.png" /></p><h4 id="switch-mode">Switch Mode</h4><hr /><p><img src="../images/lab1/switch_mode0.png" /></p><ul><li>Disable interrupts & String Operations increment.</li><li>Clear segment registers.</li></ul><p><img src="../images/lab1/switch_mode1.png" /></p><ul><li>Enable A20 (polling method).</li><li><code>lgdt gdtdesc</code> load information in <code>gdtdesc</code>(next slide) into GDTR(in CPU).</li><li>After that use three instructions to set up <code>cr0</code>’slowest(0) bit (PE, protected mode enable).</li><li><code>Ljmp $PROT_MODE_CSEG, $protcseg</code> simply jump to nextinstruction, but in 32-bit code segment. Switches processor into 32-bitmode. We can check this at following picture.</li></ul><p><img src="../images/lab1/switch_mode2.png" /></p><ul><li>The relevant knowledge about <code>GDT</code> and <code>cr</code>registers family, we can check at the preliminary knowledgesection.</li></ul><p><img src="../images/lab1/switch_mode3.png" /></p><p><img src="../images/lab1/switch_mode4.png" /></p><ul><li>Here comes the highlight part of this periods: set up theprotected-mode data segment registers.</li></ul><p><img src="../images/lab1/switch_mode5.png" /></p><ul><li>We are now at the end of the <code>boot loader</code>. We have toassign the control to the next part.</li><li>If something went wrong, loop.</li></ul><h4 id="loading-the-kernel">Loading The Kernel</h4><hr /><ul><li>Kernel image must be ELF format("Executable and LinkableFormat")(More details: 程序员的自我修养)</li><li>VMA(link address) & LMA(load address)</li><li>The link address of a section is the memory address from which thesection expects to execute.</li><li>The linker encodes the link address in the binary in various ways,such as when the code needs the address of a global variable, with theresult that a binary usually won't work if it is executing from anaddress that it is not linked for. (Modern PIC tech is used solvethis)</li><li>Typically, the link and load addresses are the same.</li><li>The source code of this part is in <code>main.c</code>, we can checkthe simplify introduction at its beginning:</li></ul><p><img src="../images/lab1/load_kernel0.png" /></p><p>We are going to travel through this file and gain the insight aboutthe process which is aimed at loading the kernel.</p><p><img src="../images/lab1/load_kernel1.png" /></p><ul><li>Read 'count' (para) from kernel image into physical address'pa'(para).</li><li>Using <code>readsect</code>:</li></ul><p><img src="../images/lab1/load_kernel2.png" /></p><ul><li><code>readsect</code> using a 'low-level' method to achieve thetarget that reading a sector into physical address space.</li><li><code>waitdisk</code> using the idea of <code>polling</code>.</li></ul><p><img src="../images/lab1/load_kernel3.png" /></p><ul><li><code>"readseg((uint32_t) ELFHDR, SECTSIZE*8, 0);"</code>reading thefirst page (1 page= 8sectors).</li><li><code>ELF_MAGIC</code>, member of <code>ELF32(64)_Ehdr</code>,contained in the member e_ident . The beginning 4 bytes is <code>0x7F,0x45, 0x4c, 0x46</code>.(In ascii, DEL, 'E', 'L', 'F'.).</li><li>Interesting fact <code>a.out</code> is 0x1, 0x7; PE/COFF is 0x4d,0x5a aka 'M', 'Z' in ascii.</li><li><code>ELF32_Ehdr</code> tell us <code>e_phoff</code> (start ofprogram headers) and <code>e_phnum</code>(Number of program headers).Using this to index the program headers in the kernel image.</li><li>Using members in <code>Elf32_Phdr</code>(program header),<code>p_pa</code>(physical, LMA), <code>p_memsz</code>(size of segment),<code>p_offset</code>(offset of segment in kernel image).</li><li><code>e_entry</code>, the member of <code>Elf32_Ehdr</code>. (VMA ofthe entry, for relocatable file aka <code>*.o</code> in linux<code>*.obj</code> in windows, is 0).</li></ul><p><img src="../images/lab1/load_kernel6.png" /></p><ul><li>Deeper into base, we can check the content in<code>obj/boot/boot.asm</code> which came from <code>boot.S</code> and<code>main.c</code> after compilation.</li><li>Loading the kernel into disk involved with plenty of<code>I/O</code> operation, and it need lots of knowledge about the baseof the system. Here, I highly recommended you read it but in a overviewway.</li><li>Take a notice on the address, when we travel through<code>qemu-gdb</code>, we can check the consistence about theaddress.</li><li>Step further, give an eye about following variation about thecontent in address <code>0x100000</code> which reveal the changing ofmemory mode.</li></ul><p><img src="../images/lab1/load_kernel7.png" /></p><ul><li>The phenomenon above showed that the success about mode switchingand the right of control has been transferred to the main character ofthe next stage: kernel</li></ul><h3 id="kernel">Kernel</h3><hr /><p>At first, go around in the <code>gdb</code>, you can check new changeabout the content inferred by the same address when we pass by some'magic' instructions.</p><p><img src="../images/lab1/load_kernel4.png" /></p><ul><li>Attention at the content at <code>0xf0100000</code> and<code>0x100000</code>. Check the difference around the "magicinstructions":</li></ul><pre class="line-numbers language-assembly" data-language="assembly"><code class="language-assembly">mov $0xf010002f, %eax<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><ul><li>Same as before, this variation revealed the mapping method has beenchanged.</li><li>Actually the instruction above isn't the core part of the magic,it's just the "signal step" which represents the conversion betweenformer mapping method to virtual memory mapping.</li><li>New mapping mechanism was actually set up at the moment just after<code>mov %eax, %cr0</code></li><li>Let's step further in the source code to dig more detail about this"magic".</li></ul><p><img src="../images/lab1/load_kernel8.png" /></p><p><img src="../images/lab1/load_kernel9.png" /></p><ul><li>Use <code>eax</code> to load the physical address of<code>entry_pgdir</code> into cr3 which is defined in<code>entrypgdir.c.</code> From <code>gdb</code> we can tell that<code>entry_pgdir</code> is <code>0x110000</code>. We can also checkthis from <code>obj/kern/kernel.asm</code></li><li>And we're gonna set cr0 with <code>orl$(CR0_PE|CRP_PG|CRP_WP)</code> PE represents "protection enable", PGrepresents "Paging", and WP represents "Write Protect". After that, thenew mapping is established. (Look back to the picture at preliminaryknowledge)</li></ul><p><img src="../images/lab1/load_kernel10.png" /></p><p><img src="../images/lab1/load_kernel11.png" /></p><ul><li>What happened here is the same as what had happened when we enteredinto protection mode. Just jump to the next instruction.</li></ul><p>There is still a final task needs to be done by kernel: build up thestack.</p><p><img src="../images/lab1/stack0.png" /></p><p><img src="../images/lab1/stack1.png" /></p><p>The process obey the following steps:</p><ul><li>Clear <code>ebp</code>.</li><li>Set <code>esp</code>(we can check it at<code>obj/kern/kernel.asm</code>)</li><li>note here: KSTKSIZE=8 page size</li><li>Parameter about the stack JOS build<ul><li>Virtual address: <code>0xf0108000-0xf0110000</code></li><li>Physical address: <code>0x00108000-0x00110000</code></li></ul></li></ul><p>And here is a schematic diagram about the layout of the stack:</p><p><img src="../images/lab1/stack2.png" /></p><h2 id="to-be-continued">To be continued</h2><hr /><p>This paper ignore plenty of details about Lab 1, such as<code>VA_LIST</code> part and etc.</p>]]></content>
<categories>
<category> OS </category>
</categories>
<tags>
<tag> 6.828 </tag>
</tags>
</entry>
<entry>
<title>Raspberry Pi Learning</title>
<link href="2020/raspberry-pi-learning.html"/>
<url>2020/raspberry-pi-learning.html</url>
<content type="html"><![CDATA[<h1 id="preview">Preview</h1><hr /><p>Here record the learning process of my RaspberryPi.</p><h1 id="search-the-ip-address-of-raspberry-pi">Search the IP Address ofRaspberry PI</h1><hr /><p>Because of the lack of screen, I turn to add an empty ssh file inraspberry pi's boot. In order to get access to raspberry pi by ssh. Ihave to search the IP address first. Here is my way:</p><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">ping raspberrypi.local<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><h1 id="about-ssh">About SSH</h1><hr /><p>Recently, I learnt that windows terminal it has built-in<code>ssh</code> function. So I took a try on this instead of usingPuTTY. However, there are some trouble striking me that.</p><p>Firstly, what does this situation means: <ahref="https://superuser.com/questions/421074/ssh-the-authenticity-of-host-host-cant-be-established/421084#421084">ssh:The authenticity of host can't be established</a> . This is <strong>notan error</strong>, it's just <strong>warning</strong> you have to<strong>confirm</strong> the host you've never establish connectionbefore. Some people will mistaken this and lower the level of thesecurity of his <code>ssh</code>(however, sometimes, lower the level maybe a better choice, it depends on the situation you meet with).</p><p>What's more, what will happen if I rewrite the RaspberryPi's OS, Imean, using windows terminal without convenient PuTTY tool, how to <ahref="https://stackoverflow.com/questions/20840012/ssh-remote-host-identification-has-changed">SSHremote host identification has changed</a> . This questions instacksoverflow solves my problem with this command:</p><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">$ ssh-keygen -R <host><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>With this command, it will removes all keys belonging to host fromknown_hosts file.</p>]]></content>
<categories>
<category> Raspberry pi </category>
</categories>
<tags>
<tag> Tools </tag>
<tag> Technique </tag>
</tags>
</entry>
<entry>
<title>Docker Learning</title>
<link href="2020/docker-learning.html"/>
<url>2020/docker-learning.html</url>
<content type="html"><![CDATA[<h1 id="preview">Preview</h1><hr /><p>This semester, I was going to do some project such as machinelearning, MIT 6.828 course, on my Raspberry Pi. So, I obsessed to learnthe using of docker.</p><p>This blog continues to be updated.</p><h1 id="basic-ideas">Basic Ideas</h1><hr /><p>Raspberry pi 4 is based on <strong>ARM</strong> architecture, andDocker CE not only support <strong>x86_64</strong>, but also<strong>ARM</strong>. So to suit for docker CE, your raspberry pi's OSshould be <strong>Raspberry Pi OS Buster</strong></p><p>There are two different ways to install docker on your PC.</p><h1 id="installation">Installation</h1><hr /><p>Thanks for <ahref="https://yeasy.gitbook.io/docker_practice/install/raspberry-pi">thisblog</a>, most of the installation part of Docker on Raspberry pi isbased on this blog.</p><h2 id="using-shell-script">Using Shell Script</h2><hr /><p>Actually, for the guys like me, just for learning docker andblahblahblah, I highly recommended you to use this method, maybe afterplenty of experience of docker, you can use <strong>apt</strong> methodwhich will be introduced next chapter.</p><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">$ curl -fsSL get.docker.com -o get-docker.sh && sudo sh get-docker.sh --mirror Aliyun<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>The option <em>mirror</em> will determine whether to use mirrorsource to speed up the downloading process. Because some country mayhave some problem on connecting with its official website.</p><h2 id="apt-method">Apt method</h2><hr /><p>First step, adding some packages using HTTPS and CA certification</p><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">$ sudo apt-get update$ sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg2 \ lsb-release \ software-properties-common<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>Now, to confirm the legality of the software you wanna download, addGPG key</p><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">$ curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/raspbian/gpg | sudo apt-key add -# official# $ curl -fsSL https://download.docker.com/linux/raspbian/gpg | sudo apt-key add -<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>Now, we need to add the source of Docker CE software:</p><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">$ sudo add-apt-repository \ "deb [arch=armhf] https://mirrors.ustc.edu.cn/docker-ce/linux/raspbian \ $(lsb_release -cs) \ stable"# official# $ sudo add-apt-repository \# "deb [arch=armhf] https://download.docker.com/linux/raspbian \# $(lsb_release -cs) \# stable"<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>Ensure you have accomplish the step 1, otherwise you may not use<code>add-apt-repository</code></p><p>The last step, load the elephant into the refrigerator.</p><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">$ sudo apt-get update$ sudo apt-get install docker-ce<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><h1 id="launch-the-docker">Launch the Docker</h1><hr /><p>If you OS use <code>systemd</code>(actually the system that blogrecommended is using <code>systemd</code>), run this command</p><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">$ sudo systemctl enable docker$ sudo systemctl start docker<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>Now, we gonna create a group named <code>docker</code> , and add yourcurrent user into docker so that without becoming <code>root</code> usercan you use docker.</p><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">$ sudo groupadd docker$ sudo usermod -aG docker $USER# For instance, maybe you are still login your raspberry pi as user, normally, pi, you can use pi to replace `$USER`: `sudo usermod -aG docker pi`<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><h2 id="test-the-installation">Test The Installation</h2><hr /><p>Try this:</p><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">$ docker run arm32v7/hello-worldUnable to find image 'hello-world:latest' locallylatest: Pulling from library/hello-worldd1725b59e92d: Pull completeDigest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788Status: Downloaded newer image for hello-world:latestHello from Docker!This message shows that your installation appears to be working correctly.To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bashShare images, automate workflows, and more with a free Docker ID: https://hub.docker.com/For more examples and ideas, visit: https://docs.docker.com/get-started/<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>Maybe you will meet with this situation, which is the situation I'vemet at the first installation:</p><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">Docker: Error response from daemon: failed to create endpoint on network bridge: failed to add blahblahblah....<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>Try to reboot your raspberry ;). Maybe it also works for you.</p><h2 id="speed-up">Speed Up</h2><hr /><p>As I've mentioned, many country may have some problem with contactingwith the hub of docker. For linux using <code>systemd</code>, such asUbuntu 16.04+、Debian 8+、CentOS 7.</p><p>Find <code>/etc/docker/daemon.json</code> (if this file doesn'texist, create it), and adding the contents like this:</p><pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">{</span> <span class="token property">"registry-mirrors"</span><span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token string">"https://hub-mirror.c.163.com"</span><span class="token punctuation">,</span> <span class="token string">"https://mirror.baidubce.com"</span> <span class="token punctuation">]</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>Always remember this must be strictly satisfy with the<code>json</code> format.</p><p>Then restart the service:</p><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">$ sudo systemctl daemon-reload$ sudo systemctl restart docker<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><h1 id="basic-usage">Basic Usage</h1><hr /><p>About the usage tutorial of docker, there is no doubt that theofficial docs are the best choice. And for most people who just try touse some frequently used docker command, I highly recommend <ahref="https://yeasy.gitbook.io/docker_practice/install/raspberry-pi">thisblog</a> which I've mentioned before.</p><h2 id="basic-idea">Basic Idea</h2><hr /><p>There are some basic concepts of docker, such as image, container andrepository. And just keep in mind the following points will just befine. To explore more, you can read the blog I recommend and search inthe docs.</p><ul><li><p>Always keep in mind, the more specifically usage of the commandcan be learnt from docs or <code>--help</code></p></li><li><p>The image is composed by a set of files which are organized byhierarchy architecture.</p></li><li><p>The changes you've done on container will just be record into thecurrent storage file.</p></li><li><p>It's funny that docker actually working in a C/S mode. Thatcreate a <strong>context</strong> concept. The process of creating yourown image will be done on docker's <strong>'server'</strong>, and therange of files that you can manipulate is determined by the<strong>context</strong>. More specifically, only the files under the<strong>context</strong> directory.</p></li><li><p>To save the space, docker rely on the mechanism which the file ismulti-used.</p></li></ul><h2 id="basic-image-operation">Basic Image Operation</h2><hr /><ul><li>Get the image: <code>docker pull [opts]RepositoryName[:tag]</code></li><li>list the image: <code>docker image ls [opts][repository[:tag]]</code><ul><li><code>-f</code>(also named <code>--filter</code>) is quite useful,according to that blog.</li><li>The combination of <code>-f</code> and <code>-q</code> will just theID of the dockers that you wanna remove.</li></ul></li><li>remove the image: <code>docker image rm [opts] <Image idlist></code> or <code>docker rmi [opts] <Image id list></code><ul><li>Remember that, you can only remove the certain image when there isno other container is based on it.</li><li>The things really happens when we run the remove commands, is wellinterpreted in the blog. Broadly speaking, it has <em>untagged</em>process and <em>Deleted</em> process.</li><li><code>docker container prune</code>: this will remove all the unusedimages.</li></ul></li></ul><h2 id="container-operation">Container Operation</h2><hr /><h3 id="running-docker">Running Docker</h3><hr /><ul><li>command format: <code>docker run [opts] repository:tag [CMD]</code><ul><li><code>[CMD]</code> represent the first command executed by dockerwhen you first create a brand new docker. Otherwise, without this, itwill be determined by docker file</li></ul></li><li><code>-i</code>: Interactive mode, keep container's stdin open</li><li><code>-t</code>: Dispatch a pseudo-tty binding with stdin</li><li><code>--rm</code>: Once you stop the container, it will be removedautomatically.</li><li><code>-d</code>: Docker will execute its command at background. Youcan check the output by <code>docker container logs</code></li></ul><h3 id="start-restart-and-stop">Start, Restart and Stop</h3><hr /><ul><li><code>docker container stop</code>: Stop the container. In theinteractive mode, you can also achieve this by <code>exit</code> or<kbd>ctrl</kbd>+<kbd>d</kbd></li><li><code>docker restart</code>: used to stop the container that isrunning and restart it</li><li><code>docker start</code>: start the container which is in theterminate mode</li></ul><h3 id="enter-the-container">Enter the Container</h3><hr /><p>This is aimed to handle the container which is running with<code>-d</code>, there are two commands: <code>attach</code> and<code>exec</code></p><ul><li><code>docker attach</code>: If you use <code>exit</code>, thecontainer will stop</li><li><code>docker exec [OPOTIONS] CONTAINER COMMAND [ARG]</code>: Can befollowed by <code>-i</code>, <code>-t</code>. When you exit, thecontainer will not stop.</li></ul><h3 id="remove">Remove</h3><hr /><ul><li><code>docker container rm</code>: Remove a container which is in theterminate mode<ul><li><code>-f</code>: with this options, you can remove a container inthe running mode</li></ul></li><li><code>docker prune</code>: Remove all the containers that is in theterminate mode</li></ul><h3 id="commit">Commit</h3><p>Actually, you can create your own image in this way. However, it'snot a recommended method. Because of hierarchy architecture that I'vementioned before, this will cause unnecessary redundant on<strong>image</strong>, such as you can't predict what will reallyhappen when you use remove commands to build your image. It's alsotroublesome that it is really hard to maintain because it's hard toassure what happened about this new image. What's more, the logicaldesign of this way can't be explained in an explicit way.</p><p>However, just like what said in the reference blog, use commit we caneasily comprehend the mechanism about how <strong>dockerfile</strong>create your own image and what will happen when docker is creating animage refer to <strong>dockerfile</strong>.</p><p>Firstly, you create a container use the image that you already have.Do some operation on it and make some change. You can check thedifference caused by your operation by <code>docker diff</code>.</p><p>Then the leading actor of this section <code>commit</code> will showus its function.</p><p><code>docker commit [opt] <container's ID or name> [repositoryname[: tag]]</code></p><p>Use the container modified by you to create a new image which isespecially identified by <code>[repository name[: tag]]</code>.</p><p>You can use <code>docker history</code> to see the logs recording theprocess of creating this image. Actually, it also shows the layers whichcompose this image. When you try to use this image to create a newcontainer and run it, you will feel that it seems that you've alreadydone the operation, which you've done on the original operation before,on this container too.</p><h2 id="dockerfile">Dockerfile</h2><hr /><p>Using <strong>Dockerfile</strong> is more formal way to build animage. Suppose we've already finish a Dockerfile. How to best use it tocreate a demo image?</p><p>The answer is <code>docker build</code>, we can use this command tobuild an image from a Dockerfile. Usage: <code>docker build [OPTIONS]PATH | URL | -</code> .</p><p>Default Dockerfile path this command utilize to built an image is thecurrent working directory. You can also point out the path for dockerwith option <code>-f</code>.</p><p><code>-t</code> is also a useful option, you can name and choose atag in the <code>repository name:tag</code> format.</p><p>More details can be checked by <code>--help</code>. Let's focus onsomething that is more challenging about this process.</p><h3 id="context">Context</h3><hr /><p>As I've mentioned before, context is an important concept when webuilding a new image. Because docker use <code>client-server</code>structure to achieve the task. So the first job when docker is creatinga new image is confirm the content of context on the docker's server.More specifically, docker's client will copy all the files under the<code>PATH</code> you've sent to the docker's server. After that,docker's server will set it as "working directory".</p><p>All the local materials that you need to create a new image should beunder that <code>PATH</code>. For example, maybe in your Dockerfile,there exists some instruction such as <code>COPY</code>,<code>ADD</code>, which are using local files to create your docker'simage. Then, those file should be contained under the <code>PATH</code>you give.</p><h3 id="syntax-of-dockerfiles-basic-instruction">Syntax Of Dockerfile'sBasic Instruction</h3><hr /><ul><li>Rule of thumb: One instruction will build one layer. It's theguideline of building current layer.</li><li><code>FROM</code>: This point out the basic image we used fordocker.<ul><li>There are plenty of high-quality official images on dockerhub.</li><li>For server's image: <code>nginx, redis, mongo, mysql, httpd</code>and etc</li><li>More basic, you can also choose those OS image: <code>ubuntu,debian, centos</code></li><li>A special image <code>scratch</code>: actually, <code>Fromscratch</code> doesn't set any image as a base, this is a virtualconcept which represent no image base.</li></ul></li><li><code>RUN</code>: One of the most common used commands, it willautomatically run the commands after it during the creating imageprocess.<ul><li>There are two format: <em>shell</em> format and <em>exec</em>format</li><li><em>shell</em> format: <code>RUN <commands></code></li><li><em>exec</em> format: <code>RUN ["EXEC FILE", "OPT1", "OPT2",...]</code>, it looks like you're using a function.</li><li>Like I've mentioned in rule of thumb, one instruction is correspondto a layer. You need to think really carefully what commands should beadded after the <code>RUN</code>. Using <code>&&</code>,<code>||</code> , <code>;</code> and <code>\</code> , which is based onthe syntax feature of shell commands, is a good strategy to organize thecommands used to build image.</li><li><code>#</code> at the head of line is used to take notes.</li></ul></li><li><code>WORKDIR</code>: Appoint the working directory. The operationcontained by instructions, which are after <code>WORKDIR</code>instruction, will work at the working directory appointed by<code>WORKDIR</code>.<ul><li><ahref="https://yeasy.gitbook.io/docker_practice/image/dockerfile/workdir">ReferenceBlogs</a> points out an interesting fact by an example of improper using. Like I've said, every instruction will create a container, completethe operations, commit it and add it as a new layer. So when you usenext instruction, the working directory has nothing to do with the finalworking directory in the former container created by previousinstruction. It will only be initialized by <code>WORKDIR</code>.</li><li>If <code>WORKDIR</code> use relative path to set the later workingdirectory, the path it switched is related to the previous<code>WORKDIR</code>.</li></ul></li><li><code>CMD</code>:</li></ul><h3 id="example">Example</h3><hr /><p>Here is an example. My original reason of learning docker is try tolearn MIT 6.828 course. I wanna run this lab on my docker. So I createda dockerfile:</p><pre class="line-numbers language-dockerfile" data-language="dockerfile"><code class="language-dockerfile">FROM ubuntu:16.04RUN cp /etc/apt/sources.list /etc/apt/sources.list.bak \ && cat << _EOF_ >> /etc/apt/sources.list# My Source# TsingHuadeb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiversedeb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiversedeb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiversedeb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiversedeb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiversedeb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiversedeb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiversedeb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse# Alideb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiversedeb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiversedeb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiversedeb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiversedeb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiversedeb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiversedeb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiversedeb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiversedeb-src http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiversedeb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse_EOF_RUN apt-get update \ && apt-get --no-install-recommends install -y gcc make build-essential gdb gcc-multilib zlib1g-dev libglib2.0-dev libpixman-1-devRUN apt-get install -y git \ && git clone https://github.com/mit-pdos/6.828-qemu.git qemu \ && git clone https://pdos.csail.mit.edu/6.828/2018/jos.git mit_lab_os \ && apt-get remove -y gitRUN cd qemu \ && ./configure --disable-kvm --target-list="i386-softmmu x86_64-softmmu" \ && make \ && make install \ && cd .. \ && rm -rf qemuWORKDIR "/mit_lab_os"CMD ["bash"]<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>]]></content>
</entry>
<entry>
<title>Basic Conda Usage</title>
<link href="2020/basic-conda-usage.html"/>
<url>2020/basic-conda-usage.html</url>
<content type="html"><![CDATA[<h1 id="preface">Preface</h1><hr /><p>Long time no see. This bolg is written for record the knowledge I'velearnt during installing pytorch on windows. And I'll introduce somebasic knowledge about virtual environment, update and etc. Attentionthat these command or method is suit for windows, most of them will besuit for linux.</p><h1 id="check-the-pagkages-location">Check the pagkage's location</h1><hr /><p>It's really import for me to confirm where the file/program consumethe space of my disk. To check the python module package's path. You cantry this:</p><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> module<span class="token keyword">print</span><span class="token punctuation">(</span>module<span class="token punctuation">.</span>__file__<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>In general, ananconda's package will be installed follow this path<code>[your anaconda's directory]\Lib\site-packages</code></p><h1 id="virtual-environment">Virtual Environment</h1><hr /><p>Creating virtual environment aim to better run other's python programwhich may have difference among their version. What's more, programmingin a virtual environment will prevent the conflict among the globalpython module.</p><h2 id="create">Create</h2><hr /><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">conda create -n my_env_name python=3.6<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>On windows, run this command on cmd, and you will create a newvirtual environment with the name you replace at my_env_name.</p><h2 id="check-your-virtual-environment">Check your virtualenvironment</h2><hr /><p>Check your virtual environment's list in this way:</p><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">conda info --envs<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>or this way</p><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">conda env list<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><h2 id="activate-your-virtual-environment">Activate your VirtualEnvironment</h2><hr /><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">conda activate my_env_name<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>This command will help you enter into the correspond virtualenvironment. Normally, your terminal will look like this:</p><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">(my_env_name)[user_name](blablabal..)<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><h2 id="quit-the-current-virtual-environment">Quit the Current VirtualEnvironment</h2><hr /><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">conda deactivate<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>Some blogs resquire your env_name after the <em>deactivate</em>,however I've find some error about this method. And I find this solutionfrom an github issue. It suits my condition well.</p><h2 id="install-package-at-the-specific-virtual-environment">InstallPackage At The Specific Virtual Environment</h2><hr /><p>One of the virtual environment's advantages is that you can installyour package without considering the potential conflict among the globalpackage. You can install your package, and its effect domain will beconstrained into the specific virtual environment.</p><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">conda install -n my_env_name [package]<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>or you can activate the specific virtual environment and use<code>pip install</code> at current virtual environment.</p><h2 id="remove-the-virtual-environment">Remove the VirtualEnvironment</h2><hr /><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">conda remove -n my_env_name --all<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>This command will remove my_env_name virtual environment.</p><h2 id="remove-the-package-in-the-virtual-environment">Remove thePackage In the Virtual Environment</h2><hr /><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">conda remove -n my_env_name package<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p><strong>warning: still unsure this command</strong></p><h2 id="handle-the-global-package">Handle the global package</h2><hr /><p>Check the installed global package: <pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">conda list<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></p><p>Install global package: <pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">conda install package<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></p><h2 id="configure-yml-file-to-create-a-new-environment">Configure ymlfile to create a new environment</h2><hr /><p>Example yml file:</p><pre class="line-numbers language-yml" data-language="yml"><code class="language-yml">name: my_env_namedependencies:- python=3.6- pytest- keras- tqdm- Pillow- pip: - tensorflow<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>Warning: I'm not quite sure about this method.</p><p>Then you can create a new virtual environment by this command:</p><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">conda env create -f env_config.yml<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><h2 id="share-your-environment">Share Your Environment</h2><hr /><h3 id="export-your-environment-yml-file">Export your Environment ymlfile</h3><hr /><pre class="line-numbers language-none"><code class="language-none">conda env export > [environment yml file name].yml<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><h3 id="clone-your-environment">Clone your Environment</h3><hr /><pre class="line-numbers language-none"><code class="language-none">conda create -n [new environment name] --clone [existed environment name]<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><h2 id="whats-more">What's more</h2><hr /><p>For the command that you are not familiar with, you can check it'susage by add a <code>-h</code> at the end of the command.</p><h2 id="some-blogs">Some blogs</h2><p><ahref="%5Bhttp://course.killf.info/content/00-14%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90/md/shuju-conda.html#huanjing%5D(http://course.killf.info/content/00-14数据分析/md/shuju-conda.html#huanjing)">condausage</a></p>]]></content>
<categories>
<category> Programming </category>
</categories>
<tags>
<tag> Tools </tag>
</tags>
</entry>
<entry>
<title>My_Vimrc</title>
<link href="2020/my-vimrc.html"/>
<url>2020/my-vimrc.html</url>
<content type="html"><![CDATA[<p>Part of my vimrc are from example vimrc file.</p><pre class="line-numbers language-none"><code class="language-none">" An example for a vimrc file."" Maintainer: Bram Moolenaar <Bram@vim.org>" Last change: 2016 Mar 25"" To use it, copy it to" for Unix and OS/2: ~/.vimrc" for Amiga: s:.vimrc" for MS-DOS and Win32: $VIM\_vimrc" for OpenVMS: sys$login:.vimrc" When started as "evim", evim.vim will already have done these settings.if v:progname =~? "evim" finishendif" Use Vim settings, rather than Vi settings (much better!)." This must be first, because it changes other options as a side effect.set nocompatible" allow backspacing over everything in insert modeset backspace=indent,eol,start" if has("vms")" set nobackup " do not keep a backup file, use versions instead" else" set backup " keep a backup file (restore to previous version)" set undofile " keep an undo file (undo changes after closing)" endifset history=50 " keep 50 lines of command line historyset ruler " show the cursor position all the timeset showcmd " display incomplete commandsset incsearch " do incremental searching" For Win32 GUI: remove 't' flag from 'guioptions': no tearoff menu entries" let &guioptions = substitute(&guioptions, "t", "", "g")" Don't use Ex mode, use Q for formattingmap Q gq" CTRL-U in insert mode deletes a lot. Use CTRL-G u to first break undo," so that you can undo CTRL-U after inserting a line break.inoremap <C-U> <C-G>u<C-U>" In many terminal emulators the mouse works just fine, thus enable it.if has('mouse') set mouse=aendif" Switch syntax highlighting on, when the terminal has colors" Also switch on highlighting the last used search pattern.if &t_Co > 2 || has("gui_running") syntax on set hlsearchendif" Only do this part when compiled with support for autocommands.if has("autocmd") " Enable file type detection. " Use the default filetype settings, so that mail gets 'tw' set to 72, " 'cindent' is on in C files, etc. " Also load indent files, to automatically do language-dependent indenting. filetype plugin indent on " Put these in an autocmd group, so that we can delete them easily. augroup vimrcEx au! " For all text files set 'textwidth' to 78 characters. autocmd FileType text setlocal textwidth=78 " When editing a file, always jump to the last known cursor position. " Don't do it when the position is invalid or when inside an event handler " (happens when dropping a file on gvim). autocmd BufReadPost * \ if line("'\"") >= 1 && line("'\"") <= line("$") | \ exe "normal! g`\"" | \ endif augroup ENDelse set autoindent " always set autoindenting onendif " has("autocmd")" Convenient command to see the difference between the current buffer and the" file it was loaded from, thus the changes you made." Only define it when not defined already.if !exists(":DiffOrig") command DiffOrig vert new | set bt=nofile | r ++edit # | 0d_ | diffthis \ | wincmd p | diffthisendifif has('langmap') && exists('+langnoremap') " Prevent that the langmap option applies to characters that result from a " mapping. If unset (default), this may break plugins (but it's backward " compatible). set langnoremapendif" Add optional packages."" The matchit plugin makes the % command work better, but it is not backwards" compatible.packadd matchit" My vimrcfiletype plugin onfiletype indent onset shiftwidth=4set tabstop=4set expandtabset encoding=utf-8set softtabstop=4<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>]]></content>
<categories>
<category> Programming </category>
</categories>
<tags>
<tag> Operating System </tag>
</tags>
</entry>
<entry>
<title>Resources Collection</title>
<link href="2019/resources-collection.html"/>
<url>2019/resources-collection.html</url>
<content type="html"><![CDATA[<h1 id="overview">Overview</h1><hr /><p>This blog means to store some useful URL when I learn some knowledgeor some software.</p><h1 id="im-textbook-narcissism">I'm Textbook Narcissism</h1><hr /><p><a href="https://github.com/NewtonVan">GOD's github</a></p><h1 id="ai-resources">AI Resources</h1><hr /><p><ahref="https://github.com/exacity/deeplearningbook-chinese">ClassicalBook</a></p><p><ahref="https://github.com/ChristosChristofidis/awesome-deep-learning">awesome-deep-learning</a></p><h2 id="linear-algebra">Linear Algebra</h2><hr /><p><ahref="https://datawhalechina.github.io/leela-notes/#/">LeeML</a></p><p><ahref="https://nbviewer.jupyter.org/github/zlotus/notes-linear-algebra/blob/master/ReadMe.ipynb?flush_cache=true">MITNotes</a></p><h2 id="python-project">Python Project</h2><hr /><p><a href="http://lijin-thu.github.io/">lijin</a></p><p><a href="http://scipy-lectures.org/">Scipy Lecture Notes — Scipylecture notes</a></p><p><a href="https://pyda.apachecn.org/#/">numpy, panda, scipy,matplotlib</a></p><p><a href="https://github.com/apachecn/pyda-2e-zh">利用 Python进行数据分析</a></p><h2 id="course">Course</h2><hr /><p><ahref="https://smartjinyu.com/machine/learning/2017/12/23/my_cs231n_experience.html">我的深度学习入门之路(CS231n Spring 2017) - Smartjinyu's Blog</a></p><p><ahref="https://blog.csdn.net/abc_138/article/details/78608609">CS231n课堂笔记翻译PDF版本、吴恩达深度学习、机器学习笔记</a></p><p><ahref="https://www.tinymind.cn/collections/19">斯坦福CS231n课程笔记翻译</a></p><p><ahref="https://allmachinelearning.github.io/MachineLearning/">机器学习资源Machine learning Resources | MachineLearning</a></p><p><a href="https://cs231n.github.io/">CS231n official</a></p><p><a href="https://github.com/mbadry1/CS231n-2017-Summary">cs231nnotes</a></p><p><ahref="%5Bhttps://heroinlin.github.io/categories/CS231n%E8%AF%BE%E7%A8%8B%E7%AC%94%E8%AE%B0%E7%BF%BB%E8%AF%91/%5D(https://heroinlin.github.io/categories/CS231n课程笔记翻译/)">网友整理知乎翻译</a></p><h1 id="regex">Regex</h1><hr /><p><a href="https://github.com/cdoco/learn-regex-zh">Learn Regex</a></p><p><ahref="https://www.runoob.com/regexp/regexp-syntax.html">runoob</a></p><p><a href="https://regexper.com/documentation.html">UI</a></p><p><a href="https://regexper.com/documentation.html">UI another</a></p><h1 id="c-programming">C Programming</h1><hr /><h2 id="gdb">GDB</h2><hr /><p><ahref="https://blog.csdn.net/Linux7985/article/details/52399439">Simple</a></p><p><ahref="https://github.com/hellogcc/100-gdb-tips/blob/master/src/index.md">Trick</a></p><p><ahref="https://blog.csdn.net/longerzone/article/details/8867790">Script</a></p><p><ahref="https://balloonwj.blog.csdn.net/article/details/53333894">tui</a></p><p><ahref="https://blog.csdn.net/zhangjs0322/article/details/10152279">layout</a></p><h2 id="makefile">MakeFile</h2><hr /><p><ahref="https://seisman.github.io/how-to-write-makefile/index.html">ChenHao</a></p><h2 id="funny-project">Funny Project</h2><hr /><p><a href="http://github.com/neolay/HeartMaze">Heart Maze</a></p><p><ahref="http://patorjk.com/software/taag/#p=display&h=3&v=0&f=Big&t=SuperDuperDNS">Textto ASCII Art Generator (TAAG)</a></p><h2 id="windows">Windows</h2><hr /><p><ahref="https://blog.csdn.net/chenzhisi/article/details/27310497">VirutalKeyboard Code</a></p><h2 id="ai-gomoku">AI Gomoku</h2><hr /><p><a href="https://github.com/tashaxing/QtWuziqi">AB</a></p><p><ahref="https://github.com/lihongxun945/myblog/labels/五子棋AI教程第二版">AB</a></p><p><ahref="https://github.com/jimth001/my-Gobang-game-base-AI-algorithm">NeutralNetwork</a></p><p><a href="https://gomocup.org/links/">Competetion</a></p><p><ahref="https://www.aiexp.info/gomoku-renju-resources-an-overview.html">AB</a></p><h1 id="latex">LaTex</h1><hr /><p><a href="http://detexify.kirelabs.org/classify.html">OnlineTranslation</a></p><h1 id="sublime-text">Sublime Text</h1><hr /><p><a href="http://www.yalewoo.com/sublime_text_3_gcc.html">UseGCC</a></p><p><ahref="https://blog.csdn.net/one___thousand/article/details/53219751">Configuration</a></p><h1 id="vim">Vim</h1><hr /><p><a href="https://www.runoob.com/linux/linux-vim.html">RUNOOB</a></p><p><a href="https://www.w3cschool.cn/vim/">w3cschool</a></p><h1 id="about-console-program">About Console Program</h1><hr /><p><a href="https://blog.csdn.net/stude/article/details/7645056">movecursor</a></p><p><ahref="https://blog.csdn.net/bnb45/article/details/8042819">mouse</a></p><h1 id="shell-script">Shell Script</h1><hr /><p><a href="http://me.52fhy.com/shell-book/">52fhy</a></p><h1 id="algorithm">Algorithm</h1><hr /><p><a href="https://www.cnblogs.com/xiaotuzi/p/6491491.html">A*</a></p><p><ahref="http://inst.eecs.berkeley.edu/~cs61b/fa14/ta-materials/apps/ab_tree_practice/">ABSearchGame</a></p><p><ahref="http://web.cs.ucla.edu/~rosen/161/notes/alphabeta.html">ABSearch</a></p><p><a href="https://visualgo.net/en">SeeDataStructure</a></p><p><a href="https://www.cnblogs.com/nullzx/p">ACautomaton</a></p><p><a href="https://blog.csdn.net/xlxxcc/article">ACautomaton</a></p><p><a href="https://blog.csdn.net/joylnwang/article">ACautomaton</a></p><h1 id="vim-1">Vim</h1><hr /><p><ahref="https://www.cnblogs.com/yangjig/p/6014198.html">OverviewOfVim</a></p><h1 id="jujus-blog">JuJu's Blog</h1><hr /><p><a href="http://hzwer.com/">hzwer</a></p><p><a href="http://blog.miskcoo.com/">miskcoo</a></p><p><a href="http://blog.watashi.ws/">watashi</a></p><p><a href="https://kuangbin.github.io/">kuangbin</a></p><p><a href="https://www.jqhtml.com">UsefulResource</a></p><p><ahref="https://www.jqhtml.com/down/category/resources">UsefulResource</a></p><p><a href="https://oi.men.ci/">menci</a></p><p><a href="http://www.matrix67.com/blog/">Matrix67</a></p><p><a href="https://chaoxuprime.com/">ChaoXu</a></p><p><a href="http://cuitianyi.com/">TianYiCui</a></p><h1 id="idea">Idea</h1><hr /><p><ahref="https://ken.io/note/java-quickstart-sde">Installation</a></p><h1 id="build-blog">Build Blog</h1><hr /><p><ahref="https://closetao.github.io/blog/2016/05/14/%5BSublime%E6%8F%92%E4%BB%B6%5D%20OmniMarkupPreviewer%E4%BD%BF%E7%94%A8%E4%BB%8B%E7%BB%8D/">1</a></p><p><ahref="http://www.voycn.com/article/shiyonghexogithubdajianmianfeigerenbokexiangxijiaocheng">2</a></p><p><a href="http://shigaro.org/2018/02/15/Hexo-1/">3</a></p><p><a href="http://yearito.cn/posts/hexo-writing-skills.html">4</a></p><p><a href="https://juejin.im/entry/5a574864f265da3e3c6c1217">5</a></p><p><a href="http://yearito.cn/tags/Hexo/">6</a></p><p><a href="https://io-oi.me/tech/hexo-next-optimization.html">7</a></p><p><ahref="https://sogrey.github.io/article/Hexo-添加-Gitment-评论/">8</a></p><p><ahref="https://hexo-guide.readthedocs.io/zh_CN/latest/theme/%5BNexT%5D%E9%85%8D%E7%BD%AEMathJax.html">9</a></p><h1 id="linux-kernel">Linux Kernel</h1><hr /><p><a href="https://www.kernel.org/doc/html/latest/">Offical</a></p><p><a href="https://www.anquanke.com/post/id/172216">kernel pwn</a></p><p><ahref="http://mp.weixin.qq.com/mp/homepage?__biz=MzI3NzA5MzUxNA==&hid=2&sn=73cb26dd403cac563c9d661a8126c035&scene=18#wechat_redirect">LinuxKernel</a></p><p><ahref="http://www.oldlinux.org/download/CLK-5.0-WithCover.pdf">KernelAnalyse</a></p><h1 id="x_86-gcc-registers">X_86 GCC registers</h1><hr /><p><ahref="http://abcdxyzk.github.io/blog/2012/11/23/assembly-args/">Blog</a></p><p><ahref="https://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/">LinuxSys</a></p><p><a href="https://www.jianshu.com/p/0fac72022337">EFLAGS</a></p><h1 id="ctf">CTF</h1><hr /><h2 id="resources">Resources</h2><hr /><p><ahref="https://blog.flanker017.me/%e5%be%ae%e4%bf%a1%e5%85%ac%e4%bc%97%e5%8f%b7%ef%bc%8c%e6%ac%a2%e8%bf%8e%e5%85%b3%e6%b3%a8/">FlankerSky</a></p><p><ahref="https://www.zhihu.com/question/30505597/answer/72163029">Elf</a></p><p><a href="https://zhuanlan.zhihu.com/p/21685384">JiuCai</a></p><p><a href="http://www.vuln.cn/wooyundrops">WooYun</a></p><p><ahref="https://bbs.ichunqiu.com/forum.php?mod=collection&action=view&ctid=157">linuxpwn ichunqiu</a></p><p><a href="https://liveoverflow.com/">liveoverflow</a></p><p><ahref="https://space.bilibili.com/14500640?spm_id_from=333.788.b_765f7570696e666f.1">videoup</a></p><p><ahref="https://www.bilibili.com/video/av18860370?from=search&seid=11132194760548132748">liveoverflowvideo</a></p><p><ahref="https://space.bilibili.com/19216267/channel/detail?cid=73112">AnBili collection of liveoverflow</a></p><p><ahref="https://www.bilibili.com/video/av62214776?from=search&seid=9562473917120214345">Train</a></p><p><ahref="https://www.bilibili.com/video/av34865942?from=search&seid=16009644012258607039">BugBank Video</a></p><p><ahref="https://space.bilibili.com/179001398?from=search&seid=11032246572899612324">BugBank</a></p><p><a href="https://github.com/ctfs/">WP Source</a></p><h2 id="game">Game</h2><hr /><p><a href="https://www.xctf.org.cn/">XCTF</a></p><h2 id="assembly-language">Assembly Language</h2><hr /><p><a href="https://zhuanlan.zhihu.com/c_144694924">CNSS</a></p><p><ahref="https://github.com/justjavac/Programming-Alpha-To-Omega">DIY</a></p><h2 id="stack">Stack</h2><hr /><p><a href="https://zhuanlan.zhihu.com/p/25816426">LongYard</a></p><p><a href="https://zhuanlan.zhihu.com/p/25892385">LongYard1</a></p><p><a href="https://zhuanlan.zhihu.com/p/23487280">ROP_X86</a></p><p><a href="https://zhuanlan.zhihu.com/p/23537552">ROP_X64</a></p><p><a href="https://www.anquanke.com/post/id/85129">DynELF</a></p><h2 id="heap">Heap</h2><hr /><p><ahref="https://github.com/lattera/glibc/tree/master/malloc">Ptmalloc</a></p><p><a href="https://zhuanlan.zhihu.com/p/24753861">Ali-Heap</a></p><p><a href="https://zhuanlan.zhihu.com/p/24790164">Ali-Heap1</a></p><p><ahref="https://paper.seebug.org/papers/Archive/refs/heap/glibc%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86ptmalloc%E6%BA%90%E4%BB%A3%E7%A0%81%E5%88%86%E6%9E%90.pdf">HuaTing</a></p><h2 id="vmp">VMP</h2><hr /><p><ahref="https://www.52pojie.cn/forum.php?mod=viewthread&tid=713219">VMProtectEasy</a></p><h2 id="wiki">Wiki</h2><hr /><p><ahref="https://ctf-wiki.github.io/ctf-wiki/pwn/linux/stackoverflow/stack_intro/">CTF-wiki</a></p><p><ahref="https://wiki.x10sec.org/pwn/stackoverflow/basic_rop/">ctf-wiki</a></p><h2 id="tools">Tools</h2><hr /><h3 id="pwntools">pwntools</h3><hr /><p><ahref="https://pwntools-docs-zh.readthedocs.io/zh_CN/latest/install.html">pwntools</a></p><p><a href="https://bbs.pediy.com/thread-247217.htm">KanXue</a></p><p><a href="https://www.anquanke.com/post/id/85138">Safety</a></p><h3 id="ce-tutorial">CE Tutorial</h3><hr /><p><ahref="https://www.cnblogs.com/LyShark/p/10799926.html">blog</a></p><p><a href="https://www.bilibili.com/video/av47193685/">video</a></p><h3 id="ida">IDA</h3><hr /><p><ahref="https://www.cnblogs.com/xuan52rock/p/11005976.html">IDA_BASIC_TUTORIAL</a></p><p><ahref="https://blog.csdn.net/fjh658/article/details/52268907">IDA_KEYPATCH</a></p><p><ahref="https://www.52pojie.cn/thread-618234-1-1.html">IDA_KEYPATCH1</a></p><p><a href="https://xz.aliyun.com/t/4658">IDA_KEYPATCH2</a></p><h3 id="decode">Decode</h3><hr /><p><ahref="https://blog.csdn.net/qq_41638851/article/details/100526839">DecodeURL</a></p><p><ahref="https://blog.csdn.net/xiaoi123/article/details/79077111">DecodeURL1</a></p><p><ahref="https://blog.csdn.net/qq_40837276/article/details/83080460">Summary</a></p><p><ahref="https://blog.csdn.net/qq_40836553/article/details/79383488">Summary1</a></p><h2 id="mask">Mask</h2><hr /><p><ahref="https://www.zhihu.com/question/31008989/answer/606863254">TemporaryMail Phone</a></p><p><ahref="https://www.zhihu.com/question/31008989/answer/210853403">Shodanand Mask</a></p><h2 id="eye">Eye</h2><hr /><p><ahref="https://www.zhihu.com/question/31008989/answer/606843201">Eye</a></p><h1 id="acm">ACM</h1><hr /><h2 id="problem">Problem</h2><p><a href="http://www.pythontip.com/acm/problemCategory#POJ">ProblemClassification</a></p><p><ahref="https://vjudge.net/contest#category=all&running=0&title=&owner=kuangbin">KuangBin</a></p><p><ahref="https://vjudge.net/contest/#category=all&running=0&title=&owner=hzwer">hzwer</a></p><h2 id="knowledge">Knowledge</h2><p><ahref="https://github.com/enkerewpo/OI-Public-Library/tree/master/">AmazingSource!!!</a></p><p><a href="https://www.luogu.com.cn/discuss/show/48491">LuoguNewspaper</a></p><h2 id="video-class">Video Class</h2><hr /><p><a href="https://space.bilibili.com/7836741/">AcWing</a></p><p><a href="https://space.bilibili.com/120174936/">AgOH</a></p><p><a href="https://space.bilibili.com/133821921/">Happy 233</a></p><p><a href="https://space.bilibili.com/7711573/">UESTC</a></p><p><a href="https://space.bilibili.com/611212/">qscqesze</a></p><p><a href="https://space.bilibili.com/8198319/">a soldier</a></p><h2 id="wiki-1">Wiki</h2><hr /><p><a href="https://oi-wiki.org/">OI-Wiki</a></p><h1 id="how-to-fly">How to Fly</h1><hr /><h2 id="v2r4y">v2r4y</h2><hr /><p><ahref="https://github.com/233boy/v2ray/wiki/V2Ray搭建详细图文教程">Main</a></p><p><ahref="https://github.com/233boy/v2ray/wiki/V2Ray搭建详细图文教程">Configure</a></p><p><a href="https://mianao.info/2018/04/23/轻松搭建和配置v2ray">xshellcommand</a></p><p><a href="https://umrhe.com/v2ray.html">Mr.He</a></p><p><a href="https://tlanyan.me/v2ray-tutorial/">tlanyan</a></p><p><a href="https://tlanyan.me/v2ray-traffic-mask/">traficmask</a></p><p><a href="https://www.hijk.pw/">网络跳越</a></p><h2 id="v2r4yn">v2r4yN</h2><hr /><p><a href="https://www.lookcos.cn/?p=416">1</a></p><h2 id="ssssr">SS/SSR</h2><hr /><p><ahref="https://www.textarea.com/Tomaot/2019-nian-zuixin-vultr-yijian-dajian-ss-ssr-jiaoben-xiaobai-kan-le-ye-hui-1173/">TeadySun</a></p><p><a href="https://www.vultrblog.com/vultr-ss.html">ConfigurationsDetails</a></p><h1 id="anaconda">Anaconda</h1><hr /><p><ahref="https://www.cnblogs.com/zhusleep/p/5616099.html">refresh</a></p><p><a href="https://www.jianshu.com/p/aff9ab3aa89d">refresh</a></p><h1 id="rasberry-pi">Rasberry Pi</h1><hr /><p><ahref="https://github.com/1021683053/awesome-raspberry-pi-zh">resource</a></p><p><ahref="https://my.oschina.net/u/877567/blog/422820">LINUX终端下配置WPA2加密无线网络- 行者深蓝的个人空间 - OSCHINA</a></p><p><ahref="https://shumeipai.nxez.com/2017/09/13/raspberry-pi-network-configuration-before-boot.html">无屏幕和键盘配置树莓派WiFi和SSH| 树莓派实验室</a></p><h1 id="sql">SQL</h1><hr /><p><a href="http://www.sqlfiddle.com/">SQL FIDDLE</a></p><p><a href="https://sqlbolt.com/">SQL BOLT</a></p><h1 id="the-economist">The Economist</h1><hr /><p>Really thanks to this fine people who shared the free resources</p><p><a href="https://github.com/nailperry-zd/The-Economist">GithubRsc</a></p><p><a href="https://github.com/fredliu168/TheEconomist2020">Github MdRsc</a></p><p><a href="https://getmyte.com/">Cool Blog!!!</a> (Currently, thereexists some problem)</p><p><ahref="https://freemagazinepdf.com/?s=the+economist">freemagazine</a></p><p><a href="http://magzdb.org/makelist?t=The%20economist">Response fromLibGen</a></p><p><ahref="https://magazinelib.com/?s=The+Economist">magazinelib</a></p><p><a href="https://vk.com/wsnws/TheEconomist">vk</a></p><h1 id="goldendict">GoldenDict</h1><hr /><p><ahref="https://freemdict.com/2018/11/10/freemdict资料库点此下载/">mdict词库下载 各种学习资料 | FreeMdict</a></p><h1 id="web">Web</h1><hr /><h2 id="front-end">Front End</h2><p><ahref="https://coolshell.cn/articles/10590.html">二维码的生成细节和原理 || 酷 壳 - CoolShell</a></p><h2 id="browser-tools">Browser Tools</h2><hr /><p><a href="https://greasyfork.org/zh-CN">Greasy Fork -安全、实用的用户脚本大全</a></p><h1 id="os">OS</h1><hr /><p><a href="https://zhuanlan.zhihu.com/p/74028717">zhihuMIT6.838</a></p><p><a href="https://markrepo.github.io/category/#/kernel">link, load andlibrary</a></p>]]></content>
<categories>
<category> Misc </category>
</categories>
</entry>
<entry>
<title>Entropy Theory Of C.E.Shannon</title>
<link href="2019/entropy-theory-of-c-e-shannon.html"/>
<url>2019/entropy-theory-of-c-e-shannon.html</url>
<content type="html"><![CDATA[<h1 id="abstract">Abstract</h1><hr /><p>This article is about C.E.Shannon's famous article <em>A MathematicalTheory of Communication</em>. Inspired by what I learnt after thephysics and Probability and Mathematical Statistics class, I'm going toreintroduce this great concept, and make the important prove inferred bymyself. What's more, I will give my personal comprehension on concept ofthe entropy both phtsics and the information theory, by combing theknowledge I learnt by myself after that two class.</p><p><strong>Keyword</strong>: <strong>C.E.Shannon's Entropy</strong><strong>Boltzmann's Entropy</strong> <strong>Uncertainty</strong><strong>Choice</strong></p><h1 id="communication-system1">Communication System<ahref="#refer-anchor"><sup>1</sup></a></h1><hr /><p>It consists of essentially five parts:</p><ul><li>An information source which produces a message or sequence ofmessages to be communicated to the receiving terminal.</li><li>A transmitter which operates on the message in some way to produce asignal suitable for transmission over the channel.</li><li>The channel is merely the medium used to transmit the signal fromtransmitter to receiver.</li><li>The receiver ordinarily performs the inverse operation of that doneby the transmitter, reconstructing the message from the signal.</li><li>The destination is the person (or thing) for whom the message isintended.</li></ul><p>We may roughly classify communication systems into three maincategories: discrete, continuous and mix.</p><h1 id="choice-uncertainty-and-entropy1">Choice, Uncertainty AndEntropy<a href="#refer-anchor"><sup>1</sup></a></h1><hr /><p>In Shannon's article, Shannon has came up with an important questionthat how to measure how much information is "produced" by such aprocess.</p><p>Suppose we have a set of possible events whose probabilities ofoccurrence are <span class="math inline">\(p_1, p_2, \dotsc,p_n\)</span>. Now we focus mainly on how to measure how much "choice"which can help us concern which event will occur, is involved in theselection of the event or how "uncertain" we are of the outcome. Themore choices we have to make, the more uncertainty we can say about thisevent.</p><p>We can define a function <span class="math inline">\(H(p_1, p_2,\dotsc, p_n)\)</span> , it is reasonable to make these contraints forthis function.</p><ul><li>H should be continuous in the <spanclass="math inline">\(p_i\)</span></li><li>If all the <span class="math inline">\(p_i\)</span> are equal, <spanclass="math inline">\(p_i= \frac{1}{n}\)</span>, then <spanclass="math inline">\(H\)</span> should be a monotonic increasingfunction of n. With equally likely events there is more choice, oruncertainty, when there are more possible events.</li><li>If a choice be broken down into two successive choices, the orginal<span class="math inline">\(H\)</span> should be the weighted sum of theindividual values of <span class="math inline">\(H\)</span>. This can beequivalent described as the content I have learnt in the probabilitytheory class: <span class="math display">\[H(p_1, \dotsc , p_n)=H(p_1+p_2+\dotsc +p_M, p_{M+1},\dotsc ,p_n)+(p_1+p_2+\dotsc +p_n)\cdotH(q_1, q_2, \dotsc q_M),\quad q_i= \frac{p_i}{p_1+\dotsc +p_M}\]</span>As you can see, this is strongly connected with the concept ofconditional probability.</li></ul><p>We can prove that <span class="math display">\[H=-K \sum_{i=1}^{n} p_{i} \log p_{i}\]</span> is the only form that can suit these three requirements.Before I show the provement, let me explain why this formula form isintuitively correct.</p><h2 id="intuitively-explaination">Intuitively Explaination</h2><hr /><ul><li>If something has lower probability to happen, but it happens, thenit bring more "surprise" for you. In the expression of entropy, we cansee that the less the probability is, the more it contributes to theentropy.</li><li>The entropy of the whole system should be the weighted sum of allthe system's event, because to measure the uncertainty, in another way,to sure how much information it contains, we should make choice toconfirm. But not every choice is indispensable, whether to make a choicedepends on its possibility.</li></ul><h2 id="prove">Prove</h2><hr /><h3 id="the-random-variables-are-uniformly-distributed">The randomvariables are uniformly distributed</h3><hr /><p>With property 3, we can learn this equation: <spanclass="math display">\[\begin{equation}\begin{aligned} g(M N) &=f\left(\frac{1}{M}, \ldots,\frac{1}{M}\right)+\sum_{i}^{M} \frac{1}{M} f\left(\frac{1}{N}, \ldots,\frac{1}{N}\right) \\ &=g(M)+g(N) \end{aligned}\end{equation}\]</span></p><p>With property 2 and last conclusion, we get this:</p><p>if <span class="math inline">\(s^m\leq t^n\leq s^{m+1}\)</span>, then<span class="math inline">\(mg(s)\leq ng(t)\leq (m+1)g(s)\)</span></p><p>So what we can finally get:</p><p><span class="math inline">\(\frac{m}{n}\leq \frac{g(t)}{g(s)}\leq\frac{m+1}{n}\)</span>.</p><p>Now, let's minus <span class="math inline">\(\frac{m}{n}\)</span> onthe both side of the second inequality.</p><p><span class="math display">\[\begin{equation}\left|\frac{m}{n}-\frac{g(t)}{g(s)}\right|<\frac{1}{n}\end{equation}\]</span></p><p>And for the same <span class="math inline">\(s, m, t, n\)</span>,$$function has the same property. <spanclass="math display">\[\begin{equation}\left|\frac{m}{n}-\frac{\log(t)}{\log(s)}\right|<\frac{1}{n}\end{equation}\]</span></p><p>Because of <span class="math inline">\(\left| a-b\right| \leq \left|a\right|+\left| b\right|\)</span>:</p><p><span class="math display">\[\begin{equation}\left|\frac{\log(t)}{\log(s)}-\frac{g(t)}{g(s)}\right|<\frac{2}{n}\end{equation}\]</span></p><p>Let <span class="math inline">\(n\)</span> is big enough. <spanclass="math display">\[\frac{g(t)}{g(s)} \rightarrow \frac{\log t}{\log s}\]</span> <span class="math display">\[\begin{equation}g(t)= C\log(t)\end{equation}\]</span></p><h3 id="general-situation">General Situation</h3><hr /><p>Let's first think about rational possibility value, and irrationalvalue can be easily solved with take the limit of the rational numbersequence which is a common trick in analysis theory.</p><p>Let <span class="math inline">\(P(n)= \frac{m_n}{\sum_{i=1}^{N} m_i}=\frac{m_n}{M}\)</span>, <span class="math inline">\(g(M)=f(\frac{1}{M},\dotsc , \frac{1}{M})\)</span></p><p>We can get: <span class="math display">\[\begin{equation}\begin{aligned} g(M) &=f \left(\underbrace{\frac{1}{M},\ldots,}_{m_{1} } \dotsc ,\underbrace{\ldots, \frac{1}{M}}_{m_{N}}\right) \\ &=f\left(P_{1}, P_{2}, \ldotsP_{N}\right)+\sum_{i=1}^{N} \frac{m_{i}}{M} f\left(\frac{1}{m_{i}},\ldots, \frac{1}{m_{i}}\right) \\ &=f\left(P_{1}, P_{2}, \ldotsP_{N}\right)+\sum_{i=1}^{N} \frac{m_{i}}{M} g\left(m_{i}\right)\end{aligned}\end{equation}\]</span></p><p><span class="math display">\[\begin{equation}\begin{aligned}f(P_1, P_2, \dotsc , P_N)&= g(M)-\sum_{i=1}^{N}P_ig(m_i)\\&= C\log(M)-\sum_{i=1}^{N}P_iC\log(m_i)\\&= -C\sum_{i=1}^{N}P_i(\log(m_i)-\log(M))\\&= -C\sum_{i=1}^{N}P_i(\log(\frac{m_i}{M}))\\&= -C\sum_{i=1}^{N}P_i\log{P_i}\end{aligned}\end{equation}\]</span></p><h1id="personal-comprehension-combined-with-the-concept-of-entropy-in-the-physics">PersonalComprehension Combined With The Concept Of Entropy In The Physics</h1><hr /><p>After taking the class of statistic mechanics, I have learnt theconcept of Boltzmann's entropy theory. Shannon's entropy is differentfrom the entropy in physics. This idea is advised by John von Neumann.Because their form are similar and lots of similarity about theirproperty. Inspired by the concept of the entropy concept in the physics,I consider another set of constraints to interpret entropy which isequivalent to the Shannon's definition.</p><ul><li>Physics's entropy represent the degree of chaos in microcosmicmeaning. In the Boltzmann's equation <span class="math inline">\(S=\ln(\Omega )\)</span>. <span class="math inline">\(\Omega\)</span>represents the number of microstates. And considering the problem in theinformation system, to confirm an event with property <spanclass="math inline">\(p\)</span>, we discuss the meaning of <spanclass="math inline">\(\frac{1}{p}\)</span>. We can difine this as<strong>space complexity of target search</strong>. The bigger <spanclass="math inline">\(\frac{1}{p}\)</span>, the bigger the spacecomplexity of target search is. The larger the complexity, the moreinformation we need to confirm this event. This is just the same as the<span class="math inline">\(\Omega\)</span>. So we analogy the <spanclass="math inline">\(\Omega\)</span> to comprehend <spanclass="math inline">\(\frac{1}{p}\)</span>. So for the single event, wehave <span class="math inline">\(H(p)= C\log(\frac{1}{p})\)</span></li><li>The whole system consist of all these events, every single eventscontibutes its own entropy should be weighted by its probability <spanclass="math inline">\(p\)</span>. This is a natural thought.</li></ul><p>So the finally definition should be <span class="math inline">\(H=-C\sum_{i=1}^{N}P_i\log{P_i}\)</span></p><div id="refer-anchor"></div><h1 id="reference">Reference</h1><hr /><p>[1] <a href="https://dl.acm.org/citation.cfm?id=584093">C.E.Shannon,AMathematical Theory of Communication, ACM, v.5, January 2001</a></p>]]></content>
<categories>
<category> Maths </category>
</categories>
<tags>
<tag> Math and Basic knowledge of ML </tag>
</tags>
</entry>
<entry>
<title>virtual machine</title>
<link href="2019/virtual-machine.html"/>
<url>2019/virtual-machine.html</url>
<content type="html"><![CDATA[<h1 id="overview">Overview</h1><hr /><p>"When you say, "I wrote a program that crashed windows", people juststare at you blankly and say, "hey, I got those with the system, forfree"" By <strong>Linus</strong></p><p>Fxxk you windows!!!</p><p>When windows updates, the world goes to crazy. And I have toreunistall the virtual machine to install linux.</p><h1 id="download-vmware">Download Vmware</h1><hr /><p>Remember that, always use the register code you can find in theinternet rather than the software that has been hacked.</p><h1 id="create-the-machine">Create The Machine</h1><hr /><p>There are several tips that needed to be attentioned. You'd betterdevide your space of the disk by your hand.</p><p>Always remember that, the default settings always have trouble, soyou'd better handle it yourself.</p><table><thead><tr class="header"><th>Region</th><th>Space</th><th>Sort</th><th>Type</th></tr></thead><tbody><tr class="odd"><td>/.</td><td>25%-35% space</td><td>Main</td><td>ext4</td></tr><tr class="even"><td>/boot</td><td>200M</td><td>Main</td><td>ext4</td></tr><tr class="odd"><td>/home</td><td>remain space</td><td>Logical</td><td>ext4</td></tr><tr class="even"><td>/swap</td><td>6G</td><td>Logical</td><td>swap space</td></tr></tbody></table><h1 id="set-the-settings">Set The Settings</h1><hr /><p>Click edit your settings.</p><p>The processor choose 4.</p><p>CD/DVD SATA choose the iso you download.</p><p>The Internet choose the NAT.</p><h1 id="install-vm-tools">Install VM-Tools</h1><hr /><p>Click the "virtual machine" button on the side bar. Install theVM-Tools and restart your linux.</p><h1 id="make-it-accept-32-bit-program">Make It Accept 32-bitprogram</h1><hr /><p>Unlike windows, it will not accept 32-bit program if it is a 64-bitlinux. Maybe this is the only advantage of windows.</p><p>Just implement these commands in terminal.</p><pre class="line-numbers language-none"><code class="language-none">sudo apt-get update sudo apt-get install libc6:i386 -ysudo apt-get install lib32ncurses5 lib32z1 -ysudo apt-get install gcc-multilib -y<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><p>Everything is OJBK.</p><h1 id="ida-dynamic-debug">IDA Dynamic Debug</h1><hr /><p><a href="https://bbs.pediy.com/thread-247830-1.html">TUTORIAL</a></p><h1 id="install-qemu-to-fit-mips">Install qemu To Fit MIPS</h1><hr /><p><code>sudo apt-get install qemu</code></p><p>To run the big port use command: <code>qemu-mips <filename></code></p><p>Run the small port: <code>qemu-mipsel <file name></code></p><h1 id="clean-your-boot">Clean Your boot</h1><hr /><p>After your linux kernel has been updated, your space for /boot may benarrow. Here are the blogs which introduce the method to clean yourspace.</p><p><ahref="https://www.linuxidc.com/Linux/2017-12/149655.htm">Linux</a></p><p><ahref="https://www.cnblogs.com/carle-09/p/11363020.html">cnblogs</a></p><p><ahref="https://my.oschina.net/u/931493/blog/512227">COSCHINA</a></p>]]></content>
<categories>
<category> Programming </category>
</categories>
<tags>
<tag> CTF </tag>
</tags>
</entry>
<entry>
<title>Let Hexo Next Support Latex</title>
<link href="2019/let-hexo-next-support-latex.html"/>
<url>2019/let-hexo-next-support-latex.html</url>
<content type="html"><![CDATA[<h1 id="overview">Overview</h1><hr /><p>This blog is to record one important function newly opened by myblog. It trouble me for a long time to insert math equation.</p><h1 id="open-mathjax-in-next-theme">Open MathJax In Next Theme</h1><hr /><p>Next theme has already prepared the mathjax for supporting latex. Goto this file: <code>D:\MyBlog\Blog\themes\next\_config.yml</code> andrewrite this code.</p><pre class="line-numbers language-css" data-language="css"><code class="language-css"># ---------------------------------------------------------------# Third Party Services Settings# ---------------------------------------------------------------# Math Equations Render Support<span class="token property">math</span><span class="token punctuation">:</span> <span class="token property">enable</span><span class="token punctuation">:</span> true # Default <span class="token punctuation">(</span>true<span class="token punctuation">)</span> will load mathjax / katex script on demand. # That is it only render those page which has `<span class="token property">mathjax</span><span class="token punctuation">:</span> true` in Front Matter. # If you set it to false<span class="token punctuation">,</span> it will load mathjax / katex srcipt EVERY PAGE. <span class="token property">per_page</span><span class="token punctuation">:</span> true <span class="token property">engine</span><span class="token punctuation">:</span> mathjax #<span class="token property">engine</span><span class="token punctuation">:</span> katex # hexo-rendering-pandoc <span class="token punctuation">(</span>or hexo-renderer-kramed<span class="token punctuation">)</span> needed to full MathJax support. <span class="token property">mathjax</span><span class="token punctuation">:</span> # Use 2.7.1 as default<span class="token punctuation">,</span> jsdelivr as default CDN<span class="token punctuation">,</span> works everywhere even in China <span class="token property">cdn</span><span class="token punctuation">:</span> //cdn.jsdelivr.net/npm/mathjax@2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML # For direct link to MathJax.js with CloudFlare CDN <span class="token punctuation">(</span>cdnjs.cloudflare.com<span class="token punctuation">)</span> #<span class="token property">cdn</span><span class="token punctuation">:</span> //cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML # <span class="token property">See</span><span class="token punctuation">:</span> <span class="token property">https</span><span class="token punctuation">:</span>//mhchem.github.io/MathJax-mhchem/ #<span class="token property">mhchem</span><span class="token punctuation">:</span> //cdn.jsdelivr.net/npm/mathjax-mhchem@3 #<span class="token property">mhchem</span><span class="token punctuation">:</span> //cdnjs.cloudflare.com/ajax/libs/mathjax-mhchem/3.3.0 # hexo-renderer-markdown-it-plus <span class="token punctuation">(</span>or hexo-renderer-markdown-it with markdown-it-katex plugin<span class="token punctuation">)</span> needed to full Katex support. <span class="token property">katex</span><span class="token punctuation">:</span> # Use 0.7.1 as default<span class="token punctuation">,</span> jsdelivr as default CDN<span class="token punctuation">,</span> works everywhere even in China <span class="token property">cdn</span><span class="token punctuation">:</span> //cdn.jsdelivr.net/npm/katex@0.7.1/dist/katex.min.css # CDNJS<span class="token punctuation">,</span> provided by cloudflare<span class="token punctuation">,</span> maybe the best CDN<span class="token punctuation">,</span> but not works in China #<span class="token property">cdn</span><span class="token punctuation">:</span> //cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.css <span class="token property">copy_tex</span><span class="token punctuation">:</span> # <span class="token property">See</span><span class="token punctuation">:</span> <span class="token property">https</span><span class="token punctuation">:</span>//github.com/KaTeX/KaTeX/tree/master/contrib/copy-tex <span class="token property">enable</span><span class="token punctuation">:</span> false <span class="token property">copy_tex_js</span><span class="token punctuation">:</span> //cdn.jsdelivr.net/npm/katex@0/dist/contrib/copy-tex.min.js <span class="token property">copy_tex_css</span><span class="token punctuation">:</span> //cdn.jsdelivr.net/npm/katex@0/dist/contrib/copy-tex.min.css<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>Make math <strong>enable</strong> and <strong>per_page</strong>to betrue.</p><h1 id="important-shell-operation">Important Shell Operation</h1><hr /><p>I had tryed to support latex for millions of time. But it doesn'twork just because this stupid command that most blogs don't mention.</p><p><code>hexo clean</code></p><p>Then <code>hexo d -g</code> can see the beautiful equation.</p><p>To solve some conflicts about the syntax between Latex and MarkDown.We use the pandoc plugin.</p><p><code>npm un hexo-renderer-marked --save</code> Uninstall the markedrenderer.</p><p><code>npm i hexo-renderer-pandoc --save</code> Install the pandocrenderer.</p><p><ahref="https://hexo-guide.readthedocs.io/zh_CN/latest/theme/%5BNexT%5D%E9%85%8D%E7%BD%AEMathJax.html">OfficialGuidance</a></p>]]></content>
<categories>
<category> Programming </category>
</categories>
<tags>
<tag> Blog </tag>
<tag> Front-end </tag>
</tags>
</entry>
<entry>
<title>Data Structure</title>
<link href="2019/data-structure.html"/>
<url>2019/data-structure.html</url>
<content type="html"><![CDATA[<h1 id="overview">Overview</h1><hr /><p>This is the reading note about the 3 books of data structure. Morespecifically, the main idea is from the <em>Data structures andAlgorithm Analysis in C</em> and the other 2 books of TsingHua Univesityconsists of the supplementary or regeneralize some operation inthe<em>Data structures and Algorithm Analysis in C</em> in another way.And I will add more details about these data structures in the otherblogs.</p><p>This is just a blog about some basic data structure. I'll write ablog about the advance data structure in other blogs.</p><h1 id="list-stack-and-queue">List, Stack and Queue</h1><hr /><h2 id="linked-list">Linked List</h2><hr /><p>To reduce the cost of deletion and insertion, we avoid storing thedata sequentially. Linked list is the data structure that achieve thisgoal. Linded list consist of the memory cells that doesn't need to beadjacent in memory.</p><h3 id="some-preparation-for-tsinghuas-books">Some Preparation ForTsingHua's Books</h3><hr /><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">OK</span> <span class="token expression"><span class="token number">1</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">ERROR</span> <span class="token expression"><span class="token number">0</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">TRUE</span> <span class="token expression"><span class="token number">1</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">FALSE</span> <span class="token expression"><span class="token number">0</span></span></span><span class="token keyword">typedef</span> <span class="token keyword">int</span> Status<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="programming-details">Programming details</h3><hr /><ul><li>There is no obvious way to insert at the front of the list from thedefinitions given.</li><li>Deleting from the front of the list is a special case, because itchanges the start of the list.</li><li>Deletion in general.</li></ul><p>To solve these three problems, we create a sentinal node calledheader or dummy node.</p><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifndef</span> <span class="token expression">_List_H</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">_List_H</span></span><span class="token keyword">struct</span> <span class="token class-name">Node</span><span class="token punctuation">;</span><span class="token keyword">typedef</span> <span class="token keyword">struct</span> <span class="token class-name">Node</span> <span class="token operator">*</span>PtrToNode<span class="token punctuation">;</span><span class="token keyword">typedef</span> PtrToNode List<span class="token punctuation">;</span><span class="token keyword">typedef</span> PtrToNode Position<span class="token punctuation">;</span>List <span class="token function">MakeEmpty</span><span class="token punctuation">(</span>List L<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">IsEmpty</span><span class="token punctuation">(</span>List L<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">IsLast</span><span class="token punctuation">(</span>Position P<span class="token punctuation">,</span> List L<span class="token punctuation">)</span><span class="token punctuation">;</span>Position <span class="token function">Find</span><span class="token punctuation">(</span>ElementType X<span class="token punctuation">,</span> List L<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">void</span> <span class="token function">Delete</span><span class="token punctuation">(</span>ElementType X<span class="token punctuation">,</span> List L<span class="token punctuation">)</span><span class="token punctuation">;</span>Position <span class="token function">FindPrevious</span><span class="token punctuation">(</span>ElementType X<span class="token punctuation">,</span> List L<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">void</span> <span class="token function">Insert</span><span class="token punctuation">(</span>ElementType P<span class="token punctuation">,</span> List L<span class="token punctuation">,</span> Position P<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">void</span> <span class="token function">DeleteList</span><span class="token punctuation">(</span>List L<span class="token punctuation">)</span><span class="token punctuation">;</span>Position <span class="token function">Header</span><span class="token punctuation">(</span>List L<span class="token punctuation">)</span><span class="token punctuation">;</span>Position <span class="token function">First</span><span class="token punctuation">(</span>List L<span class="token punctuation">)</span><span class="token punctuation">;</span>Position <span class="token function">Advance</span><span class="token punctuation">(</span>Position P<span class="token punctuation">)</span><span class="token punctuation">;</span>ElementType <span class="token function">Retrieve</span><span class="token punctuation">(</span>Position P<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span><span class="token comment">/*Place in the implementation file*/</span><span class="token keyword">struct</span> <span class="token class-name">Node</span><span class="token punctuation">{</span> ElementType Element<span class="token punctuation">;</span> Position Next<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>The function to test whether the linked list is empty.<pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token comment">/*Return true if the list is empty*/</span><span class="token keyword">int</span> <span class="token function">IsEmpty</span><span class="token punctuation">(</span>List L<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token constant">NULL</span> <span class="token operator">==</span> L<span class="token operator">-></span>Next<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre></p><p>Test whether the current position is the end of the list.<pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token comment">/*Return true if P is the last position in list L*/</span><span class="token comment">/*Parameter is unused in this implementation*/</span><span class="token keyword">int</span> <span class="token function">IsLast</span><span class="token punctuation">(</span>Position P<span class="token punctuation">,</span> List L<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token constant">NULL</span> <span class="token operator">==</span> P<span class="token operator">-></span>Next<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></p><p>We must avoid use recursion to achieve Find. <pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token comment">/*Return Position of X in L; NULL if not find*/</span>Position <span class="token function">Find</span><span class="token punctuation">(</span>ElementType X<span class="token punctuation">,</span> List L<span class="token punctuation">)</span><span class="token punctuation">{</span> Position P<span class="token punctuation">;</span> P <span class="token operator">=</span> L<span class="token operator">-></span>Next<span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token constant">NULL</span> <span class="token operator">!=</span> P <span class="token operator">&&</span> X <span class="token operator">!=</span> P<span class="token operator">-></span>Element<span class="token punctuation">)</span><span class="token punctuation">{</span> P <span class="token operator">=</span> P<span class="token operator">-></span>Next<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> P<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></p><p>Our fourth routine is to delete the first occurance of X from L.Addtionally, we will also achieve the function FindPrevious.<pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token comment">/*Delete first occurance of X from a list */</span><span class="token comment">/*Assume use of a header node*/</span><span class="token keyword">void</span> <span class="token function">Delete</span><span class="token punctuation">(</span>ElementType X<span class="token punctuation">,</span> List L<span class="token punctuation">)</span><span class="token punctuation">{</span> Position P<span class="token punctuation">,</span> TmpCell<span class="token punctuation">;</span> P <span class="token operator">=</span> <span class="token function">FindPrevious</span><span class="token punctuation">(</span>P<span class="token punctuation">,</span> L<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">IsLast</span><span class="token punctuation">(</span>P<span class="token punctuation">,</span> L<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> TmpCell <span class="token operator">=</span> P<span class="token operator">-></span>Next<span class="token punctuation">;</span> P<span class="token operator">-></span>Next <span class="token operator">=</span> TmpCell<span class="token operator">-></span>Next<span class="token punctuation">;</span> <span class="token function">free</span><span class="token punctuation">(</span>TmpCell<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span>Position <span class="token function">FindPrevious</span><span class="token punctuation">(</span>ElementType X<span class="token punctuation">,</span> List L<span class="token punctuation">)</span><span class="token punctuation">{</span> Position P<span class="token punctuation">;</span> P <span class="token operator">=</span> L<span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token constant">NULL</span> <span class="token operator">!=</span> P<span class="token operator">-></span>Next <span class="token operator">&&</span> X <span class="token operator">!=</span> P<span class="token operator">-></span>Next<span class="token operator">-></span>Element<span class="token punctuation">)</span><span class="token punctuation">{</span> P <span class="token operator">=</span> P<span class="token operator">-></span>Next<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> P<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></p><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token comment">/*Insert (after legal position P)*/</span><span class="token comment">/*Header implementation assumed*/</span><span class="token comment">/*Parameter L is unused in this implementation*/</span><span class="token keyword">void</span> <span class="token function">Insert</span><span class="token punctuation">(</span>ElementType X<span class="token punctuation">,</span> List L<span class="token punctuation">,</span> Position P<span class="token punctuation">)</span><span class="token punctuation">{</span> Position TmpCell<span class="token punctuation">;</span> TmpCell <span class="token operator">=</span> <span class="token punctuation">(</span>Position<span class="token punctuation">)</span><span class="token function">malloc</span><span class="token punctuation">(</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">Node</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token constant">NULL</span> <span class="token operator">==</span> TmpCell<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">FatalError</span><span class="token punctuation">(</span><span class="token string">"Out of space!!!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> TmpCell<span class="token operator">-></span>Element <span class="token operator">=</span> X<span class="token punctuation">;</span> TmpCell<span class="token operator">-></span>Next <span class="token operator">=</span> P<span class="token operator">-></span>Next<span class="token punctuation">;</span> P<span class="token operator">-></span>Next <span class="token operator">=</span> TmpCell<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3id="supplementary-about-the-programming-details-of-linked-list">SupplementaryAbout the Programming details of Linked List</h3><hr /><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token comment">/*Function GetElem is used to get the ith node's data*/</span><span class="token comment">/* Use e to return the result of the ith data*/</span>Status <span class="token function">GetElem</span><span class="token punctuation">(</span>List L<span class="token punctuation">,</span> <span class="token keyword">int</span> i<span class="token punctuation">,</span> ElementType <span class="token operator">&</span>e<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> j<span class="token punctuation">;</span> List T<span class="token punctuation">;</span> T <span class="token operator">=</span> L<span class="token operator">-></span>Next<span class="token punctuation">;</span> j <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>T <span class="token operator">&&</span> j<span class="token operator"><</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span> T <span class="token operator">=</span> T<span class="token operator">-></span>Next<span class="token punctuation">;</span> <span class="token operator">++</span>j<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>T <span class="token operator">||</span> j<span class="token operator">></span>i<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> ERROR<span class="token punctuation">;</span> <span class="token punctuation">}</span> e <span class="token operator">=</span> T<span class="token operator">-></span>Element<span class="token punctuation">;</span> <span class="token keyword">return</span> OK<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">/*Another way to insert the new data e*/</span><span class="token comment">// ListInsert will insert the new node e before the ith node.</span>Status <span class="token function">ListInsert</span><span class="token punctuation">(</span>List <span class="token operator">*</span>L<span class="token punctuation">,</span> <span class="token keyword">int</span> i<span class="token punctuation">,</span> ElementType e<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> List T<span class="token punctuation">,</span> S<span class="token punctuation">;</span> T <span class="token operator">=</span> <span class="token operator">*</span>L<span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span> T <span class="token operator">&&</span> j<span class="token operator"><</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span> T <span class="token operator">=</span> T<span class="token operator">-></span>Next<span class="token punctuation">;</span> <span class="token operator">++</span>j<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>T <span class="token operator">||</span> j<span class="token operator">></span>i<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> ERROR<span class="token punctuation">;</span> <span class="token punctuation">}</span> S <span class="token operator">=</span> <span class="token punctuation">(</span>List<span class="token punctuation">)</span><span class="token function">malloc</span><span class="token punctuation">(</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>Node<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> S<span class="token operator">-></span>Element <span class="token operator">=</span> e<span class="token punctuation">;</span> S<span class="token operator">-></span>Next <span class="token operator">=</span> T<span class="token operator">-></span>Next<span class="token punctuation">;</span> T<span class="token operator">-></span>Next <span class="token operator">=</span> S<span class="token punctuation">;</span> <span class="token keyword">return</span> OK<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">/*Another way to delete the data*/</span><span class="token comment">/*Delete ith node*/</span>Status <span class="token function">ListDelete</span><span class="token punctuation">(</span>List <span class="token operator">*</span>L<span class="token punctuation">,</span> <span class="token keyword">int</span> i<span class="token punctuation">,</span> ElementType <span class="token operator">&</span>e<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> List T<span class="token punctuation">,</span> S<span class="token punctuation">;</span> T <span class="token operator">=</span> <span class="token operator">*</span>L<span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>T<span class="token operator">-></span>Next <span class="token operator">&&</span> j<span class="token operator"><</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span> T <span class="token operator">=</span> T<span class="token operator">-></span>Next<span class="token punctuation">;</span> <span class="token operator">++</span>j<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token punctuation">(</span>T<span class="token operator">-></span>Next<span class="token punctuation">)</span> <span class="token operator">||</span> j<span class="token operator">></span>i<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> ERROR<span class="token punctuation">;</span> <span class="token punctuation">}</span> S <span class="token operator">=</span> T<span class="token operator">-></span>Next<span class="token punctuation">;</span> T<span class="token operator">-></span>Next <span class="token operator">=</span> S<span class="token operator">-></span>Next<span class="token punctuation">;</span> e <span class="token operator">=</span> S<span class="token operator">-></span>Element<span class="token punctuation">;</span> <span class="token function">free</span><span class="token punctuation">(</span>S<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> OK<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">/*Create a List with header Node.*/</span><span class="token keyword">void</span> <span class="token function">CreateListHead</span><span class="token punctuation">(</span>List <span class="token operator">*</span>L<span class="token punctuation">,</span> <span class="token keyword">int</span> n<span class="token punctuation">)</span><span class="token punctuation">{</span> List T<span class="token punctuation">;</span> <span class="token keyword">int</span> i<span class="token punctuation">;</span> <span class="token function">srand</span><span class="token punctuation">(</span><span class="token function">time</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token operator">*</span>L <span class="token operator">=</span> <span class="token punctuation">(</span>List<span class="token punctuation">)</span><span class="token function">malloc</span><span class="token punctuation">(</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>Node<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">(</span><span class="token operator">*</span>L<span class="token punctuation">)</span><span class="token operator">-></span>Next <span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator"><</span>n<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span> T <span class="token operator">=</span> <span class="token punctuation">(</span>List<span class="token punctuation">)</span><span class="token function">malloc</span><span class="token punctuation">(</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>Node<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> T<span class="token operator">-></span>Element <span class="token operator">=</span> <span class="token function">rand</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">%</span><span class="token number">100</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span> T<span class="token operator">-></span>Next <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token operator">*</span>L<span class="token punctuation">)</span><span class="token operator">-></span>Next<span class="token punctuation">;</span> <span class="token punctuation">(</span><span class="token operator">*</span>L<span class="token punctuation">)</span><span class="token operator">-></span>Next <span class="token operator">=</span> T<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">void</span> <span class="token function">CreateListTail</span><span class="token punctuation">(</span>List <span class="token operator">*</span>L<span class="token punctuation">,</span> <span class="token keyword">int</span> n<span class="token punctuation">)</span><span class="token punctuation">{</span> List T<span class="token punctuation">,</span> S<span class="token punctuation">;</span> <span class="token keyword">int</span> i<span class="token punctuation">;</span> <span class="token function">srand</span><span class="token punctuation">(</span><span class="token function">time</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token operator">*</span>L <span class="token operator">=</span> <span class="token punctuation">(</span>List<span class="token punctuation">)</span><span class="token function">malloc</span><span class="token punctuation">(</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>Node<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> T <span class="token operator">=</span> <span class="token operator">*</span>L<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator"><</span>n<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span> S <span class="token operator">=</span> <span class="token punctuation">(</span>List<span class="token punctuation">)</span><span class="token function">malloc</span><span class="token punctuation">(</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>Node<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> S<span class="token operator">-></span>Element<span class="token operator">=</span> <span class="token function">rand</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">%</span><span class="token number">100</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span> T<span class="token operator">-></span>Next <span class="token operator">=</span> S<span class="token punctuation">;</span> T<span class="token operator">=</span> S<span class="token punctuation">;</span> <span class="token punctuation">}</span> T<span class="token operator">-></span>Next<span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">/*Merge to Linked List*/</span><span class="token keyword">void</span> <span class="token function">MergeList</span><span class="token punctuation">(</span>List <span class="token operator">&</span>La<span class="token punctuation">,</span> List <span class="token operator">&</span>Lb<span class="token punctuation">,</span> List <span class="token operator">&</span>Lc<span class="token punctuation">)</span><span class="token punctuation">{</span> List Pa<span class="token punctuation">,</span> Pb<span class="token punctuation">,</span> Pc<span class="token punctuation">;</span> Pa<span class="token operator">=</span> La<span class="token operator">-></span>Next<span class="token punctuation">;</span> Pb<span class="token operator">=</span> Lb<span class="token operator">-></span>Next<span class="token punctuation">;</span> Lc<span class="token operator">=</span> Pc<span class="token operator">=</span> La<span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>Pa <span class="token operator">&&</span> Pb<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>Pa<span class="token operator">-></span>Element <span class="token operator"><=</span> Pb<span class="token operator">-></span>Element<span class="token punctuation">)</span><span class="token punctuation">{</span> Pc<span class="token operator">-></span>Next <span class="token operator">=</span> Pa<span class="token punctuation">;</span> Pc<span class="token operator">=</span> Pa<span class="token punctuation">;</span> Pa<span class="token operator">=</span> Pa<span class="token operator">-></span>Next<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span><span class="token punctuation">{</span> Pc<span class="token operator">-></span>Next<span class="token operator">=</span> Pb<span class="token punctuation">;</span> Pc<span class="token operator">=</span> Pb<span class="token punctuation">;</span> Pb<span class="token operator">=</span> Pb<span class="token operator">-></span>Next<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> Pc<span class="token operator">-></span>Next<span class="token operator">=</span> Pa<span class="token operator">?</span>Pa<span class="token operator">:</span>Pb<span class="token punctuation">;</span> <span class="token function">free</span><span class="token punctuation">(</span>Lb<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">/* Delete the whole list*/</span>Status <span class="token function">ClearList</span><span class="token punctuation">(</span>List <span class="token operator">*</span>L<span class="token punctuation">)</span><span class="token punctuation">{</span> List T<span class="token punctuation">,</span> S<span class="token punctuation">;</span> T <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token operator">*</span>L<span class="token punctuation">)</span><span class="token operator">-></span>Next<span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>T<span class="token punctuation">)</span><span class="token punctuation">{</span> S <span class="token operator">=</span> T<span class="token operator">-></span>Next<span class="token punctuation">;</span> <span class="token function">free</span><span class="token punctuation">(</span>T<span class="token punctuation">)</span><span class="token punctuation">;</span> T <span class="token operator">=</span> S<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">(</span><span class="token operator">*</span>L<span class="token punctuation">)</span><span class="token operator">-></span>Next <span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">;</span> <span class="token keyword">return</span> OK<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>And these two books also recommend the static linked list. Attention,the first element of the static linked list store the position of thefirst element of the back up Linked List, and the last element store theposition of the first data point that is inserted.</p><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">MAXSIZE</span> <span class="token expression"><span class="token number">10000000</span></span></span><span class="token keyword">typedef</span> <span class="token keyword">struct</span> <span class="token punctuation">{</span> ElementType Element<span class="token punctuation">;</span> <span class="token keyword">int</span> cur<span class="token punctuation">;</span> <span class="token comment">// cur is the same as the Next pointer in the Node</span><span class="token punctuation">}</span>Component<span class="token punctuation">,</span> StaticLinkedList<span class="token punctuation">[</span>MAXSIZE<span class="token punctuation">]</span><span class="token punctuation">;</span>Status <span class="token function">InitList</span><span class="token punctuation">(</span>StaticLinkedList space<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> i<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator"><</span>MAXSIZE<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span> space<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>cur <span class="token operator">=</span> i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> space<span class="token punctuation">[</span>MAXSIZE<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>cur <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">return</span> OK<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">/* achieve the malloc function of ourselves*/</span><span class="token keyword">int</span> <span class="token function">Malloc_SLL</span><span class="token punctuation">(</span>StaticLinkedList space<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> i <span class="token operator">=</span> space<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>cur<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>space<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>cur<span class="token punctuation">)</span><span class="token punctuation">{</span> space<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>cur <span class="token operator">=</span> space<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>cur<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> i<span class="token punctuation">;</span><span class="token punctuation">}</span>Status <span class="token function">ListInsert</span><span class="token punctuation">(</span>StaticLinkedList L<span class="token punctuation">,</span> <span class="token keyword">int</span> i<span class="token punctuation">,</span> ElementType e<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> j<span class="token punctuation">,</span> k<span class="token punctuation">,</span> l<span class="token punctuation">;</span> k <span class="token operator">=</span> MAXSIZE <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>i<span class="token operator"><</span><span class="token number">1</span> <span class="token operator">||</span> i<span class="token operator">></span><span class="token function">ListLength</span><span class="token punctuation">(</span>L<span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> ERROR<span class="token punctuation">;</span> <span class="token punctuation">}</span> j <span class="token operator">=</span> <span class="token function">Malloc_SLL</span><span class="token punctuation">(</span>L<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>j<span class="token punctuation">)</span><span class="token punctuation">{</span> L<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>Element <span class="token operator">=</span> e<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>l<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span> l<span class="token operator"><</span>i<span class="token punctuation">;</span> <span class="token operator">++</span>l<span class="token punctuation">)</span><span class="token punctuation">{</span> k <span class="token operator">=</span> L<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>cur<span class="token punctuation">;</span> <span class="token punctuation">}</span> L<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>cur <span class="token operator">=</span> L<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>cur<span class="token punctuation">;</span> L<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>cur <span class="token operator">=</span> j<span class="token punctuation">;</span> <span class="token keyword">return</span> OK<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> ERROR<span class="token punctuation">;</span><span class="token punctuation">}</span>Status <span class="token function">ListDelete</span><span class="token punctuation">(</span>StaticLinkedList L<span class="token punctuation">,</span> <span class="token keyword">int</span> i<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> j<span class="token punctuation">,</span> k<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>i<span class="token operator"><</span><span class="token number">1</span> <span class="token operator">||</span> i<span class="token operator">></span><span class="token function">ListLength</span><span class="token punctuation">(</span>L<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> ERROR<span class="token punctuation">;</span> <span class="token punctuation">}</span> k <span class="token operator">=</span> MAXSIZE <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>j<span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> j<span class="token operator"><</span>i<span class="token punctuation">;</span> <span class="token operator">++</span>j<span class="token punctuation">)</span><span class="token punctuation">{</span> k <span class="token operator">=</span> L<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>cur<span class="token punctuation">;</span> <span class="token punctuation">}</span> j <span class="token operator">=</span> L<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>cur<span class="token punctuation">;</span> L<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>cur <span class="token operator">=</span> L<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>cur<span class="token punctuation">;</span> <span class="token function">Free_SSL</span><span class="token punctuation">(</span>L<span class="token punctuation">,</span> j<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> OK<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">void</span> <span class="token function">Free_SSL</span><span class="token punctuation">(</span>StaticLinkedList space<span class="token punctuation">,</span> <span class="token keyword">int</span> k<span class="token punctuation">)</span><span class="token punctuation">{</span> space<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">.</span>cur <span class="token operator">=</span> space<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>cur<span class="token punctuation">;</span> space<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>cur <span class="token operator">=</span> k<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">int</span> <span class="token function">ListLength</span><span class="token punctuation">(</span>StaticLinkedList L<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">int</span> i <span class="token operator">=</span> L<span class="token punctuation">[</span>MAXSIZE<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>cur<span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span> i <span class="token operator">=</span> L<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token operator">++</span>j<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> j<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="doubly-linked-list">Doubly Linked List</h3><hr /><p>It is convenient to traverse the linked list inversly in this way.<pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">typedef</span> <span class="token keyword">struct</span> <span class="token class-name">DulNode</span><span class="token punctuation">{</span> ElementType data<span class="token punctuation">;</span> <span class="token keyword">struct</span> <span class="token class-name">DulNode</span> <span class="token operator">*</span>prior<span class="token punctuation">;</span> <span class="token keyword">struct</span> <span class="token class-name">DulNode</span> <span class="token operator">*</span>next<span class="token punctuation">;</span><span class="token punctuation">}</span>DulNode<span class="token punctuation">,</span> <span class="token operator">*</span>DuLinkList<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> ### Circularly Linked List ***</p><p>A popular convention is to have the last cell keep a pointer back tothe first. This can be done without a header and can also be done withdoubly linked lists.</p><p>The main difference between circularly linked list and the plainlinked list is the condition of judgement. The condition of circularlylinked list is to judge whether Next node equal to header node.</p><p>To visit both the head and the rear of the linked list, we change thehead pointer to the tail pointer that point to the rear of the linkedlist. In this way, we can find the header node userear->Next->Next.</p><p>The routine to merge to circularly is simple by using rear pointer.<pre class="line-numbers language-c" data-language="c"><code class="language-c">Status <span class="token function">MergeCirCularlyLinkedList</span><span class="token punctuation">(</span>List rearA<span class="token punctuation">,</span> List rearB<span class="token punctuation">)</span><span class="token punctuation">{</span> List T<span class="token punctuation">,</span> S<span class="token punctuation">;</span> T <span class="token operator">=</span> rearA<span class="token operator">-></span>Next<span class="token punctuation">;</span> rearA<span class="token operator">-></span>Next <span class="token operator">=</span> rearB<span class="token operator">-></span>Next<span class="token operator">-></span>Next<span class="token punctuation">;</span> S <span class="token operator">=</span> rearB<span class="token operator">-></span>Next<span class="token punctuation">;</span> rearB<span class="token operator">-></span>Next <span class="token operator">=</span> T<span class="token punctuation">;</span> <span class="token function">free</span><span class="token punctuation">(</span>S<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> OK<span class="token punctuation">;</span> <span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></p><h3 id="example-the-polynomial-adt">Example: The Polynomial ADT</h3><hr /><p>We can define an abstract data type for single-variable polynomialsby using a list. However this is only adequate for densepolynomials.</p><p>To solve the more general problem, we'd bette use a linked list torepresent the polynomial and define some routine to make someoperation.</p><p>The codes of this part are mainly come from the two books of TsingHua.</p><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">typedef</span> <span class="token keyword">struct</span><span class="token punctuation">{</span> <span class="token keyword">float</span> coef<span class="token punctuation">;</span> <span class="token keyword">int</span> expn<span class="token punctuation">;</span><span class="token punctuation">}</span>term<span class="token punctuation">,</span> ElementType<span class="token punctuation">;</span><span class="token keyword">typedef</span> List polynomial<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="example-radix-sort">Example: Radix Sort</h3><hr /><h2 id="stack-adt">Stack ADT</h2><hr /><h3 id="stack-model">Stack Model</h3><p>Stack is a data structure that constrain the insertion and deletionat the same place, which is called its top. The corresponding operationsof insertion and deletion are called <strong>push</strong> and<strong>pop</strong>.</p><p>Sometimes, stack also called as LIFO list.</p><h3 id="stack-made-by-linked-list">Stack Made By Linked List</h3><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifndef</span> <span class="token expression">_Stack_H</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">_Stack_H</span></span><span class="token keyword">struct</span> <span class="token class-name">Node</span><span class="token punctuation">;</span><span class="token keyword">typedef</span> Node <span class="token operator">*</span>PtrToNode<span class="token punctuation">;</span><span class="token keyword">typedef</span> PtrToNode Stack<span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">IsEmpty</span><span class="token punctuation">(</span>Stack S<span class="token punctuation">)</span><span class="token punctuation">;</span>Stack <span class="token function">CreateStack</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">void</span> <span class="token function">DisposeStack</span><span class="token punctuation">(</span>Stack S<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">void</span> <span class="token function">MakeEmpty</span><span class="token punctuation">(</span>Stack S<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">void</span> <span class="token function">Push</span><span class="token punctuation">(</span>ElementType X<span class="token punctuation">,</span> Stack S<span class="token punctuation">)</span><span class="token punctuation">;</span>ElementType <span class="token function">Top</span><span class="token punctuation">(</span>Stack S<span class="token punctuation">)</span><span class="token punctuation">;</span>ElementType <span class="token function">Pop</span><span class="token punctuation">(</span>Stack S<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span><span class="token comment">/*Place in implementation file*/</span><span class="token comment">/*Stack implementation is a linked list with a header*/</span><span class="token keyword">struct</span> <span class="token class-name">Node</span><span class="token punctuation">{</span> ElementType Element<span class="token punctuation">;</span> PtrToNode Next<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>Functions definitions:</p><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">int</span> <span class="token function">IsEmpty</span><span class="token punctuation">(</span>Stack S<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token constant">NULL</span> <span class="token operator">==</span> S<span class="token operator">-></span>Next<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><p>Create a stack and make the stack empty. <pre class="line-numbers language-c" data-language="c"><code class="language-c">Stack <span class="token function">CreateStack</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">{</span> Stack S<span class="token punctuation">;</span> S <span class="token operator">=</span> <span class="token punctuation">(</span>Stack<span class="token punctuation">)</span><span class="token function">malloc</span><span class="token punctuation">(</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>Node<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token constant">NULL</span> <span class="token operator">==</span> S<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">FatalError</span><span class="token punctuation">(</span><span class="token string">"Out of space"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> S<span class="token operator">-></span>Next <span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">;</span> <span class="token function">MakeEmpty</span><span class="token punctuation">(</span>S<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> S<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">void</span> <span class="token function">MakeEmpty</span><span class="token punctuation">(</span>Stack S<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token constant">NULL</span> <span class="token operator">==</span> S<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">Error</span><span class="token punctuation">(</span><span class="token string">"Make use CreateStack first."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span><span class="token punctuation">{</span> <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">IsEmpty</span><span class="token punctuation">(</span>S<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">Pop</span><span class="token punctuation">(</span>S<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></p><p>Three important operations:</p><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">void</span> <span class="token function">Push</span><span class="token punctuation">(</span>ElementType X<span class="token punctuation">,</span> Stack S<span class="token punctuation">)</span><span class="token punctuation">{</span> PtrToNode TmpCell<span class="token punctuation">;</span> TmpCell <span class="token operator">=</span> <span class="token punctuation">(</span>PtrToNode<span class="token punctuation">)</span><span class="token function">malloc</span><span class="token punctuation">(</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>Node<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token constant">NULL</span> <span class="token operator">==</span> TmpCell<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">FatalError</span><span class="token punctuation">(</span><span class="token string">"Out of space"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span><span class="token punctuation">{</span> TmpCell<span class="token operator">-></span>Element <span class="token operator">=</span> X<span class="token punctuation">;</span> TmpCell<span class="token operator">-></span>Next <span class="token operator">=</span> S<span class="token operator">-></span>Next<span class="token punctuation">;</span> S<span class="token operator">-></span>Next <span class="token operator">=</span> TmpCell<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span>ElementType <span class="token function">Top</span><span class="token punctuation">(</span>Stack S<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">IsEmpty</span><span class="token punctuation">(</span>S<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> S<span class="token operator">-></span>Next<span class="token operator">-></span>Element<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token function">Error</span><span class="token punctuation">(</span><span class="token string">"Empty Stack"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token punctuation">[</span>warning value<span class="token punctuation">]</span> <span class="token comment">//return value used to avoid warning</span><span class="token punctuation">}</span><span class="token keyword">void</span> <span class="token function">Pop</span><span class="token punctuation">(</span>Stack S<span class="token punctuation">)</span><span class="token punctuation">{</span> PtrToNode FirstCell<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>IsEmpty<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">Error</span><span class="token punctuation">(</span><span class="token string">"Empty Stack"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span><span class="token punctuation">{</span> FirstCell <span class="token operator">=</span> S<span class="token operator">-></span>Next<span class="token punctuation">;</span> S<span class="token operator">-></span>Next <span class="token operator">=</span> FirstCell<span class="token operator">-></span>Next<span class="token punctuation">;</span> <span class="token function">free</span><span class="token punctuation">(</span>FirstCell<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="stack-made-by-array">Stack Made By Array</h3><hr /><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifndef</span> <span class="token expression">_Stack_h</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">_Stack_h</span></span><span class="token keyword">struct</span> <span class="token class-name">StackRecord</span><span class="token punctuation">;</span><span class="token keyword">typedef</span> <span class="token keyword">struct</span> <span class="token class-name">StackRecord</span> <span class="token operator">*</span>Stack<span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">IsEmpty</span><span class="token punctuation">(</span>Stack S<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">IsFull</span><span class="token punctuation">(</span>Stack S<span class="token punctuation">)</span><span class="token punctuation">;</span>Stack <span class="token function">CreateStack</span><span class="token punctuation">(</span><span class="token keyword">int</span> MaxElements<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">void</span> <span class="token function">DisposeStack</span><span class="token punctuation">(</span>Stack S<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">void</span> <span class="token function">MakeEmpty</span><span class="token punctuation">(</span>Stack S<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">void</span> <span class="token function">Push</span><span class="token punctuation">(</span>ElementType X<span class="token punctuation">,</span> Stack S<span class="token punctuation">)</span><span class="token punctuation">;</span>ElementType <span class="token function">Top</span><span class="token punctuation">(</span>Stack S<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">void</span> <span class="token function">Pop</span><span class="token punctuation">(</span>Stack S<span class="token punctuation">)</span><span class="token punctuation">;</span>ElementType <span class="token function">PopAndPop</span><span class="token punctuation">(</span>Stack S<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span><span class="token comment">/*Stack in implementation file is a dynamically allocated array*/</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">EmptyTOS</span> <span class="token expression"><span class="token operator">-</span><span class="token number">1</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">MinStackSize</span> <span class="token expression"><span class="token punctuation">[</span>any number<span class="token punctuation">]</span></span></span><span class="token keyword">struct</span> <span class="token class-name">StackRecord</span><span class="token punctuation">{</span> <span class="token keyword">int</span> Capacity<span class="token punctuation">;</span> <span class="token keyword">int</span> TopOfStack<span class="token punctuation">;</span> ElementType <span class="token operator">*</span>Array<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>Create a stack: <pre class="line-numbers language-c" data-language="c"><code class="language-c">Stack <span class="token function">CreateStack</span><span class="token punctuation">(</span><span class="token keyword">int</span> MaxElements<span class="token punctuation">)</span><span class="token punctuation">{</span> Stack S<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>MaxElements <span class="token operator"><</span> MinStackSize<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">Error</span><span class="token punctuation">(</span><span class="token string">"Stack size is too small"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> S <span class="token operator">=</span> <span class="token punctuation">(</span>Stack<span class="token punctuation">)</span><span class="token function">malloc</span><span class="token punctuation">(</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">struct</span> <span class="token class-name">StackRecord</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token constant">NULL</span> <span class="token operator">==</span> S<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">FatalError</span><span class="token punctuation">(</span><span class="token string">"Out of space"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> S<span class="token operator">-></span>Array <span class="token operator">=</span> <span class="token punctuation">(</span>ElementType<span class="token operator">*</span><span class="token punctuation">)</span><span class="token function">malloc</span><span class="token punctuation">(</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>Elementype<span class="token punctuation">)</span><span class="token operator">*</span>MaxElements<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token constant">NULL</span> <span class="token operator">==</span> S<span class="token operator">-></span>Array<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">FatalError</span><span class="token punctuation">(</span><span class="token string">"Out of space"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> S<span class="token operator">-></span>Capacity <span class="token operator">=</span> MaxElements<span class="token punctuation">;</span> <span class="token function">MakeEmpty</span><span class="token punctuation">(</span>S<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> S<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></p><p>Dispose the stack: <pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">void</span> <span class="token function">DisposeStack</span><span class="token punctuation">(</span>Stack S<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token constant">NULL</span> <span class="token operator">!=</span> S<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">free</span><span class="token punctuation">(</span>S<span class="token operator">-></span>Array<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">free</span><span class="token punctuation">(</span>S<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></p><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">int</span> <span class="token function">IsEmpty</span><span class="token punctuation">(</span>Stack S<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> EmptyTOS<span class="token operator">==</span> S<span class="token operator">-></span>TopOfStack<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">void</span> <span class="token function">MakeEmpty</span><span class="token punctuation">(</span>Stack S<span class="token punctuation">)</span><span class="token punctuation">{</span> S<span class="token operator">-></span>TopOfStack<span class="token operator">=</span> EmptyTOS<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">void</span> <span class="token function">Push</span><span class="token punctuation">(</span>Elementype X<span class="token punctuation">,</span> Stack S<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">IsFull</span><span class="token punctuation">(</span>S<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">Error</span><span class="token punctuation">(</span><span class="token string">"Full Stack"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span><span class="token punctuation">{</span> S<span class="token operator">-></span>Array<span class="token punctuation">[</span><span class="token operator">++</span>S<span class="token operator">-></span>TopOfStack<span class="token punctuation">]</span><span class="token operator">=</span> X<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span>Elementype <span class="token function">Top</span><span class="token punctuation">(</span>Stack S<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">IsEmpty</span><span class="token punctuation">(</span>S<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> S<span class="token operator">-></span>Array<span class="token punctuation">[</span>TopOfStack<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token function">Error</span><span class="token punctuation">(</span><span class="token string">"Empty Stack"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">void</span> <span class="token function">Pop</span><span class="token punctuation">(</span>Stack S<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">IsEmpty</span><span class="token punctuation">(</span>S<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">Error</span><span class="token punctuation">(</span><span class="token string">"Empty Stack"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span><span class="token punctuation">{</span> <span class="token operator">--</span>S<span class="token operator">-></span>TopOfStack<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span>Elementype <span class="token function">TopAndPop</span><span class="token punctuation">(</span>Stack S<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">IsEmpty</span><span class="token punctuation">(</span>S<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> S<span class="token operator">-></span>Array<span class="token punctuation">[</span>S<span class="token operator">-></span>TopAndPop<span class="token operator">--</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token function">Error</span><span class="token punctuation">(</span><span class="token string">"Empty Stack"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="application">Application</h3><hr /><h2 id="queue">Queue</h2><hr /><p>Queue is an abstract data structure that constrain the insertion atone side of the list and the deletion at the other side. Queue is alsocalled FIFO ADT.</p><h3 id="queue-made-by-array">Queue Made By Array</h3><hr /><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifndef</span> <span class="token expression">_Queue_H</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">_Queue_H</span></span><span class="token keyword">struct</span> <span class="token class-name">QueueRecord</span><span class="token punctuation">;</span><span class="token keyword">typedef</span> <span class="token keyword">struct</span> <span class="token class-name">QueueRecord</span> <span class="token operator">*</span>Queue<span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">IsEmpty</span><span class="token punctuation">(</span>Queue Q<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">IsFull</span><span class="token punctuation">(</span>Queue Q<span class="token punctuation">)</span><span class="token punctuation">;</span>Queue <span class="token function">CreateQueue</span><span class="token punctuation">(</span><span class="token keyword">int</span> MaxElements<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">void</span> <span class="token function">DisposeQueue</span><span class="token punctuation">(</span>Queue Q<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">void</span> <span class="token function">MakeEmpty</span><span class="token punctuation">(</span>Queue Q<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">void</span> <span class="token function">Enqueue</span><span class="token punctuation">(</span>Elementype X<span class="token punctuation">,</span> Queue Q<span class="token punctuation">)</span><span class="token punctuation">;</span>ElementType <span class="token function">Front</span><span class="token punctuation">(</span>Queue Q<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">void</span> <span class="token function">Dequeue</span><span class="token punctuation">(</span>Queue Q<span class="token punctuation">)</span><span class="token punctuation">;</span>Elementype <span class="token function">FrontAndDequeue</span><span class="token punctuation">(</span>Queue<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span><span class="token comment">/*Place in implementation file*/</span><span class="token comment">/*Queue implementation is a dynamically allocated array*/</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">MinQueueSize</span> <span class="token expression"><span class="token punctuation">[</span>any positive number<span class="token punctuation">]</span></span></span><span class="token keyword">struct</span> <span class="token class-name">QueueRecord</span><span class="token punctuation">{</span> <span class="token keyword">int</span> Capacity<span class="token punctuation">;</span> <span class="token keyword">int</span> Front<span class="token punctuation">;</span> <span class="token keyword">int</span> Rear<span class="token punctuation">;</span> <span class="token keyword">int</span> Size<span class="token punctuation">;</span> Elementype <span class="token operator">*</span>Array<span class="token punctuation">}</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>Some routine:</p><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">int</span> <span class="token function">IsEmpty</span><span class="token punctuation">(</span>Queue Q<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token operator">==</span> Q<span class="token operator">-></span>Size<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">void</span> <span class="token function">MakeEmpty</span><span class="token punctuation">(</span>Queue Q<span class="token punctuation">)</span><span class="token punctuation">{</span> Q<span class="token operator">-></span>Size<span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> Q<span class="token operator">-></span>Front<span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> Q<span class="token operator">-></span>Rear<span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span>Static <span class="token keyword">int</span> <span class="token function">Succ</span><span class="token punctuation">(</span><span class="token keyword">int</span> Value<span class="token punctuation">,</span> Queue Q<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">++</span>Value<span class="token operator">==</span> Q<span class="token operator">-></span>Capacity<span class="token punctuation">)</span><span class="token punctuation">{</span> Value<span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> Value<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">void</span> <span class="token function">Enqueue</span><span class="token punctuation">(</span>Elementype X<span class="token punctuation">,</span> Queue Q<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">IsFull</span><span class="token punctuation">(</span>Q<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token operator">++</span>Q<span class="token operator">-></span>Size<span class="token punctuation">;</span> Q<span class="token operator">-></span>Rear<span class="token operator">=</span> <span class="token function">Succ</span><span class="token punctuation">(</span>Q<span class="token operator">-></span>Rear<span class="token punctuation">,</span> Q<span class="token punctuation">)</span><span class="token punctuation">;</span> Q<span class="token operator">-></span><span class="token punctuation">[</span>Q<span class="token operator">-></span>Rear<span class="token punctuation">]</span><span class="token operator">=</span> X<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span><span class="token punctuation">{</span> <span class="token function">Error</span><span class="token punctuation">(</span><span class="token string">"Full Queue"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="queue-implemented-by-linked-listsupplementary">Queue ImplementedBy Linked List(Supplementary)</h3><hr /><p>Some preparations:</p><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">typedef</span> <span class="token keyword">struct</span> <span class="token class-name">QNode</span><span class="token punctuation">{</span> QElemType data<span class="token punctuation">;</span> <span class="token keyword">struct</span> <span class="token class-name">QNode</span> <span class="token operator">*</span>next<span class="token punctuation">;</span><span class="token punctuation">}</span>QNode<span class="token punctuation">,</span> <span class="token operator">*</span>QueuePtr<span class="token punctuation">;</span><span class="token keyword">typedef</span> <span class="token keyword">struct</span><span class="token punctuation">{</span> QueuePtr front<span class="token punctuation">;</span> QueuePtr rear<span class="token punctuation">;</span><span class="token punctuation">}</span>LinkQueue<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>Routines:</p><pre class="line-numbers language-c" data-language="c"><code class="language-c">Status <span class="token function">EnQueue</span><span class="token punctuation">(</span>LinkQueue <span class="token operator">*</span>Q<span class="token punctuation">,</span> QElemType e<span class="token punctuation">)</span><span class="token punctuation">{</span> QueuePtr s<span class="token operator">=</span> <span class="token punctuation">(</span>QueuePtr<span class="token punctuation">)</span><span class="token function">malloc</span><span class="token punctuation">(</span><span class="token function">sizof</span><span class="token punctuation">(</span>QNode<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>s<span class="token punctuation">)</span> <span class="token function">exit</span><span class="token punctuation">(</span>OVERFLOW<span class="token punctuation">)</span><span class="token punctuation">;</span> s<span class="token operator">-></span>data<span class="token operator">=</span> e<span class="token punctuation">;</span> s<span class="token operator">-></span>next<span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">;</span> Q<span class="token operator">-></span>rear<span class="token operator">-></span>next<span class="token operator">=</span> s<span class="token punctuation">;</span> Q<span class="token operator">-></span>rear<span class="token operator">=</span> s<span class="token punctuation">;</span> <span class="token keyword">return</span> OK<span class="token punctuation">;</span><span class="token punctuation">}</span>Status <span class="token function">Dequeue</span><span class="token punctuation">(</span>LinkQueue <span class="token operator">*</span>Q<span class="token punctuation">,</span> QElemType <span class="token operator">*</span>e<span class="token punctuation">)</span><span class="token punctuation">{</span> QueuePtr p<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>Q<span class="token operator">-></span>front<span class="token operator">==</span> Q<span class="token operator">-></span>rear<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> ERROR<span class="token punctuation">;</span> <span class="token punctuation">}</span> p<span class="token operator">=</span> Q<span class="token operator">-></span>front<span class="token operator">-></span>next<span class="token punctuation">;</span> <span class="token operator">*</span>e<span class="token operator">=</span> p<span class="token operator">-></span>data<span class="token punctuation">;</span> Q<span class="token operator">-></span>front<span class="token operator">-></span>next<span class="token operator">=</span> p<span class="token operator">-></span>next<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>Q<span class="token operator">-></span>rear<span class="token operator">==</span> p<span class="token punctuation">)</span><span class="token punctuation">{</span> Q<span class="token operator">-></span>rear<span class="token operator">=</span> Q<span class="token operator">-></span>front<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token function">free</span><span class="token punctuation">(</span>p<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> OK<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h1 id="string">String</h1><hr /><p>This chapter consists of the contents from two Tsing Hua's books.</p><h2 id="definition">Definition</h2><hr /><p><strong>String is an ordered sequence made by none character or manycharacters.</strong></p><p>We denoted a string <em>s</em> as:</p><p><span class="math display">\[s= a_{1} a_{2} \cdots a_{n}\]</span></p><p>The number of the characters in the string is called the<em>length</em> of the string. If the string doesn't have any character,we call it the <em>null string</em>.</p><h2 id="the-comparasion-between-strings">The Comparasion BetweenStrings</h2><hr /><p><span class="math inline">\(s= "a_{1} a_{2} \cdotsa_{n}"\)</span> <span class="math inline">\(t= "a_{1} a_{2}\cdots a_{m}"\)</span></p><p>If any condition of the two conditions displayed below is satisfied,we define this <span class="math inline">\(s<t\)</span>.</p><ul><li><span class="math inline">\(n<m\)</span>, &<spanclass="math inline">\(a_i= b_i\)</span>.</li><li>There exist some <span class="math inline">\(k\leq min(m,n)\)</span> such that <span class="math inline">\(a_i= b_i(i= 1, 2,\cdots, k-1), a_k<b_k\)</span></li></ul><h2 id="the-storage-structure-of-the-string">The Storage Structure OfThe String</h2><hr /><p>It can be divided into two types.</p><h3 id="the-sequential-storage-structure">The Sequential StorageStructure</h3><hr /><p>It is a sequence of characters which have a contiguous set ofaddresses. This kind of strings usually have to ensure the enough spacefor the string.</p><h3 id="chain-storage-structure">Chain Storage Structure</h3><hr /><p>It has a waste of the space and it doesn't flexible like thesequential storage structure.</p><h2 id="pattern-matching-algorithmkmp">Pattern MatchingAlgorithm(KMP)</h2><hr /><p>Actually, I intend to write another blog to focus on some patternmatching algorithm from <em>Handbook of Exact String MatchingAlgorithms</em>.</p><p>In this blog, we just simply focus on KMP Pattern Matching Algorithm.More specifically, the C code of KMP.</p><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">void</span> <span class="token function">PreKMP</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span>x<span class="token punctuation">,</span> <span class="token keyword">int</span> m<span class="token punctuation">,</span> <span class="token keyword">int</span> KmpNext<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> i<span class="token punctuation">,</span> j<span class="token punctuation">;</span> i<span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j<span class="token operator">=</span> KmpNext<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment">// j is always the end index of the prefix substring.</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>i<span class="token operator"><</span>m<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>j<span class="token operator">></span> <span class="token operator">-</span><span class="token number">1</span> <span class="token operator">&&</span> x<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">!=</span> x<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span> j<span class="token operator">=</span> KmpNext<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">// To simplify the code, we add the while loop at here</span> <span class="token operator">++</span>i<span class="token punctuation">;</span> <span class="token operator">++</span>j<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>x<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">==</span> x<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span> KmpNext<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span> KmpNext<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> KmpNext<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span> j<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">void</span> <span class="token function">KMP</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span>x<span class="token punctuation">,</span> <span class="token keyword">int</span> m<span class="token punctuation">,</span> <span class="token keyword">char</span> <span class="token operator">*</span>y<span class="token punctuation">,</span> <span class="token keyword">int</span> n<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> i<span class="token punctuation">,</span> j<span class="token punctuation">,</span> KmpNext<span class="token punctuation">[</span>XSIZE<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token function">PreKMP</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> m<span class="token punctuation">,</span> KmpNext<span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">=</span> j<span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>j<span class="token operator"><</span>n<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>i<span class="token operator">></span><span class="token operator">-</span><span class="token number">1</span> <span class="token operator">&&</span> x<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">!=</span> y<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span> i<span class="token operator">=</span> KmpNext<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token operator">++</span>i<span class="token punctuation">;</span> <span class="token operator">++</span>j<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>i<span class="token operator">>=</span> m<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">OUTPUT</span><span class="token punctuation">(</span>j<span class="token operator">-</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">=</span> KmpNext<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>Another C code: <pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">void</span> <span class="token function">GetNext</span><span class="token punctuation">(</span>String T<span class="token punctuation">,</span> <span class="token keyword">int</span> <span class="token operator">*</span>next<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre> # Tree ***</p><p>For the big size of the data set, linear time to access is too slow.The tree is a data structure which most of its operation can beimplemented in <span class="math inline">\(\Omega(N)\)</span> time.</p><h2 id="definition-1">Definition</h2><hr /><p>In generally, we define a tree data structure by recursion.</p><p>A tree is the set of some nodes. This set can be null. If not, a treeconsist of a node r named <strong>root</strong> and lots of subtreeswhich are not empty. What's more, every root node from these subtrees islinked by a directed edge from the node r.</p><p>The amount of the subtrees the node own is defined as the degree ofthe node. And the node with 0 degree is called as the leaf node. Thenode of the tree is defined as the maximum degree of the node in thetree.</p><p>The root of the subtree which the node own is called as child. Andthe node is known as parent. Two child with the same parent aresibling.</p><p>The level is counted from the root. And the maximum level of the treeis called the depth.</p><p>Forest is a set of disjoint trees.</p><h2 id="the-traverse-and-application-of-the-tree">The Traverse AndApplication Of The Tree</h2><hr /><h1 id="hash">Hash</h1><hr /><p>Hash technique is set up a relation between its key word and itsstorage location. We call the relation as hash function.</p><h2 id="process">Process</h2><hr /><ul><li>design hash functions</li><li>use hash functions to store the keyword</li><li>deal with the collision</li><li>search the location by hash functions and the keyword you wannafind</li><li>if there is a collision, solve it</li></ul><h2 id="principles-of-designing-hash-function">Principles Of DesigningHash Function</h2><hr /><ul><li>Easy to calculate</li><li>It's distribution is as uniform as possible</li></ul><h2 id="direct-address-table">Direct-address Table</h2><hr /><p>It is designed for the situation the whole domain U is small. It isconvenient for us to use keyword as its index.</p><p>We can also use a linear function of its keyword as hashfunctions.</p><h2 id="analyze-numbers-method">Analyze Numbers Method</h2><hr /><p>We can extract part of the keyword and do some other operation onthis part as its index. This is based on the analysis we have about thedata we gonna handle.</p>]]></content>
<categories>
<category> Programming </category>
</categories>
<tags>
<tag> Base </tag>
</tags>
</entry>
<entry>
<title>Linux Command Line</title>
<link href="2019/linux-command-line.html"/>
<url>2019/linux-command-line.html</url>
<content type="html"><![CDATA[<h1 id="learning-shell">Learning Shell</h1><hr /><h2 id="about-shell">About Shell</h2><hr /><p>When we speak of command line, we are referring to the shell. Theshell is a program that takes keyboard commands and passes them to theoperating system to carry out. Almost all linux distribtuions supply ashell program from the GNU project called bash, which is an acronym for"Bourne Again Shell"</p><p>When using a graphical user interface, we need another program calleda terminal emulator to interact with shell.</p><h2 id="navigate-the-file-system">Navigate the File System</h2><hr /><ul><li>pwd - print working directory</li><li>ls - list the files and the directories in the current workingdirectory, we use the ls command line.</li><li>cd [pathname]- change the working directory.</li><li>cd - change the working directory to your home directory.</li><li>"cd -" - change the working directory to the previous workingdirectory.</li><li>cd ~username - change the working directory to the home directoryuser_name.</li></ul><h3 id="absolute-pathname">Absolute Pathname</h3><hr /><p>An absolute pathname begins with the root directory and follows withbranch by branch until the path to the desired directory and file iscompleted.</p><h3 id="relative-pathname">Relative Pathname</h3><hr /><p>We use "." and ".." to represent relative positions in the filesystem tree. The "." symbol refers to the working directory and the ".."refers to the working directory's parent directory.</p><p>In almost all cases, you can omit the "./". It is implied.</p><h3 id="important-facts-about-filenames">Important Facts AboutFilenames</h3><hr /><ul><li>Filenames that begin with a period character "." are hidden. Thatonly means that ls will not list them unless you use "ls-a".</li><li>Filenames and commands in linux, like unix, are case sensitive.</li><li>Linux has no concept of a "file extension" like some other operatingsystems.</li><li>Though Linux supports long filenames which may contain embeddedspaces and punctuation characters, limit the punctuation characters inthe name of files you create to period, dash, and underscore.</li></ul><h2 id="exploring-your-system">Exploring your System</h2><hr /><h3 id="the-fun-of-ls">The fun of ls</h3><hr /><ul><li>We can specify the directory to list.</li><li>Even specify multiple directories.</li></ul><h3 id="options-and-arguments">Options and Arguments</h3><hr /><table><colgroup><col style="width: 33%" /><col style="width: 33%" /><col style="width: 33%" /></colgroup><thead><tr class="header"><th>Options</th><th>long option</th><th>Description</th></tr></thead><tbody><tr class="odd"><td>-a</td><td>--all</td><td>List all files, even thos with name begin with a period, which arenormally not list.</td></tr><tr class="even"><td>-d</td><td>--directory</td><td>Ordinarily, if a directory is not specified, ls will list thecontents of the directory, not the directory itself. Use this optionconjunction with the -l option to see details about the directory ratherthan its contents.</td></tr><tr class="odd"><td>-F</td><td>--classify</td><td>This option will append an indicator character to the end of eachlisted name. For example, a '/' if the name is a directory.</td></tr><tr class="even"><td>-h</td><td>--human-readable</td><td>In long format listings, display sizes in human readable formatrather than in bytes.</td></tr><tr class="odd"><td>-l</td><td></td><td>Display result in long format.</td></tr><tr class="even"><td>-r</td><td>--reverse</td><td>Display the result in reverse order. Normally, ls display itsresults in ascending alphabetical order.</td></tr><tr class="odd"><td>-S</td><td></td><td>Sort results by file size.</td></tr><tr class="even"><td>-t</td><td></td><td>Sort by modification time.</td></tr></tbody></table><h3 id="useful-information-from-long-format">Useful Information FromLong Format</h3><hr /><p>Take an example directory from Ubuntu: > -rw-r--r-- 1 root root32059 2007-04-03 11:05 oo-cd-cover.odf</p><p>Look at the different fields from this file and examine theirmeanings: | Field | Meaning | |---|---| | -rw-r--r-- | Access rights tofile. The first character indicates the type of file. Among thedifferent types, a leading dash means a regular file, while a "d"indicates a directory. The next three characters are the access rightsfor the file's owner, the next three are for the members of the file'sgroup, and the final three are for the everyone else. The full meaningof this is discussed in chapter Permissions.| | l | File's number ofhard links. | | root | The user name of the file's owner. | | root | Thename of the group which owns the file.| | 32059 | Size of the file inbytes. | |2007-04-03 11:05 | Date and time of the last modification| |oo-cd-cover.odf | Name of the file. |</p><h3 id="confirm-the-type-of-the-file">Confirm the Type Of the File</h3><hr /><p>We can invoke the file command this way: >file [filename]</p><h3 id="view-text-files-with-less">View Text Files with Less</h3><hr /><ul><li>less [filename]</li></ul><table><thead><tr class="header"><th>Command of less mode</th><th>Action</th></tr></thead><tbody><tr class="odd"><td>PAGE UP or b</td><td>Scroll back one page</td></tr><tr class="even"><td>PAGE DOWN or space</td><td>Scorll forward one page</td></tr><tr class="odd"><td>UP Arrow</td><td>Scroll Up one line</td></tr><tr class="even"><td>Down Arrow</td><td>Scroll down one line</td></tr><tr class="odd"><td>G</td><td>Move to the end of the text file</td></tr><tr class="even"><td>lG or g</td><td>Move to the beginning of the text file</td></tr><tr class="odd"><td>/[characters]</td><td>Search forward for the next occurance of characters</td></tr><tr class="even"><td>n</td><td>Search forward for the next occurance of the previous search</td></tr><tr class="odd"><td>h</td><td>Display help screen</td></tr><tr class="even"><td>q</td><td>Quit less mode</td></tr></tbody></table><h3 id="copy-and-paste-trick">Copy and Paste Trick</h3><hr /><p>Double click on a file name and middle click to paste it intocommands.</p><h3 id="wandering-around-the-file-system">Wandering Around the FileSystem</h3><hr /><table><colgroup><col style="width: 50%" /><col style="width: 50%" /></colgroup><thead><tr class="header"><th>Drectory</th><th>Comments</th></tr></thead><tbody><tr class="odd"><td>/</td><td>The root directory. Where everything begins.</td></tr><tr class="even"><td>/bin</td><td>Contains binaries(program) that must be present for the system toboot and run.</td></tr><tr class="odd"><td>/boot</td><td>Contains the linux kernel, initial RAM disk image(for drivers'needed at boot time), and the boot loader.</td></tr><tr class="even"><td>/dev</td><td>This is a special directory which contains device nodes. Here iswhere the kernel maintains a list of all the devices it understand.</td></tr><tr class="odd"><td>/etc</td><td>The etc directory contains all the system-wide configuration files.It also contains a collection of shell scripts which start each of thesystem devices at boot time. Everything in this directory should bereadable text files.</td></tr><tr class="even"><td>/home</td><td>In normal configurations, each user is given a directory in /home.Ordinary users can only write their files in their home directories.This limitation protects system from errant user activity.</td></tr><tr class="odd"><td>/lib</td><td>Contained shared library files used by the core system programs.These are similar to DLL in windows.</td></tr><tr class="even"><td>/lost+found</td><td>Each formatted partition or device using a linux file system, suchas ext3, will have this directory. It is used in the case of a partialrecovery from a file system corruption event. Unless something reallybad has happened to your system, this directroy will remain empty.</td></tr><tr class="odd"><td>/media</td><td>On modern Linux systems the /media directory will contain the mountpoints for removable media such USB drives, CD-ROMs, etc. that aremounted automatically at insertion.</td></tr><tr class="even"><td>/mnt</td><td>On older Linux systems, the /mnt directory contains mount points forremovable devices that have been mounted manually.</td></tr><tr class="odd"><td>/opt</td><td>The /opt directory is used to install “optional” software. This ismainly used to hold commercial software products that may be installedon your system.</td></tr><tr class="even"><td>/proc</td><td>The /proc directory is special. It's not a real file system in thesense of files stored on your hard drive. Rather, it is a virtual filesystem maintained by the Linux kernel. The “files” it contains arepeepholes into the kernel itself. The files are readable and will giveyou a picture of how the kernel sees your computer.</td></tr><tr class="odd"><td>/root</td><td>This is the home directory for the root account.</td></tr><tr class="even"><td>/sbin</td><td>This directory contains “system” binaries. These are programs thatperform vital system tasks that are generally reserved for thesuperuser.</td></tr><tr class="odd"><td>/tmp</td><td>The /tmp directory is intended for storage of temporary, transientfiles created by various programs. Some configurations cause thisdirectory to be emptied each time the system is rebooted.</td></tr><tr class="even"><td>/usr</td><td>The /usr directory tree is likely the largest one on a Linux system.It contains all the programs and support files used by regularusers.</td></tr><tr class="odd"><td>/usr/bin</td><td>/usr/bin contains the executable programs installed by your Linuxdistribution. It is not uncommon for this directory to hold thousands ofprograms.</td></tr><tr class="even"><td>/usr/lib</td><td>The shared libraries for the programs in /usr/bin.</td></tr><tr class="odd"><td>/usr/local</td><td>The /usr/local tree is where programs that are not included withyour distribution but are intended for system- wide use are installed.Programs compiled from source code are normally installed in/usr/local/bin. On a newly installed Linux system, this tree exists, butit will be empty until the system administrator puts something init.</td></tr><tr class="even"><td>/usr/sbin</td><td>Contains more system administration programs.</td></tr><tr class="odd"><td>/usr/share</td><td>/usr/share contains all the shared data used by programs in/usr/bin. This includes things like default configuration files, icons,screen backgrounds, sound files, etc.</td></tr><tr class="even"><td>/usr/share/doc</td><td>Most packages installed on the system will include some kind ofdocumentation. In /usr/share/doc, we will find documentation filesorganized by package.</td></tr><tr class="odd"><td>/var</td><td>With the exception of /tmp and /home, the directories we have lookedat so far remain relatively static, that is, their contents don'tchange. The /var directory tree is where data that is likely to changeis stored. Various databases, spool files, user mail, etc. are locatedhere.</td></tr><tr class="even"><td>/var/log</td><td>/var/log contains log files, records of various system activity.These are very important and should be monitored from time to time. Themost useful one is /var/log/messages. Note that for security reasons onsome systems, you must be the superuser to view log files.</td></tr></tbody></table><h3 id="symbolic-links">Symbolic links</h3><hr /><p>In most Unix-like system, it is possible to have a file referenced bymultiple names.</p><h2 id="manipulating-system">Manipulating System</h2><hr /><h3 id="wildcard">Wildcard</h3><hr /><table><colgroup><col style="width: 50%" /><col style="width: 50%" /></colgroup><thead><tr class="header"><th>Wildcard</th><th>Meaning</th></tr></thead><tbody><tr class="odd"><td>*</td><td>Matches any characters.</td></tr><tr class="even"><td>?</td><td>Match any single character.</td></tr><tr class="odd"><td>[characters]</td><td>Matches any character that is a member of the set characters.</td></tr><tr class="even"><td>[[:class:]]</td><td>Matches any character that is a member of the specified class.</td></tr></tbody></table><table><thead><tr class="header"><th>Character class</th><th>Meaning</th></tr></thead><tbody><tr class="odd"><td>[:alnum:]</td><td>Matches any alphanumeric character.</td></tr><tr class="even"><td>[:alpha:]</td><td>Matches any alphabetic character.</td></tr><tr class="odd"><td>[:digit:]</td><td>Matches any numeral.</td></tr><tr class="even"><td>[:lower:]</td><td>Matches any lowercase letter.</td></tr><tr class="odd"><td>[:upper:]</td><td>Matches any uppercase letter.</td></tr></tbody></table><h3 id="mkdir">mkdir</h3><hr /><p>Create a new file by this command. <code>mkdir[filename]...</code></p><p>We can create more than one file one time.</p><h3 id="cp--copy-the-file-or-the-directory">cp- copy the file or thedirectory</h3><hr /><p><code>cp item1 item2</code> <code>cp [item]... [directory]</code></p><p>We can copy one item or multiple items into the target file.</p><h3 id="useful-option-for-cp">Useful Option For cp</h3><hr /><table><colgroup><col style="width: 50%" /><col style="width: 50%" /></colgroup><thead><tr class="header"><th>Option</th><th>Meaning</th></tr></thead><tbody><tr class="odd"><td>-a, --archive</td><td>Copy the files and directories and all of their attributes,including ownerships and permissions. Normally, copies take on thedefault attributes of the user performing the copy.</td></tr><tr class="even"><td>-i, --interactive</td><td>Before overwriting an existing file, prompt the user forconfirmation. If this option is not specified, cp will silentlyoverwrite files.</td></tr><tr class="odd"><td>-r, --recursive</td><td>Recursively copy directories and their contents. This option (or the-a option) is required when copying directories.</td></tr><tr class="even"><td>-u, --update</td><td>When copying files from one directory to another, only copy filesthat either don't exist, or are newer than the existing correspondingfiles, in the destination directory.</td></tr><tr class="odd"><td>-v, --verbose</td><td>Display informative messages as the copy is performed.</td></tr></tbody></table><h3 id="mv--move-and-rename-the-file">mv- move and rename the file</h3><hr /><p><code>mv item1 item2</code></p><p>Move or rename the "item1" to "item2".</p><p><code>mv [item]... [directory]</code></p><p>Move one or more items from one directory to another.</p><h3 id="useful-option-for-mv">Useful option For mv</h3><hr /><table><colgroup><col style="width: 50%" /><col style="width: 50%" /></colgroup><thead><tr class="header"><th>Option</th><th>Meaning</th></tr></thead><tbody><tr class="odd"><td>-i --interactive</td><td>Before overwriting an existing file, prompt the user forconfirmation. If this option is not specified, mv command will silentlyoverwrite files.</td></tr><tr class="even"><td>-u --update</td><td>When moving files from one directory to another, only move filesthat either don't exist, or are newer than the existing correspondingfiles in the destination directory.</td></tr><tr class="odd"><td>-v --verbose</td><td>Display informative messages as the move is performed.</td></tr></tbody></table><h3 id="rm-">rm-!!!</h3><hr /><blockquote><p>rm item...</p></blockquote><p>Remove the item by this command.</p><h3 id="useful-option-for-rm">Useful option for rm</h3><hr /><table><colgroup><col style="width: 50%" /><col style="width: 50%" /></colgroup><thead><tr class="header"><th>Option</th><th>Meaning</th></tr></thead><tbody><tr class="odd"><td>-i, --interactive</td><td>Before deleting an existing file, prompt the user for confirmation.If this option is not specified, rm will silently delete files.</td></tr><tr class="even"><td>-r, --recursive</td><td>Recursively delete directories. This means that if a directory beingdeleted has subdirectories, delete them too. To delete a directory, thisoption must be specified.</td></tr><tr class="odd"><td>-f, --force</td><td>Ignore nonexistent files and do not prompt. This overrides the--interactive option.</td></tr><tr class="even"><td>-v, --verbose</td><td>Display informative messages as the deletion is performed.</td></tr></tbody></table><p>Before you use wildcards with rm, besides carefully checking yourspelling, you'd better use ls to examine the file or directory you aregoing to remove.</p><h3 id="ln--create-links">ln- create links</h3><hr /><p>It is used to create hard or symbolic links.</p><p>Create a hard link:</p><blockquote><p>ln file link</p></blockquote><p>Create a symbolic link:</p><blockquote><p>ln -s item link</p></blockquote><h3 id="what-is-hard-link">What is hard link</h3><hr /><p>Hard links are the original Unix way of creating links; symboliclinks are more modern. By default, every file has a single hard linkthat gives the file its name. When we create a hard link, we create anadditional directory entry for a file.</p><p>Here are the two limitation of hard link:</p><ul><li>A hard link cannot reference a file outside its own file system.This means a link may not reference a file that is not on the same diskpartition as the link itself.</li><li>A hard link may not reference a directory.</li></ul><p>A hard link is indistinguishable from the file itself. Unlike asymbolic link, when you list a directory containing a hard link you willsee no special indication of the link. When a hard link is deleted, thelink is removed but the contents of the file itself continue to exist(that is, its space is not deallocated) until all links to the file aredeleted.</p><h3 id="symbolic-link">Symbolic link</h3><hr /><p>Symbolic links were created to overcome the limitations of hardlinks. Symbolic links work by creating a special type of file thatcontains a text pointer to the referenced file or directory. In thisregard, they operate in much the same way as a Windows shortcut thoughof course.</p><p>A file pointed to by a symbolic link, and the symbolic link itselfare largely indistinguishable from one another. For example, if youwrite some something to the symbolic link, the referenced file is alsowritten to. However when you delete a symbolic link, only the link isdeleted, not the file itself. If the file is deleted before the symboliclink, the link will continue to exist, but will point to nothing. Inthis case, the link is said to be broken. In many implementations, thels command will display broken links in a distinguishing color, such asred, to reveal their presence.</p><h3 id="creating-symlinks-with-gui">Creating Symlinks with GUI</h3><hr /><p>The file managers in both GNOME and KDE provide an easy and automaticmethod of creating symbolic links. With GNOME, holding the Ctrl+Shiftkeys while dragging a file will create a link rather than copying (ormoving) the file. In KDE, a small menu appears whenever a file isdropped, offering a choice of copying, moving, or linking the file.</p><h2 id="using-command">Using Command</h2><hr /><p>What is command?</p><ul><li>An executable program like all those files we saw in /usr/bin.Within this category, programs can be compiled binaries such as programswritten in C and C++, or programs written in scripting languages such asthe shell, perl, python, ruby, etc.</li><li>A command built into the shell itself. bash supports a number ofcommands internally called shell builtins. The cd command, for example,is a shell builtin.</li><li>A shell function. These are miniature shell scripts incorporatedinto the environment. We will cover configuring the environment andwriting shell functions in later chapters.</li><li>An alias. Commands that we can define ourselves, built from othercommands.</li></ul><h3 id="type---show-the-type-of-the-command">type - show the type of thecommand</h3><hr /><p>The type command is a shell builtin that displays the kind of commandthe shell will execute, given a particular command name.</p><p><code>type [command]</code></p><h3 id="which---show-the-position-of-executable-program.">which - showthe position of executable program.</h3><hr /><p>which only works for executable programs, not builtins nor aliasesthat are substitutes for actual executable programs.</p><p>To determine the exact location of a given executable, the whichcommand is used: <code>which [command]</code></p><h3 id="help-get-the-document-of-the-builtins-for-the-shell">help-getthe document of the builtins for the shell</h3><hr /><p>Bash has a built-in help facility available for each of the shellbuiltins. To use it, type “help” followed by the name of the shellbuiltin. <code>help [command]</code></p><p>A note on notation: When square brackets appear in the description ofa command’s syntax, they indicate optional items. A vertical barcharacter indicates mutually exclusive items.</p><h3 id="help---show-the-information-of-command">--help - show theinformation of command</h3><hr /><p>Many executable programs support a “--help” option that displays adescription of the command’s supported syntax and options.</p><p><code>[command] --help</code></p><h3id="man---call-for-the-documentation-called-man-page-manual-page">man -call for the documentation called man page / manual page</h3><hr /><p><code>man [command]</code></p><p>On most Linux systems, man uses less to display the manual page, soall of the familiar less commands work while displaying the page.</p><p>The “manual” that man displays is broken into sections and not onlycovers user commands but also system administration commands,programming interfaces, file formats and more.</p><table><thead><tr class="header"><th>Section</th><th>Contents</th></tr></thead><tbody><tr class="odd"><td>1</td><td>User commands</td></tr><tr class="even"><td>2</td><td>Programming interfaces kernel system calls</td></tr><tr class="odd"><td>3</td><td>Programming interfaces to the C library</td></tr><tr class="even"><td>4</td><td>Special files such as device nodes and drivers</td></tr><tr class="odd"><td>5</td><td>File formats</td></tr><tr class="even"><td>6</td><td>Games and amusements such as screen savers</td></tr><tr class="odd"><td>7</td><td>Miscellaneous</td></tr><tr class="even"><td>8</td><td>System administration commands</td></tr></tbody></table><p>Sometimes we need to look in a specific section of the manual to findwhat we are looking for. This is particularly true if we are looking fora file format that is also the name of a command. Without specifying asection number, we will always get the first instance of a match,probably in section 1.</p><h3 id="apropos---show-the-proper-command">apropos - show the propercommand</h3><hr /><p><code>apropos [keyword]</code> The first field in each line of outputis the name of the man page, the second field shows the section. Notethat the man command with the “-k” option performs the exact samefunction as apropos.</p><h3 id="whatis---very-simple-introduction">whatis - very simpleintroduction</h3><hr /><p>The whatis program displays the name and a one line description of aman page matching a specified keyword.</p><h3 id="info---show-the-contents-of-program">info - show the contents ofprogram</h3><hr /><p>The GNU Project provides an alternative to man pages for theirprograms, called “info.” Info pages are displayed with a reader programnamed, appropriately enough, info. Info pages are hyperlinked much likeweb pages.</p><p>The info program reads info files, which are tree structured intoindividual nodes, each containing a single topic. Info files containhyperlinks that can move you from node to node. A hyperlink can beidentified by its leading asterisk, and is activated by placing thecursor upon it and pressing the enter key.</p><p>To invoke info, type “info” followed optionally by the name of aprogram. Below is a table of commands used to control the reader whiledisplaying an info page.</p><table><colgroup><col style="width: 50%" /><col style="width: 50%" /></colgroup><thead><tr class="header"><th>Commands</th><th>Action</th></tr></thead><tbody><tr class="odd"><td>?</td><td>Display command help</td></tr><tr class="even"><td>PageUp or Backspace</td><td>Display privious page</td></tr><tr class="odd"><td>PgDn or Space</td><td>Display next page</td></tr><tr class="even"><td>n</td><td>Display next node</td></tr><tr class="odd"><td>p</td><td>Previous - Display the previous node</td></tr><tr class="even"><td>u</td><td>Up - Display the parent node of the currently displayed node,usually a menu.</td></tr><tr class="odd"><td>Enter</td><td>Follow the hyperlink at the cursor location</td></tr><tr class="even"><td>q</td><td>quit</td></tr></tbody></table><p>Most of the command line programs we have discussed so far are partof the GNU Project’s “coreutils” package. So typing:</p><p><code>info coreutils</code></p><p>will display a menu page with hyperlinks to each program contained inthe coreutils package.</p><h3 id="alias---create-your-own-command">alias - create your owncommand</h3><hr /><p>Now for our very first experience with programming! We will create acommand of our own using the alias command. But before we start, we needto reveal a small command line trick. It’s possible to put more than onecommand on a line by separating each command with a semicoloncharacter.</p><p>Now let’s turn this sequence into a new command using alias. Thefirst thing we have to do is dream up a name for our new command. Let’stry “test”. Before we do that, it would be a good idea to find out ifthe name “test” is already being used.</p><p>To create our alias: <code>alias [your command]= "[the commandstring]"</code></p><p>After the command “alias” we give alias a name followed immediately(no whitespace allowed) by an equals sign, followed immediately by aquoted string containing the meaning to be assigned to the name. Afterwe define our alias, it can be used anywhere the shell would expect acommand.</p><p>To remove an alias, the unalias command is used, like so:</p><p><code>unalias [your command]</code></p><p>To see all the aliases defined in the environment, use the aliascommand without arguments. Here are some of the aliases defined bydefault on a Fedora system.</p><p>There is one tiny problem with defining aliases on the command line.They vanish when your shell session ends.</p><h2 id="redirection">Redirection</h2><hr /><h3 id="stdin-stdout-stderr">stdin, stdout, stderr</h3><hr /><p>Many of the programs that we have used so far produce output of somekind. This output often consists of two types. First, we have theprogram’s results; that is, the data the program is designed to produce,and second, we have status and error messages that tell us how theprogram is getting along.</p><p>Keeping with the Unix theme of “everything is a file,” programs suchas ls actually send their results to a special file called standardoutput (often expressed as stdout) and their status messages to anotherfile called standard error (stderr). By default, both standard outputand standard error are linked to the screen and not saved into a diskfile. In addition, many programs take input from a facility calledstandard input (stdin) which is, by default, attached to thekeyboard.</p><p>I/O redirection allows us to redefine where standard output goes. Toredirect standard output to another file besides the screen, we use the“>” redirection operator followed by the name of the file. It’s oftenuseful to store the output of a command in a file. For example, we couldtell the shell to send the output of the ls command to the filels-output.txt instead of the screen</p><h3 id="stdout-redirection">stdout redirection</h3><hr /><p>When we redirect output with the “>” redirection operator, thedestination file is always rewritten from the beginning.</p><p>In fact, if we ever need to actually truncate a file (or create anew, empty file) we can use a trick like this:</p><p><code>> [filename]</code></p><p>We can append redirected output to a file instead overwriting thefile from the file in this way:</p><p><code>[stdout needed to be redirected] >>[destination]</code></p><p>Using the “>>” operator will result in the output beingappended to the file. If the file does not already exist, it is createdjust as though the “>” operator had been used.</p><h3 id="stderr-redirection">stderr redirection</h3><hr /><p>Redirecting standard error lacks the ease of a dedicated redirectionoperator. To redirect standard error we must refer to its filedescriptor. A program can produce output on any of several numbered filestreams. While we have referred to the first three of these file streamsas standard input, output and error, the shell references theminternally as file descriptors zero, one and two, respectively. Theshell provides a notation for redirecting files using the filedescriptor number.</p><p><code>[stderr needed to be redirected] 2> [destination]</code></p><p>The file descriptor “2” is placed immediately before the redirectionoperator to perform the redirection of standard error to the file.</p><h3 id="stdout-stderr-redirection">stdout & stderr redirection</h3><hr /><p>There are cases in which we may wish to capture all of the output ofa command to a single file. To do this, we must redirect both standardoutput and standard error at the same time. There are two ways to dothis. First, the traditional way, which works with old versions of theshell:</p><p><code>[content needed to be redirected] > [destination]2>&1</code></p><p>Using this method, we perform two redirections. First we redirectstandard output to the file and then we redirect file descriptor two(standard error) to file descriptor one (standard output) using thenotation 2>&1.</p><p>Notice that the order of the redirections is significant. Theredirection of standard error must always occur after redirectingstandard output or it doesn’t work.</p><p>Recent versions of bash provide a second, more streamlined method forperforming this combined redirection:</p><p><code>[content needed to be redirected] &>[destination]</code></p><p>In this example, we use the single notation &> to redirectboth standard output and standard error to the file.</p><h3 id="handle-with-the-meaningless-output">Handle with the meaninglessoutput</h3><hr /><p>Sometimes “silence is golden,” and we don’t want output from acommand, we just want to throw it away. This applies particularly toerror and status messages. The system provides a way to do this byredirecting output to a special file called “/dev/null”. This file is asystem device called a bit bucket which accepts input and does nothingwith it. To suppress error messages from a command, we do this:</p><p><code>[rubbish information] 2> /dev/null</code></p><h3 id="cat--concatenate">cat- concatenate</h3><hr /><p>The cat command reads one or more files and copies them to standardoutput like so:</p><p><code>cat [file]</code></p><p>In most cases, you can think of cat as being analogous to the TYPEcommand in DOS. You can use it to display files without paging.</p><p>If cat is not given any arguments, it reads from standard input andsince standard input is, by default, attached to the keyboard, it’swaiting for us to type something.</p><p>After you typed something, type a Ctrl-d (i.e., hold down the Ctrlkey and press “d”) to tell cat that it has reached end of file (EOF) onstandard input. In the absence of filename arguments, cat copiesstandard input to standard output, so we see our line of textrepeated.</p><p>We can play cat in this way:</p><p><code>cat > [file]</code></p><p>Type the command followed by the text we want in to place in thefile. Remember to type Ctrl-d at the end.</p><p>Using the “<” redirection operator, we change the source ofstandard input from the keyboard to the file.</p><h3 id="pipe-line">| - Pipe Line</h3><hr /><p>The ability of commands to read data from standard input and send tostandard output is utilized by a shell feature called pipelines. Usingthe pipe operator “|” (vertical bar), the standard output of one commandcan be piped into the standard input of another.</p><p><code>command1 | command2</code></p><h3 id="filters">Filters</h3><hr /><p>Pipelines are often used to perform complex operations on data. It ispossible to put several commands together into a pipeline. Frequently,the commands used this way are referred to as filters.</p><h3 id="uniq---report-or-ingore-duplicates">uniq - report or ingoreduplicates</h3><hr /><p>The uniq command is often used in conjunction with sort. uniq acceptsa sorted list of data from either standard input or a single filenameargument (see the uniq man page for details) and, by default, removesany duplicates from the list.</p><p>If we want to see the list of duplicates instead, we add the “-d”option to uniq.</p><h3 id="wc---print-the-number-of-lines-words-and-bytes">wc - print thenumber of lines, words, and bytes</h3><hr /><p>Like our previous commands, if executed without command linearguments, wc accepts standard input. The “-l” option limits its outputto only report lines. Adding it to a pipeline is a handy way to countthings.</p><h3 id="grep---find-text-patterns">grep - find text patterns</h3><hr /><p><code>grep pattern [file...]</code></p><p>When grep encounters a “pattern” in the file, it prints out the linescontaining it.</p><p>There are a couple of handy options for grep: “-i” which causes grepto ignore case when performing the search (normally searches are casesensitive) and “-v” which tells grep to only print lines that do notmatch the pattern.</p><h3 id="head-tail---print-the-head-or-the-tail-of-the-file">head, tail -print the head or the tail of the file</h3><hr /><p>Sometimes you don’t want all of the output from a command. You mayonly want the first few lines or the last few lines. The head commandprints the first ten lines of a file and the tail command prints thelast ten lines. By default, both commands print ten lines of text, butthis can be adjusted with the “-n” option.</p><p><code>head -n [numbers of line you wanna print] [file...]</code></p><p>tail has an option which allows you to view files in real-time. Thisis useful for watching the progress of log files as they are beingwritten. In the following example, we will look at the messages file in/var/log. Superuser privileges are required to do this on some Linuxdistributions, since the /var/log/messages file may contain securityinformation.</p><p>Using the “-f” option, tail continues to monitor the file and whennew lines are appended, they immediately appear on the display. Thiscontinues until you type Ctrl-c.</p><h3 id="tee---get-the-transport-from-stderr-to-stdout-and-file">tee -get the transport from stderr to stdout and file</h3><hr /><p>In keeping with our plumbing metaphor, Linux provides a commandcalled tee which creates a “tee” fitting on our pipe. The tee programreads standard input and copies it to both standard output (allowing thedata to continue down the pipeline) and to one or more files. This isuseful for capturing a pipeline’s contents at an intermediate stage ofprocessing.</p><h3 id="useful-toturial">Useful Toturial</h3><hr /><p><ahref="https://catonmat.net/bash-one-liners-explained-part-three">catonmat</a></p><h2 id="staring-at-the-world-from-shell">Staring at the world fromShell</h2><hr /><h3 id="characterexpansion">(Character)Expansion</h3><hr /><p>With expansion, youo type something and it is expanded into somethingele before shell acts upon it.</p><p>echo is a shell builtin that performs a very simple task. It printsout its text arguments on stout.</p><p><code>echo [content]</code></p><p>However, if your input some wildcard for its arguments, the shellwill expands the wildcard into something else. When the enter key ispressed, the shell automatically expands any qualifying characters onthe command line before the command is carried out, so the echo only sawthe expanded result.</p><h3 id="pathname-expansion">Pathname Expansion</h3><hr /><p><code>echo [regular expression]</code></p><p>If we wanna show the hidden files of the current directory,<code>echo .*</code> will also dispaly "." & ".." for us. To avoidthis result, correct command is this <code>echo .[!.]?*</code> or<code>ls -d .[!.]?*</code></p><h3 id="the-tilde-expansion">~ - the tilde expansion</h3><hr /><p>When used at the beginning of the word, it expands into the name ofthe home directory of the name user, or if no user is named, the homedirectory of the current user.</p><h3 id="arithmetic-expansion">Arithmetic expansion</h3><hr /><p>Shell allow us to use the shell prompt as a calculator.</p><p><code>echo $((expression))</code></p><p>Where expression is an arithmetic expression consisting of values andarithmetic operators.</p><p>Arithmetic expansion only support integers, but can perform quite anumber of different operations. Here are a few of the supportedoperators.</p><p>Space are not significant in arithmetic expressions and expressionsmay be nested.</p><h3 id="brace-expansion">Brace Expansion</h3><hr /><p>With it, you can create multiple text strings from a patterncontaining braces. Patterns to be brace expanded may contain a leadingportion called a preamble and a trailing portion called a postscript.The pattern may not contain embedded whitespace.</p><p>Here is an example using a range of integers: <code>echoNumber_{1..5}</code>. And the output is<code>Number_1 Number_2 Number_3 Number_4 Number_5</code>.</p><p>Another example: <code>echo {Z..A}</code></p><p>And nexted: <code>echo a{A{1,2},B{3,4}}b</code></p><h3 id="parameter-expansion">Parameter Expansion</h3><hr /><p>It’s a feature that is more useful in shell scripts than directly onthe command line. Many of its capabilities have to do with the system’sability to store small chunks of data and to give each chunk a name.Many such chunks, more properly called variables, are available for yourexamination. With parameter expansion, if you misspell the name of avariable, the expansion will still take place, but will result in anempty string.</p><h3 id="command-substitions">Command Substitions</h3><hr /><p>Command substitution allows us to use the output of a command as anexpansion: <code>echo $(ls)</code>. And this will print out thefiles'names which are in the current directory.</p><p>We are not limited to just simple commands. Entire pipelines can beused (only partial output shown): <code>file $(ls /usr/bin/* | grepzip)</code></p><h3 id="double-quotes">Double Quotes</h3><hr /><p>If you place text inside double quotes, all the special charactersused by the shell lose their special meaning and are treated as ordinarycharacters. The exceptions are $, (backslash), and ` (back-quote). Thismeans that word-splitting, pathname expansion, tilde expansion, andbrace expansion are suppressed, but parameter expansion, arithmeticexpansion, and command substitution are still carried out. Using doublequotes, we can cope with filenames containing embedded spaces.</p><p>By default, word-splitting looks for the presence of spaces, tabs,and newlines (linefeed characters) and treats them as delimiters betweenwords. This means that unquoted spaces, tabs, and newlines are notconsidered to be part of the text. They only serve as separators. Sincethey separate the words into different arguments, our example commandline contains a command followed by four distinct arguments.</p><pre class="line-numbers language-none"><code class="language-none">$ echo $(cal)February 2008 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 1415 16 17 18 19 20 21 22 23 24 25 26 27 28 29$ echo "$(cal)"February 2008....<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>In the first instance, the unquoted command substitution resulted ina command line containing thirty-eight arguments. In the second, acommand line with one argument that includes the embedded spaces andnewlines.</p><h3 id="single-quotes">Single quotes</h3><hr /><p>If we need to suppress all expansions, we use single quotes.<pre class="line-numbers language-none"><code class="language-none">$ echo text ~/*.txt {a,b} $(echo foo) $((2+2)) $USERtext /home/me/ls-output.txt a b foo 4 me$ echo "text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER"text ~/*.txt {a,b} foo 4 me$ echo 'text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER'text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></p><h3 id="escape-character">Escape Character</h3><hr /><p>Sometimes we only want to quote a single character. To do this, wecan precede a character with a backslash, which in this context iscalled the escape character. <code>echo "The balance for user $USER is:\$5.00"</code></p><p>And '$' will just be thought as a character.</p><p>It is also common to use escaping to eliminate the special meaning ofa character in a filename. For example, it is possible to use charactersin filenames that normally have special meaning to the shell. Thesewould include “$”, “!”, “&”, “ “, and others. <code>mvbad\&filename good_filename</code></p><p>Always remember, to allow a backslash character to appear, escape itby typing “”. Note that within single quotes, the backslash loses itsspecial meaning and is treated as an ordinary character.</p><h2 id="advanced-skills-of-keyboard-operating">Advanced Skills OfKeyboard Operating</h2><hr /><p>bash uses a library (a shared collection of routines that differentprograms can use) called Readline to implement command line editing.</p><h3 id="move-the-cursor">Move The Cursor</h3><hr /><table><colgroup><col style="width: 50%" /><col style="width: 50%" /></colgroup><thead><tr class="header"><th>Key</th><th>Action</th></tr></thead><tbody><tr class="odd"><td>Ctrl-a</td><td>Move cursor to the beginning of the line.</td></tr><tr class="even"><td>Ctrl-e</td><td>Move cursor to the end of the line.</td></tr><tr class="odd"><td>Ctrl-f</td><td>Move cursor forward one character;same as the right arrow key.</td></tr><tr class="even"><td>Ctrl-b</td><td>Move cursor backward one character;same as the left arrow key.</td></tr><tr class="odd"><td>Alt-f</td><td>Move cursor forward one word.</td></tr><tr class="even"><td>Alt-b</td><td>Move cursor backward one word.</td></tr><tr class="odd"><td>Ctrl-l</td><td>Clear the screen and move the cursor to the top left corner. Theclear command does the same thing.</td></tr></tbody></table><h3 id="edit-text">Edit Text</h3><hr /><table><colgroup><col style="width: 50%" /><col style="width: 50%" /></colgroup><thead><tr class="header"><th>Key</th><th>Action</th></tr></thead><tbody><tr class="odd"><td>Ctrl-d</td><td>Delete the character at the cursor location.</td></tr><tr class="even"><td>Ctrl-t</td><td>Transpose(exchange)the character at the cursor location with the onepreceding it.</td></tr><tr class="odd"><td>Alt-t</td><td>Transpose the word at the cursor location with the one precedingit.</td></tr><tr class="even"><td>Alt-u</td><td>Convert the characters from the cursor location to the end of theword to uppercase.</td></tr></tbody></table><p>.... Needed to be completed</p><h2 id="rights">Rights</h2><hr /><h3 id="owner-group-member">Owner, Group Member</h3><hr /><p>In the Unix security model, a user may own files and directories.When a user owns a file or directory, the user has control over itsaccess. Users can, in turn, belong to a group consisting of one or moreusers who are given access to files and directories by their owners. Inaddition to granting access to a group, an owner may also grant some setof access rights to everybody, which in Unix terms is referred to as theworld. To find out information about your identity, use the id command.<code>id</code> and its result <code>uid= gid= groups=</code></p><p>When user accounts are created, users are assigned a number called auser ID or uid which is then, for the sake of the humans, mapped to auser name. The user is assigned a primary group ID or gid and may belongto additional groups.</p><p>Fedora starts its numbering of regular user accounts at 500, whileUbuntu starts at 1000. We can see that the Ubuntu user belongs to a lotmore groups.</p><p>User accounts are defined in the /etc/passwd file and groups aredefined in the /etc/group file. When user accounts and groups arecreated, these files are modified along with /etc/shadow which holdsinformation about the user’s password. For each user account, the/etc/passwd file defines the user (login) name, uid, gid, the account’sreal name, home directory, and login shell. If you examine the contentsof /etc/passwd and /etc/group, you will notice that besides the regularuser accounts, there are accounts for the superuser (uid 0) and variousother system users.</p><h3 id="read-access-write-access-and-execution-access">Read Access,Write Access, and Execution Access</h3><hr /><p>When we use <code>ls -l</code> command, we can see some symbol in thefront of the result. Let's try to understand these symbol. The first tencharacters of the listing are the file attributes. The first of thesecharacters is the file type: | Attribute | File Type | |---|---| | - | aregular file | | d | A directory | | l | A symbolic link. Notice thatwith symbolic links, the remainning file attributes are always“rwxrwxrwx” and are dummy values. The real file attributes are those ofthe file the symbolic link points to. | | c | A character special file.This file type refers to a device that handles data as a stream ofbytes, such as a terminal or modem. | | b | A block special file. Thisfile type refers to a device that handles data in blocks, such as a harddrive or CD-ROM drive. |</p><p>The remaining nine characters of the file attributes, called the filemode, represent the read, write, and execute permissions for the file’sowner, the file’s group owner, and everybody else.</p><p>When set, the r, w, and x mode attributes have the following effecton files and directories: | Attribute | Files | Directories ||---|---|---| | r | Allows a file to be opened and read. | Allows adirectory's contents to be listed if the execute attribute is also set.| | w | Allows a file to be written to or truncated, however thisattribute does not allow files to be renamed or deleted. The ability todelete or rename files is determined by directory attributes. | Allowsfiles within a directory to be created, deleted, and renamed if theexecute attribute is also set. | | x | Allows a file to be treated as aprogram and executed. Program files written in scripting languages mustalso be set as readable to be executed. | Allows a directory to beentered, e.g., cd directory. |</p><h3 id="chmod--change-the-mode-of-a-file">chmod- Change The Mode Of AFile</h3><hr /><p>Be aware that only the file’s owner or the superuser can change themode of a file or directory. chmod supports two distinct ways ofspecifying mode changes: octal number representation, or symbolicrepresentation.</p><p>With octal notation we use octal numbers to set the pattern ofdesired permissions. Since each digit in an octal number representsthree binary digits, this maps nicely to the scheme used to store thefile mode(Comprehend these with converting it to binary). We use threeoctal as arguments to set the access to the the file’s owner, the file’sgroup owner, and everybody else.</p><table><thead><tr class="header"><th>Octal</th><th>File Mode</th></tr></thead><tbody><tr class="odd"><td>0</td><td>---</td></tr><tr class="even"><td>1</td><td>--x</td></tr><tr class="odd"><td>2</td><td>-w-</td></tr><tr class="even"><td>3</td><td>-wx</td></tr><tr class="odd"><td>4</td><td>r--</td></tr><tr class="even"><td>5</td><td>r-x</td></tr><tr class="odd"><td>6</td><td>rw-</td></tr><tr class="even"><td>7</td><td>rwx</td></tr></tbody></table><p>chmod also supports a symbolic notation for specifying file modes.Symbolic notation is divided into three parts: who the change willaffect, which operation will be performed, and what permission will beset. To specify who is affected, a combination of the characters “u”,“g”, “o”, and “a” is used as follows:</p><p>|---|---| | u | Short for "user", but means the file or directoryowner. | | g | Group owner. | | o | Short for "others", but means world.| | a | Short for "all", the combination of "u", "g", and "o". |</p><p>If no character is specified, “all” will be assumed. The operationmay be a “+” indicating that a permission is to be added, a “-”indicating that a permission is to be taken away, or a “=” indicatingthat only the specified permissions are to be applied and that allothers are to be removed.</p><p>example <code>chmod a+rw</code></p><h3 id="umask--set-the-default-access-permission">umask- Set The DefaultAccess Permission</h3><hr /><p>To understand the <em>umask</em> command, we need to convert octalnumber to binary number and translate it to the mode of the file. Thenthe argument you add after umask is to do an <em>xor</em> operation onthe default mode.</p>]]></content>
<categories>
<category> Programming </category>
</categories>
<tags>
<tag> Operating System </tag>
</tags>
</entry>
<entry>
<title>Building your own blog!</title>
<link href="2019/building-your-own-blog.html"/>
<url>2019/building-your-own-blog.html</url>
<content type="html"><![CDATA[<h1 id="overview">Overview</h1><hr /><h2 id="git">Git</h2><ul><li>Download <strong>git</strong>, after that use bash to operate.</li><li>Set the configuration (I will show how to set the configuration onmy another blog)</li><li>Create a new file to store your blog.</li><li>Then open <strong>git bash</strong> at this file.</li><li>Then we need to set SSH key</li></ul><h3 id="ssh-key">SSH key</h3><ul><li>Enter this command <code>ssh-keygen -t rsa -C "Your mailaddress"</code>.</li><li>Then you will see Git ask you to "Enter file in which to save thekey", just press Enter.</li><li>Then you will be asked to Enter a code, it is linux style, whichmeans that you can't see the code you type. <imgsrc="https://pic4.zhimg.com/80/v2-2cb74afa9b7c9077b33da54d4513523b_hd.jpg"alt="Enter your Passphrase" /></li><li>When you see this, it means your ssh key is successfully set. <imgsrc="http://img.voycn.com/images/2019/05/c0a0f40a943fd5126ff4b6706b9922ee.png"alt="Eureka" /></li><li>Find <code>.ssh\id_rsa.pub</code> (its path is showed in theintroduction after you successfully set the ssh key)</li><li>Use <strong>Nodepad++</strong> (because of stupid microsoft) to openthis file and copy its contents.</li><li>Open your <em>Github</em>, click your <em>picture</em>, then click<em>settings</em>, and click</li><li><strong>SSH and GPG keys</strong>, choose <strong>New sskey</strong><imgsrc="http://img.voycn.com/images/2019/05/28f50b516fc60b575b8318d0d3c8db51.png"alt="Set ssh key on your gitbub" /></li><li>Test this by <code>ssh -T git@github.com</code></li><li>Then use these two commands</li></ul><blockquote><p><code>git config --global user.name "your id"</code><br /><code>git config --global user.email "your email bond with yourgithub"</code></p></blockquote><h1 id="install-node.js">Install Node.js</h1><hr /><p>I will show how to install this on another blog.</p><h1 id="hexo">Hexo</h1><hr /><ul><li>Open <em>gitbash</em> at the place where you are going to set yourblog.</li><li>Enter <code>npm install hexo-cli -g</code></li><li>Then enter <code>hexo -v</code> to test whether Hexo is successfullyinstalled. <imgsrc="http://img.voycn.com/images/2019/05/4fca51e6b5457f5cd09efddf36843467.png"alt="Install Hexo" /></li><li>Enter <code>npm install hexo --save</code></li><li>To confirm the current document file is empty.</li><li>Run the command <code>hexo init</code></li><li>You can see this in your document file</li></ul><p><imgsrc="http://img.voycn.com/images/2019/05/89bb9e3a1f43a939055a72eabe76dbec.png" /></p><h1 id="begin-to-build-up-your-blog">Begin to build up your blog</h1><hr /><figure><imgsrc="http://img.voycn.com/images/2019/05/f61b966754e4d38bc7e10cc19d653682.png"alt="How to set your blog" /><figcaption aria-hidden="true">How to set your blog</figcaption></figure><ul><li>connect Hexo and your gitbub</li></ul><figure><imgsrc="http://img.voycn.com/images/2019/05/e416f23faabb4711ef25fcaf40c4b59b.png"alt="Link Hexo and githbu" /><figcaption aria-hidden="true">Link Hexo and githbu</figcaption></figure><ul><li><p>Then use <em>gitbash</em> ><code>hexo g</code> ><code>hexos</code> ><code>npm install hexo-deployer-git --save</code>><code>hexo d</code></p></li><li><p>Then you can see your blog by this site<code>https://yourid.github.io</code></p></li></ul>]]></content>
<categories>
<category> Misc </category>
</categories>
<tags>
<tag> Blog </tag>
<tag> Front-end </tag>
</tags>
</entry>
</search>