工具 - webdancer's Blog
linux 必学命令( 转)
Linux提供了大量的命令,利用它可以有效地完成大量的工作,如磁盘操作、文件存取、目录操作、进程管理、文件权限设定等。所以,在Linux系统上工作离不开使用系统提供的命令。要想真正理解Linux系统,就必须从Linux命令学起,通过基础的命令学习可以进一步理解Linux系统。
不同Linux发行版的命令数量不一样,但Linux发行版本最少的命令也有200多个。这里笔者把比较重要和使用频率最多的命令,按照它们在系统中的作用分成下面六个部分一一介绍。
◆ 安装和登录命令:login、shutdown、halt、reboot、install、mount、umount、chsh、exit、last;
◆ 文件处理命令:file、mkdir、grep、dd、find、mv、ls、diff、cat、ln;
◆ 系统管理相关命令:df、top、free、quota、at、lp、adduser、groupadd、kill、crontab;
◆ 网络操作命令:ifconfig、ip、ping、netstat、telnet、ftp、route、rlogin、rcp、finger、mail、 nslookup;
◆ 系统安全相关命令:passwd、su、umask、chgrp、chmod、chown、chattr、sudo ps、who;
◆ 其它命令:tar、unzip、gunzip、unarj、mtools、man、unendcode、uudecode。
os实验1 ——进程控制
#include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<sys/wait.h> #include<signal.h> #include<stdlib.h> typedef void (*sighandler_t) (int); void sincon() { } int main() { pid_t p0; int sta0,sta1; signal(SIGINT,(sighandler_t)sincon); while(1) { if((p0=fork())<0) { perror("error!fork first child"); exit(0); } else if(p0==0) { pause(); sta0=execlp("/bin/ls","ls",NULL); exit(0); } else { pid_t p1; if((p1=fork())<0) { perror("error!fork second child"); exit(0); } else if(p1==0) { printf("exe the second child\n"); sta1=execlp("/bin/ps","ps",NULL); exit(0); } else { if(waitpid(p1,&sta1,0)!=p1) perror("error!"); if((kill(p0,SIGINT))>=0) printf("wake up first child\n"); sleep(3); } } } return 0; }
fork函数二三事
1.fork函数的基本功能
在linux中,只有一个函数可以创建子进程:fork。
#include <sys/types.h> #include <unistd.h> pid_t fork(void);
由fork创建的新进程被称为子进程( child process)。该函数被调用一次,但返回两次。两次返回的区别是子进程的返回值是0,而父进程的返回值则是新子进程的进程I D。将子进程I D返回给父进程的理由是:因为一个进程的子进程可以多于一个,所以没有一个函数使一个进程可以获得其所有子进程的进程I D。fork使子进程得到返回值0的理由是:一个进程只会有一个父进程,所以子进程总是可以调用getppid以获得其父进程的进程ID(进程ID 0总是由交换进程使用,所以一个子进程的进程ID不可能为0 )。
2.CoW(Copy on Write)
CoW是现代操作系统中的一个很重要的策略,在fork中也使用了。看一下下面这个例子。
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/types.h> int main(){ int i; pid_t p0; if((p0=fork())<0){ perror("error!"); exit(0); } else if(p0==0){ i=getpid(); printf("i is %d the i address is %p in the child\n",i,&i); } else { i=getpid(); printf("i is %d the i address is %p in the parent\n",i,&i); } return 0; }
运行的结果:
i is 3132 the i address is 0xbf8147e8 in the parent
i is 3133 the i address is 0xbf8147e8 in the child
3.缓冲问题
在这里http://coolshell.cn/articles/7965.html看到了一个很有趣的问题,代码如下:
#include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(void) { int i; for(i=0; i<2; i++){ fork(); printf("-"); } return 0; }
问一下:上面到底会输出几个‘-'。答案不是六个,而是八个。原因就是因为父进程的buffer也被子进程继承了。详细的分析可以参照Coolshell的这篇文章http://coolshell.cn/articles/7965.html。