linux c语言查找指定程序pid
来源:互联网 发布:知乎入门级古典音乐 编辑:程序博客网 时间:2024/06/11 05:31
工作上需要向特定进程发送USER2信号,查询进程时找到一个已经封装好的接口,做个备忘。
做为其它程序接口是在程序中加入#include "pidof.h"
,关联是加上pidof.c
就可以了。
如果做为单独程序来使用,将.c中的#include "pidof.h"
注掉就能编了。
pidof.h
#ifndef _PIDOF_H_#define _PIDOF_H_int lookup_pid(char *name, pid_t *pids, int n); #endif
pidof.c
#include <stdio.h> #include <string.h> #include <sys/types.h> #include "pidof.h"#define PS_COMMAND "ps -ax" #define SCAN(buf) (sscanf(buf,"%d %s %s %s %s",pid,s[0],s[1],s[2],cmd) == 5) /* Returns number of pid matching process name, or -1 on error */ int lookup_pid(char *name, pid_t *pids, int n) { FILE *fp; char buf[256], cmd[200], s[3][32]; pid_t *pid; if ((fp = popen(PS_COMMAND, "r")) == NULL) return -1; pid = pids; while (n > 0 && fgets(buf, sizeof(buf), fp)) if (SCAN(buf) && strstr(cmd, name)) /* improve on this simple name check */ pid++, n--; fclose(fp); return pid - pids; } #if #ifndef _PIDOF_H_int main(int argc, char **argv) { char *process; pid_t pids[8]; int n, i; if (argc < 2) { printf("Usage: %s process_name ", argv[0]); return 1; } process = argv[1]; if ((n = lookup_pid(process, pids, 8)) == -1) { printf("Error reading pid of %s ", process); return 2; } printf("%d %s ", n, process); for (i = 0; i < n; i++) printf("%d ", pids[ i]); return 0; } #endif
阅读全文
0 0
- linux c语言查找指定程序pid
- Linux查找某一进程的PID(c语言)
- Unix下c调用shell打印指定程序的pid
- Linux C语言查找当前程序运行目录
- linux下根据进程号PID查找程序路径
- linux根据pid获取进程名和获取进程pid(c语言获取pid)
- linux根据pid获取进程名和获取进程pid(c语言获取pid)
- 51单片机PID的算法实现程序C语言
- 温度控制的PID算法的C语言程序
- PID C语言实现
- c语言程序之二分法查找
- C语言:折半查找的程序编写
- C语言基础程序 查找篇
- Linux下用C语言检查指定程序名的运行实例个数
- 关闭指定pid程序, utils.sh
- C语言字典查找程序,最高效查找
- C语言实现PID算法
- PID算法 C语言实现
- angular--$q.all()
- LeetCode 572. Subtree of Another Tree
- Vue.js——60分钟快速入门
- 字符串置换
- Android获取屏幕分辨率
- linux c语言查找指定程序pid
- 二叉查找树的实现
- dpkg: 处理归档 /var/cache/apt/archives/软件名 (--unpack)时出错
- UIAlertController的使用
- Android单击通知栏后返回正在运行的程序并跳转到指定fragment页面
- JS(十一)事件类型
- JavaScript对象【基础】
- form表单的两种提交方式,submit和button的用法
- Android OpenGL显示任意3D模型文件