Useful command
df:查看磁盘使用情况
dd:convert and copy a file
top:查看CPU占用
du:estimate file space usage
tail -f:查看文件结尾信息
/etc/hosts 主机名到 IP 地址的映射关系的文件
/etc/resolv.conf DNS 服务的配置文件
/etc/gateways 建立动态路由需要用到的文件
clone是fork的升级版本,不仅可以创建进程或者线程,还可以指定创建新的命名空间(namespace)、有选择的继承父进程的内存、甚至可以将创建出来的进程变成父进程的兄弟进程等等.clone()是可以将父进程资源有选择地复制给子进程,而没有复制的数据结构则通过指针的复制让子进程共享,具体要复制哪些资源给子进程,由参数列表中的clone_flags来决定。另外,clone()返回的是子进程的pid
select,pselect,FD_CLR,FD_ISSET,FD_SET,FD_ZERO - synchronous I/O
multiplexing
free查看内存整体的使用情况
cat /proc/meminfo查看内存更详细的情况
last:原来登陆的用户清单
可以查看当前系统的启动时间:w, top, uptime
cat /proc/meminfo:查看物理内存大小
ifconfig:配置本地回环地址,配置网卡的IP地址,激活网络适配器
硬链接删了能访问,软连接删了没了,能跨文件系统。
lprm 命令用于将一个工作由打印机贮列中移除
lpq 命令用于查看一个打印队列的状态,该程序可以查看打印机队列状态及其所包含的打印任务。
lpd 命令 是一个常驻的打印机管理程序,它会根据 /etc/printcap 的内容来管理本地或远端的打印机。
lpr(line printer,按行打印)实用程序用来将一个或多个文件放入打印队列等待打印。
将网卡激活不需要重新编译内核
netd表示Network守护进程. Netd负责跟一些涉及网络的配置,操作,管理,查询等相关的功能实现,比如,例如带宽控制(Bandwidth),流量统计,带宽控制,网络地址转换(NAT),个人局域网(pan),PPP链接,soft-ap,共享上网(Tether),配置路由表,interface配置管理.
vsftpd服务流量控制的参数:local_max_rate; anon_max_rate
init启动进程需要读取()配置文件:/etc/inittab
whereis 可查询二进制文件(-b)、帮助文档(-m)、源程序(-s),无选项时,返回所有结果,-u(除上述三种的其它文件)
which 查看可执行文件的位置
whatis 查询命令有什么功能
apropos 搜索指定关键字的命令
cat /proc/interrupts查看中断
jobs 查看后台进程作业
fseek是库函数,lseek是系统调用
cp source des/filename:can rename the copied file
nohup 在注销后使用 nohup 命令运行后台中的程序
Inode可以被多个文件使用
Unix/Linux系统允许,多个文件名指向同一个inode号码。这意味着,可以用不同的文件名访问同样的内容;对文件内容进行修改,会影响到所有文件名;但是,删除一个文件名,不影响另一个文件名的访问。这种情况就被称为”硬链接”(hard link)。 除了硬链接以外,还有一种特殊情况。文件A和文件B的inode号码虽然不一样,但是文件A的内容是文件B的路径。读取文件A时,系统会自动将访问者导向文件B。因此,无论打开哪一个文件,最终读取的都是文件B。这时,文件A就称为文件B的”软链接”(soft link)或者”符号链接(symbolic link)。
所有加载的文件系统在/etc/mtab中得到反映。
目录在文件系统中也是以文件形式存在
Spinlock
spinlock在多处理器多线程环境的场景中有很广泛的使用,一般要求使用spinlock的临界区尽量简短,这样获取的锁可以尽快释放,以满足其他忙等的线程。Spinlock和mutex不同,spinlock不会导致线程的状态切换(用户态->内核态),但是spinlock使用不当(如临界区执行时间过长)会导致cpu busy飙高。
CPU负载
数字越小越好,可以通过TOP命令查看,可以通过uptime查看
Network
Linux系统中,已经将DHCP服务器架设好,客户端的网络接口eth0,可以通过( )命令获得服务器分配的IP地址:dhclient,ifdown eth0;ifup eth0
TCP、UDP可以绑定同一端口进行通信,取决于数据包的协议类型决定使用哪种协议。
网络write返回n>0表示已经发送nbyte
硬链接仅仅删除文件名,inode没删,不能链接目录;
查看本机路由情况:route/netstat
route命令用来显示目前本机路由表的内容,并且还可以针对路由表中的记录来进行相应的添加、删除或修改等操作。
ifconfig命令用来来检测和设置本机的网络接口。
ping命令用来检测两部主机之间的传输信道是否畅通,或远程主机是否正常
netstat命令用来查看主机的连接状态
DHCP服务器能提供的服务包括:提供DNS、网关信息;为特定客户机提供固定IP地址;为主机提供动态的IP地址
Select/Epoll
linux的I/O复用接口select和epoll.
select 和 epoll效率差异的原因:select采用轮询方式处理连接,epoll是触发式处理连接。
Select:
1.Socket数量限制:该限制可操作的Socket数由FD_SETSIZE决定,内核默认32*32=1024.
2.操作限制:通过遍历FD_SETSIZE(1024)个Socket来完成调度,不管哪个Socket是活跃的,都遍历一遍。
poll使用pollfd结构来存储fd,突破了select中描述符数目的限制。
与select的后两点类似,poll仍然需要将pollfd数组拷贝到内核空间,之后依次扫描fd的状态,整体复杂度依然是O(n)的,在并发量大的情况下服务器性能会快速下降。
Epoll
1.Socket数量无限制:该模式下的Socket对应的fd列表由一个数组来保存,大小不限制(默认4k)。
2.操作无限制:基于内核提供的反射模式,有活跃Socket时,内核访问该Socket的callback,不需要遍历轮询。但当所有的Socket都活跃的时候,所有的callback都被唤醒,会导致资源的竞争。既然都是要处理所有的Socket,那么遍历是最简单最有效的实现方式。
服务器的特点是经常维护着大量连接,但其中某一时刻读写的操作符数量却不多。epoll先通过epoll_ctl注册一个描述符到内核中,并一直维护着而不像poll每次操作都将所有要监控的描述符传递给内核;在描述符读写就绪时,通过回掉函数将自己加入就绪队列中,之后epoll_wait返回该就绪队列。也就是说,epoll基本不做无用的操作,时间复杂度仅与活跃的客户端数有关,而不会随着描述符数目的增加而下降。
系统调用
1.Linux执行ls,会引起哪些系统调用:read,fork,execv
nagle算法
2.在linux编程中,以下哪个TCP的套接字选项与nagle算法的开启和关闭有关?
TCP_NODELAY
3.Nagle算法的规则:
(1)如果包长度达到MSS,则允许发送;
(2)如果该包含有FIN,则允许发送;
(3)设置了TCP_NODELAY选项,则允许发送;
(4)未设置TCP_CORK选项时,若所有发出去的小数据包(包长度小于MSS)均被确认,则允许发送;
(5)上述条件都未满足,但发生了超时(一般为200ms),则立即发送。
Nagle算法只允许一个未被ACK的包存在于网络,它并不管包的大小,因此它事实上就是一个扩展的停-等协议,只不过它是基于包停-等的,而不是基于字节停-等的。Nagle算法完全由TCP协议的ACK机制决定,这会带来一些问题,比如如果对端ACK回复很快的话,Nagle事实上不会拼接太多的数据包,虽然避免了网络拥塞,网络总体的利用率依然很低。
Nagle算法是silly window syndrome(SWS)预防算法的一个半集。SWS算法预防发送少量的数据,Nagle算法是其在发送方的实现,而接收方要做的是不要通告缓冲空间的很小增长,不通知小窗口,除非缓冲区空间有显著的增长。这里显著的增长定义为完全大小的段(MSS)或增长到大于最大窗口的一半。
注意:BSD的实现是允许在空闲链接上发送大的写操作剩下的最后的小段,也就是说,当超过1个MSS数据发送时,内核先依次发送完n个MSS的数据包,然后再发送尾部的小数据包,其间不再延时等待。(假设网络不阻塞且接收窗口足够大)
举个例子,比如之前的blog中的实验,一开始client端调用socket的write操作将一个int型数据(称为A块)写入到网络中,由于此时连接是空闲的(也就是说还没有未被确认的小段),因此这个int型数据会被马上发送到server端,接着,client端又调用write操作写入‘\r\n’(简称B块),这个时候,A块的ACK没有返回,所以可以认为已经存在了一个未被确认的小段,所以B块没有立即被发送,一直等待A块的ACK收到(大概40ms之后),B块才被发送。整个过程如图所示:
这里还隐藏了一个问题,就是A块数据的ACK为什么40ms之后才收到?这是因为TCP/IP中不仅仅有nagle算法,还有一个TCP确认延迟机制 。当Server端收到数据之后,它并不会马上向client端发送ACK,而是会将ACK的发送延迟一段时间(假设为t),它希望在t时间内server端会向client端发送应答数据,这样ACK就能够和应答数据一起发送,就像是应答数据捎带着ACK过去。在我之前的时间中,t大概就是40ms。这就解释了为什么’\r\n’(B块)总是在A块之后40ms才发出。
当然,TCP确认延迟40ms并不是一直不变的,TCP连接的延迟确认时间一般初始化为最小值40ms,随后根据连接的重传超时时间(RTO)、上次收到数据包与本次接收数据包的时间间隔等参数进行不断调整。另外可以通过设置TCP_QUICKACK选项来取消确认延迟。
进程类型
交互进程——由一个shell启动的进程。交互进程既可以在前台运行,也可以在后台运行。
批处理进程——这种进程和终端没有联系,是一个进程序列。
监控进程(也称守护进程)——Linux系统启动时启动的进程,并在后台运行。