Live Note

Remain optimistic

要求

父进程创建 3 个进程,父进程等待子进程 2 运行完成之后,自行退出;其中子进程 1 运行系统命令“cp /bin/ls /tmp”;等待 2 秒后退出;子进程 2 使用标准 I/O 函数打开文件 src_file,向其内写入“this is process 2\n”,之后等待 5 秒后退出;子进程 3 处理为守护进程,每隔 5 秒向日志文件/var/log/messages 写入“this is process 3\n”

提交形式

  1. 代码
  2. 运行结果截图
Read more »

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
/**
* set the cookie
* @param {String} name
* @param {String} value
* @param {Number} day
*/
let setCookie = (name, value, day) => {
let date = new Date()
date.setDate(date.getDate() + day)
document.cookie = name + "=" + value + ";expires=" + date
}

/**
* get the cookie by name
* @param {String} name
*/
let getCookie = (name) => {
let str = document.cookie.split("; ").filter((i) => {
let result = i.split("=")
return result[0] == name
})
return str.length ? str[0].split("=")[1] : ""
}

/**
* check user
*/
let checkCookie = () => {
let username = document.cookie ? getCookie("username") : ""
if (username != "") {
alert("Welcome " + username)
} else {
username = prompt("please input your username")
if (username && username != "") {
setCookie("username", username, 7)
}
}
}

checkCookie()
console.log(getCookie("username"))

Linux 常用命令

  • man: an interface to the on-line reference manuals
  • su [options] [username]: change user ID or become superuser
  • useradd [options] LOGIN: create a new user or update default new user information
  • userdel [options] LOGIN: delete a user account and related files
  • passwd [options] [LOGIN]: change user password
  • ps [options]: report a snapshot of the current process.
  • kill [options] […]: send a signal to a process
  • fdisk [options] device: manipulate disk partition table
  • mount: mount a filesystem
  • chown [OPTION] … [OWNER]:[GROUP]] FILE …: change file owner and group
  • chgrp [OPTION] … GROUP FILE …: change group ownership
  • chmod [OPTION] … MODE[,MODE] … FILE …: change file mode bits
  • grep [OPTION] PATTERN [FILE…]: print lines matching a pattern
  • find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point…] [expression]: search for files in a directory hierarchy
  • ln: make links between files
  • gzip, gunzip, zcat: compress or expand files
  • tar: an archiving utility
  • diff [OPTION] … FILES: compare files line by line
  • patch -pnum < patchfile: apply a diff file to an original
  • ifconfig: configure a network interface

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
import System.Environment
import System.Directory
import System.IO
import Data.List

main = do
(command:args) <- getArgs -- get the command
let (Just action) = lookup command dispatch -- find the command
action args -- do something

add :: [String] -> IO ()
add [fileName, todoItem] = appendFile fileName (todoItem ++ "\n") -- add the todoItem

view :: [String] -> IO ()
view [fileName] = do -- match the filename
contents <- readFile fileName -- read the file
let todoTasks = lines contents -- get the contents
numberedTasks = zipWith (\n line -> show n ++ ": " ++ line) [0..] todoTasks
putStrLn $ unlines numberedTasks -- show the items

remove :: [String] -> IO ()
remove [fileName, numberString] = do -- get the filename and the line number
handle <- openFile fileName ReadMode
(tempName, tempHandle) <- openTempFile "." "temp" -- create a temp file
contents <- hGetContents handle -- get contents from the handle
let number = read numberString -- read the String to Number
todoTasks = lines contents
newTodoTasks = delete (todoTasks !! number) todoTasks -- remove the line of the number
hPutStr tempHandle $ unlines newTodoTasks -- putStr into the tempfile
hClose handle -- close handles
hClose tempHandle
removeFile fileName
renameFile tempName fileName

dispatch :: [(String, [String] -> IO ())]
dispatch = [
("add", add),
("view", view),
("remove", remove)
]

