CentOS配置静态IP

VirtualBox里安装CentOS,配置静态IP

(1)CentOS 6,修改/etc/sysconfig/network-scripts/ifcfg-eth0文件:

......
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.1.9
NETMASK=255.255.255.0
GATEWAY=192.168.1.1

(2)CentOS 7,修改/etc/sysconfig/network-scripts/ifcfg-enp0s3文件:

......
BOOTPROTO="static"
ONBOOT="yes"
IPADDR="192.168.1.5"
NETMASK="255.255.255.0"
GATEWAY="192.168.1.1"
DNS1="192.168.1.1"
DNS2="8.8.8.8"

 

进程的priority和nice

本文选自Difference between nice value and priority in the top output,以Linux系统为例讲解进程的prioritynice
(1)

The difference is that PR is a real priority of a process at the moment inside of the kernel and NI is just a hint for the kernel what the priority the process should have.

Priority反映当时进程真正的优先级,而nice则是告诉kernel进程应该获得什么样的优先级。

(2)Nice的值从-2019-20表示优先级最高。通常情况下,priority = nice + 20,也就是priority的值为0~39。但是上述理论仅仅适用于调度策略是SHED_OTHER的进程,此外,kernel也有可能只改变priority的值,而nice的值保持不变,因此上述等式同样不适用。

“Page out”和“swap out”

下文摘自Linux performance and tuning guidelines

The pages are used mainly for two purposes: page cache and process address space. The page cache is pages mapped to a file on disk. The pages that belong to a process address space (called anonymous memory because it is not mapped to any files, and it has no name) are used for heap and stack. When kswapd reclaims pages, it would rather shrink the page cache than page out (or swap out) the pages owned by processes. A large proportion of page cache that is reclaimed and process address space that is reclaimed might depend on the usage scenario and will affect performance. You can take some control of this behavior by using /proc/sys/vm/swappiness.

Page out and swap out: The phrases “page out” and “swap out” are sometimes confusing. The phrase “page out” means take some pages (a part of entire address space) into swap space while “swap out” means taking entire address space into swap space. They are sometimes used interchangeably.

Unix中的zombie进程和orphan进程

Unix中子进程退出后,如果父进程没有使用wait()函数获得子进程的退出状态,则子进程的相关信息仍然会在系统的进程表里占用一席之地,这时的子进程称之为zombie进程。如果父进程先于子进程退出,这时的子进程称之为orphan进程,而init进程则会变成orphan进程的父进程。init进程会定期处理父进程是initzombie进程。

参考资料:
Zombie process
Zombie process vs Orphan process

理解C语言中关于数组地址值的两个程序

程序1:

#include <stdio.h>

char ga[] = "abcdefghijklm";

void my_array_func(char ca[10]) {
    printf("%#x, %d\n", &ca, sizeof(ca));
    printf("%#x\n", &(ca[0]));
    printf("%#x\n", &(ca[1]));
    printf("%#x\n\n", ++ca);
}

void my_poiner_func(char *pa) {
    printf("%#x, %d\n", &pa, sizeof(pa));
    printf("%#x\n", &(pa[0]));
    printf("%#x\n", &(pa[1]));
    printf("%#x\n\n", ++pa);
}

int main(void) {
    printf("%#x\n", &ga);
    printf("%#x\n", &(ga[0]));
    printf("%#x\n\n", &(ga[1]));
    my_array_func(ga);
    my_poiner_func(ga);
    return 0;
}

运行结果:

0x804987c
0x804987c
0x804987d

0xff95b040, 4
0x804987c
0x804987d
0x804987d

0xff95b040, 4
0x804987c
0x804987d
0x804987d

参考Passing an array as an argument to a function in C

程序2:

#include <stdio.h>

int main(void) {
    // your code goes here
    int array[5];
    printf("%p, %d\n", array, sizeof(array));
    printf("%p, %d\n", &array[0], sizeof(&array[0]));
    printf("%p, %d\n", &array, sizeof(&array));
    printf("%p\n", array + 1);
    printf("%p\n", &array[0] + 1);
    printf("%p\n", &array + 1);
    return 0;
}

运行结果:

0xffd062ec, 20
0xffd062ec, 4
0xffd062ec, 4
0xffd062f0
0xffd062f0
0xffd06300

参考How come an array’s address is equal to its value in C?

Go语言的http.FileServer

看一下下面这段代码:

package main

import (
    "net/http"
)

func main() {
    // To serve a directory on disk (/tmp) under an alternate URL
    // path (/tmpfiles/), use StripPrefix to modify the request
    // URL's path before the FileServer sees it:
    http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))
}

(1)http.Dir("/tmp")是利用本地tmp目录实现一个文件系统;
(2)http.FileServer(http.Dir("/tmp"))返回一个Handler,其用来处理访问本地"/tmp"文件夹的HTTP请求;
(3)http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp")))返回一个新的Handler,这个Handler用来处理HTTP请求(移除前缀是"/tmpfiles/"后的URL)。
总结一下,当要访问http://localhost/tmpfiles/a文件,实际访问的是本地/tmp/a文件。

Scala笔记(5)——apply method

本文选自Deeper Look at the Apply Method in Scala

In Scala, there is a language feature generally referred to as “The Apply Method” that has the following rules:

Any object that has an apply method can be called with the .apply omitted.
Functions are no more than objects.

Let’s take a look at an example. Given the following abbreviated definition of class Array, and an instance a,

class Array{
def apply(index:Int) = { …some code to get from the array… }
}

val a = new Array(whatever)

Then the following calls are essentially equivalent:

a.apply(7)
a(7)

什么是“Pure Function”

下列文字摘自The Definition of “Pure Function”

A pure function is a function that depends only on its declared inputs and its internal algorithm to produce its output. It does not read any other values from “the outside world” — the world outside of the function’s scope — and it does not modify any values in the outside world.

1-Pure-Function-Equation