Android C++ 系列:Linux 常用函数和工具

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

image.png


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命令。


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


3.用下面的命令使网络设置生效:


sudo /etc/init.d/networking restart


4.也可以在命令行下直接输入下面的命令来获取地址: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


3.将上面的ip地址等信息换成你自己就可以了.用下面的命令使网络设置生效:sudo /etc/init.d/networking restart

4.设置DNS:要访问DNS 服务器来进行查询,需要设置/etc/resolv.conf文件, 假设DNS服务器的IP地址是192.168.2.2, 那么/etc/resolv.conf文件的内容应为:nameserver 192.168.2.2

5.手动重启网络服务:sudo /etc/init.d/networking restart


3. 总结


本文介绍了Linux常用命令工具及函数:文件访问时间函数、cpu使用时间函数、ifconfig、netstat、设置IP方式等。

目录
相关文章
|
8天前
|
Ubuntu 安全 Linux
Linux(34)Rockchip RK3568 Ubuntu22.04和Debian 10上配置远程桌面工具
Linux(34)Rockchip RK3568 Ubuntu22.04和Debian 10上配置远程桌面工具
40 0
|
2天前
|
Linux 网络安全 数据处理
【专栏】Linux 超级强大的十六进制 dump 工具:XXD 命令,我教你应该如何使用!
【4月更文挑战第28天】Linux下的xxd命令是一个强大的二进制数据处理工具,用于十六进制转储和数据分析。它可以显示文件的十六进制和ASCII表示,方便查看内容、分析数据结构和比较文件。xxd支持指定输出格式、写入文件、数据提取和转换等功能。在网络安全分析、程序调试和数据恢复等领域有广泛应用。通过掌握xxd,用户能更深入理解和处理二进制数据。
|
2天前
|
安全 Linux 网络安全
【专栏】Linux 网络扫描工具:nmap,涨知识的时间到了!
【4月更文挑战第28天】nmap, 开源网络扫描工具,用于探测主机、网络信息,包括开放端口、服务类型、OS等。本文分三部分介绍:1) nmap简介与基本原理;2) 使用方法和高级技巧,如脚本扩展;3) 实际应用,如网络安全评估、系统管理和渗透测试。学习nmap需注意合规性,持续探索新技巧,以提升网络管理与安全能力。一起开始nmap的探索之旅吧!
|
2天前
|
存储 Linux C++
【进厂修炼 - First week】Linux & C++
【进厂修炼 - First week】Linux & C++
|
6天前
|
JSON Unix Linux
Linux系统之jq工具的基本使用
Linux系统之jq工具的基本使用
33 2
|
7天前
|
监控 安全 Linux
Linux系统之安装ServerBee服务器监控工具
【4月更文挑战第22天】Linux系统之安装ServerBee服务器监控工具
49 2
|
7天前
|
编解码 Linux 数据安全/隐私保护
linux工具之curl与wget高级使用
linux工具之curl与wget高级使用
|
7天前
|
Linux C++
【代码片段】Linux C++打印当前函数调用堆栈
【代码片段】Linux C++打印当前函数调用堆栈
10 0
|
Linux
Linux系统调用二、open()函数与close()函数介绍
Linux系统调用二、open()函数与close()函数介绍
290 0
Linux系统调用二、open()函数与close()函数介绍
|
Linux C++ Unix