由进程名字获取进程 ID
来源:互联网 发布:centos 挂载u盘 ntfs 编辑:程序博客网 时间:2024/05/18 19:21
方法一:
pid_t getProcessPidbyName(char *name){ FILE *fptr; char *buf = new char[255]; char cmd[255] = {'\0'}; pid_t pid = -1; sprintf(cmd,"pidof %s",name); if((fptr = popen(cmd,"r")) != NULL) { if(fgets(buf,255,fptr) != NULL) { pid = atoi(buf); printf("pid = %d\n",pid); } } pclose(fptr); delete buf; return pid;}/*********************************************************** 来自:http://blog.csdn.net/bulreed/article/details/6682811************************************************************/
方法二:
int main(){ FILE *fp_handle; DIR *process_dir; struct dirent *ptr; SVPBool process_flag = SVP_FALSE; SVPChar buf[256] = {'\0'}; SVPChar filepath[128] = {'\0'}; SVPChar cur_task_name[128] = {'\0'}; process_dir = opendir("/proc"); if (SVP_NULL != process_dir) { while((ptr = readdir(process_dir)) != SVP_NULL) //循环读取路径下的每一个文件/文件夹 { //如果读取到的是"."或者".."则跳过,读取到的不是文件夹名字也跳过 if(DT_DIR != ptr->d_type) continue; if ((strcmp(ptr->d_name, ".") == 0) || (strcmp(ptr->d_name, "..") == 0)) continue; if(atoi(ptr->d_name) < 1130) continue; sprintf(filepath, "/proc/%s/status", ptr->d_name);//生成要读取的文件的路径 fp_handle = fopen(filepath, "r");//打开文件 if (NULL != fp_handle) { if( fgets(buf, 255, fp_handle)== SVP_NULL ){ //如果文件夹为空就跳过 fclose(fp_handle); continue; } sscanf(buf, "%*s %s", cur_task_name); //如果文件内容满足要求则打印路径的名字(即进程的PID) if (strcmp(SVP_RCHMI_PROCESS_NAME, cur_task_name) == 0) { process_flag = SVP_TRUE; printf("PID: %s\n", ptr->d_name); fclose(fp_handle); break; } } } } closedir(process_dir);//关闭路径 return process_flag;}
阅读全文
0 0
- 由进程名字获取进程 ID
- 进程1:由进程ID获取进程名字
- linux根据进程名字获取进程ID
- 根据进程ID获取进程名字
- 由进程名字获取窗口句柄
- 根据进程ID获取经常名字 和 根据进程名字获取进程ID
- 根绝exe名字获取进程ID
- 由进程名获取进程ID及其主线程ID
- VC由进程ID获取窗口句柄
- 由进程名获取进程ID的方法及实现
- 怎样由进程id获取X11窗口id
- Windows Minifilter驱动 - 获取进程ID, 进程名字和线程ID (5)
- android 获取进程名字
- 进程的id获得进程的名字
- 通过进程ID或名字结束进程
- VC由进程ID获取窗口句柄的各种方
- 由进程ID 查看进程状态
- 获取iOS设备上所有的进程名字,以及进程的ID,
- HDU
- python 数据分析与挖局书籍
- HTTP Content-Type
- CodeChef:Present for Andrii(拓扑排序)
- 自己总结的oracle基本操作(适合新手)
- 由进程名字获取进程 ID
- log4j 配置详解
- 轻松搞定对容器实例日志设置定期清理和回卷
- MySql+Mybatis+Druid:sql injection violation, multi-statement not allow
- 微信公众号开发环境部署
- eclipse使用的问题
- arm-linux-gcc交叉编译环境搭建
- Using Events in Highly Distributed Architectures(待翻译)
- Git-远程数据库