#yyds干货盘点#Android C++系列(Linux常用函数和工具)

亦余心之所善兮,虽九死其犹未悔。这篇文章主要讲述#yyds干货盘点#Android C++系列:Linux常用函数和工具相关的知识,希望能为你提供帮助。
?
1. 时间函数1.1 文件访问时间

#include < sys/types.h>
#include < utime.h>
int utime (const char *name, const struct utimebuf *t);
返回:若成功则为 0,若出错则为- 1

如果times是一个空指针,则存取时间和修改时间两者都设置为当前时间;
如果times是非空指针,则存取时间和修改时间被设置为 times所指向的结构中的值。此 时,进程的有效用户ID必须等于该文件的所有者 ID,或者进程必须是一个超级用户进程。对 文件只具有写许可权是不够的
此函数所使用的结构是:

struct utimbuf
time_t actime; /*access time*/
time_t modtime; /*modification time*/

1.2 cpu使用时间
#include < sys/time.h>
#include < sys/resource.h>
int getrusage(int who, struct rusage *usage);

  • RUSAGE_SELF:Return resource usage statistics for the calling process, which is the sum of resources used by all threads in the process.
  • RUSAGE_CHILDREN:Return resource usage statistics for all children of the calling process that have terminated and been waited for. These statis‐ tics will include the resources used by grandchildren, and fur‐ ther removed descendants, if all of the intervening descendants waited on their terminated children.
  • RUSAGE_THREAD (since Linux 2.6.26):Return resource usage statistics for the calling thread.
2. 网络工具2.1 ifconfig
sudo ifconfig eth0 down/up
sudo ifconfig eth0 192.168.102.123

2.2 netstat
  • a (all)显示所有选项,默认不显示LISTEN相关
  • t (tcp)仅显示tcp相关选项
  • u (udp)仅显示udp相关选项
  • n 拒绝显示别名,能显示数字的全部转化成数字。 -l 仅列出有在 Listen (监听) 的服務状态
  • p 显示建立相关链接的程序名 -r 显示路由信息,路由表
  • e 显示扩展信息,例如uid等 -s 按各个协议进行统计
  • c 每隔一个固定时间,执行该netstat命令。
【#yyds干货盘点#Android C++系列(Linux常用函数和工具)】LISTEN和LISTENING的状态只有用-a或者-l才能看到:
sudo netstat -anp | grep ftp

2.3 设置IP
以DHCP方式配置网卡:
  1. 编辑文件/etc/network/interfaces:??sudo vi /etc/network/interfaces??
  2. 并用下面的行来替换有关eth0的行:
# The primary network interface - use DHCP to find our address
auto eth0
iface eth0 inet dhcp

  1. 用下面的命令使网络设置生效:
sudo /etc/init.d/networking restart

  1. 也可以在命令行下直接输入下面的命令来获取地址:??sudo dhclient eth0??
为网卡配置静态IP地址:
  1. 编辑文件/etc/network/interfaces:??sudo vi /etc/network/interfaces??
  2. 用下面的行来替换有关eth0的行:
# The primary network interface auto eth0
iface eth0 inet static
address 192.168.2.1
gateway 192.168.2.254 netmask 255.255.255.0
#network 192.168.2.0
#broadcast 192.168.2.255

  1. 将上面的ip地址等信息换成你自己就可以了.用下面的命令使网络设置生效:??sudo /etc/init.d/networking restart??
  2. 设置DNS:要访问DNS 服务器来进行查询,需要设置/etc/resolv.conf文件, 假设DNS服务器的IP地址是192.168.2.2, 那么/etc/resolv.conf文件的内容应为:??nameserver 192.168.2.2??
  3. 手动重启网络服务:??sudo /etc/init.d/networking restart??
3. 总结本文介绍了Linux常用命令工具及函数:文件访问时间函数、cpu使用时间函数、ifconfig、netstat、设置IP方式等。

    推荐阅读