-- runhaskell arags-test.hs view newData.txt
-- runhaskell args-test.hs add newData.txt "Say Hello"
-- runhaskell args-test.hs remove newData.txt 4

文件类型

  • 普通文件
  • 目录文件
  • 链接文件:类似 Windows 的快捷方式,分软链接和硬链接
  • 设备文件:一般在/dev 目录下,一种是块设备文件,一种是字符设备文件

文件属性

-rwxrwxrwx

  • r: read
  • w: write
  • x: execute

第一个字符:

  • - : 普通文件
  • d : 目录文件
  • l : 链接文件
  • c : 字符设备
  • b : 块设备
  • p : 命名管道,如 FIFO
  • f : 堆栈文件,如 LIFO
  • s : 套接字

之后的三个三位字符组:

  1. 第一组代表文件拥有者(u)对该文件的权限
  2. 第二组代表文件用户组(g)对该文件的权限
  3. 第三组代表系统其它用户(o)对该文件的权限

文件系统类型

  • ext2 & ext3:ext3 是 ext2 的升级版本
  • swap:交换分区使用
  • vfat:DOS 中的系统(FAT12、FAT16 和 FAT32 等)
  • NFS:网络文件系统
  • ISO9660:光盘文件系统
Read more »

在某个论坛有人问了下面的一段代码

1
2
3
4
5
6
7
8
9
10
11
12
function f() {
console.log("outside")
}
function a() {
f()
{
function f() {
console.log("inside")
}
}
}
a()

问的是为什么在浏览器中是f is not a function
这个问题其实很好回答,存在函数提升,但是仔细想想又不对,因为函数提升是把整个函数都提升到当前作用域,所以按理来说 f 并不会是undefined
如果按照函数提升的话,结果应该是像这样:

1
2
3
4
5
6
7
8
9
10
11
function f() {
console.log("outside")
}

function a() {
function f() {
console.log("inside")
}
f() { }
}
a()

所以结果应该是inside才对,用 IE7 可以发现结果确实是 inside。
那这里为什么是 undefined 呢?
后面那位兄弟说在阮一峰的《ES6 入门》中说道,ES6 规定块级作用域中的函数定义规定为函数表达式。如果是定义为函数表达式的话,那就会像这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function f() {
console.log("outside")
}

function a() {
var f
f()
{
var f = function f() {
console.log("inside")
}
}
}
a()

这么一来这个undefined就可以解释了,因为只存在变量提升,而还未定义,所以就会出现undefined。但是那本书后还加了一句,浏览器并未遵循实现。
后面在 starkoverflow 上找到了一个老哥的回答,他的解释为这是因为strict & non-strict mode的不同。

  • let 和 const 在两种模式下行为一样
  • function 在两种模式下的行为就不是一样的,因为浏览器会扩展非严格模式,从而兼容老代码。

所以,在strict mode中,第一段代码输出的是outsidenon-strict mode中就会报错,因为会变成下面这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function f() {
console.log("outside")
}

function a() {
var f // function-scoped
f()
{
let f1 = function () {
console.log("inside")
} // block-scoped
f = f1
}
}
a()

MDN 中关于Block-level functions in non-strict code的也只有一句话: Don't.

又一次的见证历史。

卡西莫多失去了他心爱的姑娘,也失去了他心爱的钟楼。

大火照片1

大火照片2

大火照片3

废墟中的十字架依旧闪发着光芒

废墟

PS: 看到一条推,说你看大火都没有把十字架给损毁,难道你还不相信上帝吗?下面回:因为金的熔点比木高很多。XD

一篇文章

昨天晚上看了某乎的一篇文章,叫《比 996 更该死的那些人》,里面的开头:

这几年真的觉得这个世界真的已经变得不可救药了
勤劳和奋斗不仅不再被传颂和赞扬,竟然成了被鄙视,被视作愚蠢的品质。
而享乐,投机取巧成这些在过去难以启齿的行为,成了光明正大,人人争相炫耀的东西。

