Chinaunix首页 | 论坛 | 博客
  • 博客访问: 444519
  • 博文数量: 293
  • 博客积分: 4204
  • 博客等级: 上校
  • 技术积分: 3060
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-21 10:27
个人简介

nobody

文章分类

全部博文(293)

文章存档

2014年(27)

2013年(105)

2012年(41)

2011年(109)

2010年(11)

分类: LINUX

2011-02-24 22:49:43

之前对Linux Distr系统中的一些程序的功能感觉很强大,一直不知道是怎么实现的。现在知道了其中的一些是如何实现的,做个记录。

1 命令  ps    -- displays information about a selection of the active processes. 
之前对ps这个命令是如何实现的一直不得其解:因为知道kernel没有提供实现该function的system call.
在看busybox中的ps实现时才知道文件/proc/xxx/stat (其中的xxx为阿拉伯数字,代表了进程号)中包含了
相关的信息。
2  
命令 fdisk  -- partition table manipulator for Linux.
命令 lsmod - program to show the status of modules in the Linux Kernel

fdisk  -l  命令实现方式和ps 命令的实现方式是相同的:读/proc/partitions文件。
 lsmod is a trivial program which nicely formats the contents of the /proc/modules, showing what kernel modules are currently loaded.

命令 uptime --  Tell how long the system has been running.
命令 free -- Display amount of free and used memory in the system
实现原理很普通了:kernel提供了相关的系统调用,c库对该systemcall进行了封装----
sysinfo() - returns information on overall system statistics.
另外,cat /proc/uptime  ; cat /proc/meminfo 可以实现类似的效果。

4 命令 stat --  display file or file system status.
该命令的实现原理同uptime的实现相同.

5 命令 ls -- list directory contents.
kerenl 提供system call ,C API 有:opendir()/readdir()/closedir()/stat()/lstat()

6 命令 swapon/swapoff -- enable/disable devices and files for paging and swapping.
kernel 提供system call,C API 有:swapon()/swapoff()

7 命令 dd -- convert and copy a file
从一个"文件"读取数据再把数据写入另外一个“文件”。
使用system call : stat()/open()/read()/write()/close().

8 命令 uname -  print system information
kernel 提供system call,C API uname();

9 命令 kill - sends the specified signal to the specified process or process group.
使用system call: C API kill()
这里需要注意的是;API kill() 实现的是 send signal to a process, 和"杀"没有一对一的关系,也就是说kill命令可以用于terminate 某个/些process,但它还可以send other signals。
这里使用signal()替代kill()或许更“名副其实”,但因为历史原因,signal()已经被用于sets the disposition of the signal signum to handler。

10 
命令pwd---print name of current/working directory
该命令调用了C API getcwd(),而getcwd()则是对system call sys_getcwd()的封装。
return a null-terminated string containing an absolute pathname that is the current working directory of the calling process
写道这里,想到了“cd”.只不过,注意:这个cd 并不是busybox中的ap 而只是shell内置的command,其用于Change the current directory.cd 调用的则是C API chdir(),chdir则是对sys_chdir()的封装。
getcwd()/chdir() 操作的是本进程task_struct-->fs_struct-->pwd.

11 命令xargs -- build and execute command lines from standard input.
第一次用这个命令的时候感觉命令行太强大了。命令是对fork()/exec()系列/wait()系列/exit() C API的封装。
而这些C API 则分别是对system call  sys_fork()/sys_execve()/sys_waitpid()/sys_exit()的封装。
说到这里,需要提及C API  popen()/system().
对于system() -- execute a shell command,对于使用C 编码的人而言,这个接口真是太给力了。使用这个接口可以执行大多数的non-interactive程序。其缺点就是:不能对命令的输出进行反馈。这时我们就可以用popen()了。
 popen, pclose - pipe stream to or from a process.
和xargs 的实现类似,system()/popen()也可以是对fork()/exec()系列/wait()系列/exit() C API的封装.
更多关于system()/popen()的讨论可以参阅W.Richard Stevens/Stephen A. Rago 的APUE e2.


12 命令dmesg  --  dmesg - print or control the kernel ring buffer.
该命令调用了C API klogctl(),而该API 调用了sys_sysctl()。
使用cat /var/log/mesage也可以实现查看kernel 的ring buffer.

13
 命令 dirname - strip last component from file name
 命令 basename - strip directory and suffix from filenames
这个两个命令主要的工作就是进行字符串的操作,在完成字符串的操作之后stdio 中的输出函数把结果输出
到stdout。

总结:
从以上可以看到:kernel向app层提供service至少包括system call 和 挂在虚拟文件系统 方式(/proc 、/sys)。从function 的角度看,有时它们有重合(二种方式都可以实现某个function)。
另外,虽然都属于C API ,但一些API 可能是另外一些API的封装;一些API 直接对system call进行封装 ,而没有调用其他的API ;还有一些则根本没有调用system call (比如strxxx(),memxxx(),这里不展开讨论)。但从使用的角度看,几乎可以忽略它们的区别。
阅读(2202) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~