linux下ss命令源码学习(二):linux c代码获取后台进程名称及pid
来源:互联网 发布:打谱软件手机版 编辑:程序博客网 时间:2024/06/06 03:24
该代码来源于ss命令源码
#include <stdio.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <syslog.h>#include <fcntl.h>#include <sys/ioctl.h>#include <string.h>#include <arpa/inet.h>#include <resolv.h>#include <dirent.h>#include <fnmatch.h>#include <dirent.h>void get_proc_pid(void) { const char *root = "/proc/"; struct dirent *d; char name[1024] = {0}; int nameoff = 0; DIR *dir; snprintf(name, sizeof (name), "%s", root); if (strlen(name) == 0 || name[strlen(name) - 1] != '/') { snprintf(name + strlen(name), sizeof (name) - strlen(name), "%s", "/"); } nameoff = strlen(name); dir = opendir(name); if (!dir) { return; } while ((d = readdir(dir)) != NULL) { char process[16] = {0}; int pid = 0, pos = 0; DIR *dir1; char crap; if (sscanf(d->d_name, "%d%c", &pid, &crap) != 1) { continue; } snprintf(name + nameoff, sizeof (name) - nameoff, "%d/fd/", pid); pos = strlen(name); if ((dir1 = opendir(name)) == NULL) { continue; } process[0] = '\0'; char tmp[1024] = {0}; FILE *fp = NULL; snprintf(tmp, sizeof (tmp), "%s/%d/stat", root, pid); if ((fp = fopen(tmp, "r")) != NULL) { fscanf(fp, "%*d (%[^)])", process); fclose(fp); } printf("--%s--%d\n",process, pid); closedir(dir1); } closedir(dir);}int main(int argc, char *argv[]){ get_proc_pid(); return 0;}
1 0
- linux下ss命令源码学习(二):linux c代码获取后台进程名称及pid
- linux下ss命令源码学习(一):进程名、pid的获取及存储
- linux 脚本获取进程PID及路径
- linux命令(26):Bash Shell 获取进程 PID
- Linux获取进程pid
- linux根据pid获取进程名和获取进程pid(c语言获取pid)
- linux根据pid获取进程名和获取进程pid(c语言获取pid)
- linux C 获取pid终极父进程的PID
- linux 下根据进程名字获取进程的进程号PID,类似pidof,C函数
- Linux下获取进程PID并杀掉进程
- linux下获取指定exe进程pid和详细信息的命令
- linux下SS命令使用
- Linux 精准获取进程pid
- linux学习笔记--ss命令
- Linux命令 -- 获取 进程信息:name 、stat 、PID
- linux获取精准进程PID之pgrep命令
- Linux下获取命令源码
- linux 获取pid的命令
- idea报错:[2016-08-31 09:20:10,763] Artifact xxx:war exploded: Error during artifact deployment.
- Django 使用多个视图前缀
- 不同磁道的扇区数是否相同? 知乎
- 屏幕适配
- Add Two Numbers II
- linux下ss命令源码学习(二):linux c代码获取后台进程名称及pid
- github使用ssh认证,无需输入用户名和密码的方法
- 计算字符个数及sizeof 与 strlen 的区别
- 常用密码表
- 第十周项目3---二叉树遍历的应用
- Android 沉浸式体验
- 字符串复制函数 strcyp 复制的原型
- 【置顶】编程中暴露出来的问题,与诸位共勉
- 数学建模十大经典算法漫谈