虽然这篇文章我开了开头就没看了,但是还是让我感触颇深,我不赞同第二句,但是第三句我是非常赞同的。在以前我是不会羡慕别人的,不知道从什么时候开始,我会越来越羡慕那些比我强、过的比我好、智商比我高的人。是欲望变了吗?我觉得是的。那个无欲无求的日子,我是不想去回忆的,因为对我来说,和坐牢一样。

从不会去鄙视那些勤劳和奋斗的人,因为他们不知道什么时候就已经追上你并且超过你了,学如逆水行舟,不进则退,但是我实在无法理解的是,投机取巧,为什么在某些人眼里成了政治正确。炫耀享乐,这个我还是可以理解的,毕竟大部分的人还是喜欢被别夸,被别人羡慕嫉妒,当然也有真正是为了分享快乐。

投机取巧的话,不知道是否是因为圈子不同,我觉得我周围认为投机取巧的人是正确的人还不少,甚至于在学校加大监管的情况下,组队进行“帮助”,并且在成绩还可以的时候,去蛊惑那些认真考试但成绩不佳的同学,试图拉他们下水。实在是不能理解那些人心里的想法,是为了让别人顺从自己从而获得心里的快感吗?我不太懂。似乎各大高校都有这种人的存在,这到底是为什么呢?

Read more »

自然数上的一般归纳原理

假设$P$是自然数上的一个性质,则如果

  1. $P(0)$成立 – base case
  2. 对所有的自然数$k$,$P(k)$蕴涵$P(k+1)$ – induction step

则$P(n)$对所有自然数$n$成立

$$
\ \ \ \ \ \ \ \ \ \ [P(k)]\
\frac{P(0)\ P(k+1)}{P(n)}
$$

$[P(k)]$为归纳假设,$k$不能出现在$P(k + 1)$的任何假设中

定理:每个自然是要么是偶数,要么是奇数。
用归纳法来证明:

  1. 0 是偶数 -> 0 是偶数或奇数
  2. 假设 k 是偶数或奇数,证明 k + 1 是奇数或偶数:
    1. k 是偶数,则 k + 1 是奇数
    2. k 是奇数,则 k + 1 是偶数
  3. 得证
Read more »

公式: $$P{Q}R\ ({P}Q{R})$$

P 和 R 都是一阶公式, 如果前提条件 P 在执行 Q 前成立, 则执行后得到满足条件 R 的状态
部分正确性断言: 如果 P 在 Q 执行前为真, 那么, 如果 Q 的执行终止,则终止在使 R 为真的某个状态
终止性断言:如果 P 在 Q 执行前为真, 那么 Q 将终止在使 R 为真的某个状态
赋值公理: $$\vdash P_0 {x:=f} P$$

推理规则的表示

$$\frac{premise -> f_0, f_1, …, f_n}{conclusion -> f_0}$$

推理规则

  • Rules of Consequence:
    $$\frac{P{Q}R,\ R\rightarrow S}{P{Q}S}\ \ \ \ \ \frac{P{Q}R,\ S\rightarrow P}{S{Q}R}$$
  • Rule of Composition:
    $$\frac{P{Q_1}R_1,\ R_1{Q_2}R}{P{Q_1, Q_2}R}$$
  • Rules of Iteration:
    $$\frac{P\ &\ B{S}P}{P\ {while\ B\ do\ S}\ \neg B \ &\ P}$$

等式公理

  • 代换: $[N/x]M$ 表示表示 M 中的自由变元 x 用 N 代换的结果, N 中的自由变元代换后不能成为约束变元
  • 约束变元改名: $\lambda x:\sigma .M = \lambda y:\sigma.[y/x]M$ 例如:$\lambda x:\sigma.x + y = \lambda z:\sigma .z+y$
  • 等价公理: 计算函数实际就是在函数中使用实在变元替换形式变元, $(\lambda x:\sigma.M)N = [N/x]M$
  • 同余性规则: 相等的函数作用于相等的变元产生相等的结果, $\frac{M_1=M_2,\ N_1=N_2}{M_1N_1=M_2N_2}$