首页 Linux下C_C 查找某一进程

Linux下C_C 查找某一进程

举报
开通vip

Linux下C_C 查找某一进程Linux下C/C++查找某一进程 一、前言:要在程序中启动某一程序,如果程序已经存在了,就不再启动。查找了N篇文档,有所收获,总结一下。 二、实现 大体分两种: 1、exec或popen执行ps的命令行,然后运用某几个字符串匹配函数。 #include #include #include #include int main() { FILE *pstr; char cmd[128],buff[512],*p; pid_t pID; int pidnum; char *nam...

Linux下C_C  查找某一进程
Linux下C/C++查找某一进程 一、前言:要在程序中启动某一程序,如果程序已经存在了,就不再启动。查找了N篇文档,有所收获,总结一下。 二、实现 大体分两种: 1、exec或popen执行ps的命令行,然后运用某几个字符串匹配函数。 #include #include #include #include int main() { FILE *pstr; char cmd[128],buff[512],*p; pid_t pID; int pidnum; char *name= "ping ";//要查找的进程名 int ret=3; memset(cmd,0,sizeof(cmd)); sprintf(cmd, "ps -ef|grep %s ",name); pstr=popen(cmd, "r");// if(pstr==NULL) return 1; memset(buff,0,sizeof(buff)); fgets(buff,512,pstr); p=strtok(buff, " "); p=strtok(NULL, " "); //这句是否去掉,取决于当前系统中ps后,进程ID号是否是第一个字段 pclose(pstr); if(p==NULL) return 1; if(strlen(p)==0) return 1; if((pidnum=atoi(p))==0) return 1; printf("pidnum: %d\n",pidnum); pID=(pid_t)pidnum; ret=kill(pID,0);//这里不是要杀死进程,而是验证一下进程是否真的存在,返回0表示真的存在 printf("ret= %d \n",ret); if(0==ret) printf("Process: %s exist!\n",name); else printf("Process: %s not exist!\n",name); return 0; } 2、读取/proc文件 找到个代码如下: 读取/proc文件查找进程 #ifndef __cplusplus//识别C/C++的宏,望文生义 #define _GNU_SOURCE #endif #include #include #include // for opendir(), readdir(), closedir() #include // for stat() #ifdef __cplusplus #include #include #include #include #else #include #include #include #include #endif #define PROC_DIRECTORY "/proc/" #define CASE_SENSITIVE 1 #define CASE_INSENSITIVE 0 #define EXACT_MA TCH 1 #define INEXACT_MATCH 0 //是不是数字 int IsNumeric(const char* ccharptr_CharacterList) { for ( ; *ccharptr_CharacterList; ccharptr_CharacterList++) if (*ccharptr_CharacterList < '0' || *ccharptr_CharacterList > '9') return 0; // false return 1; // true } //intCaseSensitive=0大小写不敏感 int strcmp_Wrapper(const char *s1, const char *s2, int intCaseSensitive) { if (intCaseSensitive) return !strcmp(s1, s2); else return !strcasecmp(s1, s2); } //intCaseSensitive=0大小写不敏感 int strstr_Wrapper(const char* haystack, const char* needle, int intCaseSensitive) { if (intCaseSensitive) return (int) strstr(haystack, needle); else return (int) strcasestr(haystack, needle); } #ifdef __cplusplus pid_t GetPIDbyName(const char* cchrptr_ProcessName, int intCaseSensitiveness, int intExactMatch) #else pid_t GetPIDbyName_implements(const char* cchrptr_ProcessName, int intCaseSensitiveness, int intExactMatch) #endif { char chrarry_CommandLinePath[100] ; char chrarry_NameOfProcess[300] ; char* chrptr_StringToCompare = NULL ; pid_t pid_ProcessIdentifier = (pid_t) -1 ; struct dirent* de_DirEntity = NULL ; DIR* dir_proc = NULL ; int (*CompareFunction) (const char*, const char*, int) ; if (intExactMatch) CompareFunction = &strcmp_Wrapper; else CompareFunction = &strstr_Wrapper; dir_proc = opendir(PROC_DIRECTORY) ; if (dir_proc == NULL) { perror("Couldn't open the " PROC_DIRECTORY " directory") ; return (pid_t) -2 ; } // Loop while not NULL while ( (de_DirEntity = readdir(dir_proc)) ) { if (de_DirEntity->d_type == DT_DIR) { if (IsNumeric(de_DirEntity->d_name)) { strcpy(chrarry_CommandLinePath, PROC_DIRECTORY) ; strcat(chrarry_CommandLinePath, de_DirEntity->d_name) ; strcat(chrarry_CommandLinePath, "/cmdline") ; FILE* fd_CmdLineFile = fopen (chrarry_CommandLinePath, "rt") ; //open the file for reading text if (fd_CmdLineFile) { fscanf(fd_CmdLineFile, "%s", chrarry_NameOfProcess) ; //read from /proc//cmdline fclose(fd_CmdLineFile); //close the file prior to exiting the routine if (strrchr(chrarry_NameOfProcess, '/')) chrptr_StringToCompare = strrchr(chrarry_NameOfProcess, '/') +1 ; else chrptr_StringToCompare = chrarry_NameOfProcess ; //printf("Process name: %s\n", chrarry_NameOfProcess); //这个是全路径,比如/bin/ls //printf("Pure Process name: %s\n", chrptr_StringToCompare ); //这个是纯进程名,比如ls //这里可以比较全路径名,设置为chrarry_NameOfProcess即可 if ( CompareFunction(chrptr_StringToCompare, cchrptr_ProcessName, intCaseSensitiveness) ) { pid_ProcessIdentifier = (pid_t) atoi(de_DirEntity->d_name) ; closedir(dir_proc) ; return pid_ProcessIdentifier ; } } } } } closedir(dir_proc) ; return pid_ProcessIdentifier ; } #ifdef __cplusplus pid_t GetPIDbyName(const char* cchrptr_ProcessName) { return GetPIDbyName(cchrptr_ProcessName, CASE_INSENSITIVE, EXACT_MA TCH) ; } #else // C cannot overload functions - fixed /*这里是 原文 少年中国说原文俱舍论原文大医精诚原文注音大学原文和译文对照归藏易原文 的实现,但貌似需要某些牛X的设置,因不需要,删之 pid_t GetPIDbyName_Wrapper(const char* cchrptr_ProcessName, ... ) { int intTempArgument ; int intInputArguments[2] ; // intInputArguments[0] = 0 ; // intInputArguments[1] = 0 ; memset(intInputArguments, 0, sizeof(intInputArguments) ) ; int intInputIndex ; va_List argptr; va_start( argptr, cchrptr_ProcessName ); for (intInputIndex = 0; (intTempArgument = va_arg( argptr, int )) != 15; ++intInputIndex) { intInputArguments[intInputIndex] = intTempArgument ; } va_end( argptr ); return GetPIDbyName_implements(cchrptr_ProcessName, intInputArguments[0], intInputArguments[1]); } #define GetPIDbyName(ProcessName,...) GetPIDbyName_Wrapper(ProcessName, ##__V A_ARGS__, (int) 15) */ //简单实现 pid_t GetPIDbyName_Wrapper(const char* cchrptr_ProcessName) { return GetPIDbyName_implements(cchrptr_ProcessName, 0,0);//大小写不敏感} #endif int main() { pid_t pid = GetPIDbyName_Wrapper("bash") ; // If -1 = not found, if -2 = proc fs access error printf("PID %d\n", pid); return EXIT_SUCCESS ; } 通过比较全路径,能一定程度上避免第1种方法的问题。 以下是整理后的C语言实现: #include #include #include // for opendir(), readdir(), closedir() #include // for stat() #include #include #include #include #define PROC_DIRECTORY "/proc/" #define CASE_SENSITIVE 1 #define CASE_INSENSITIVE 0 #define EXACT_MA TCH 1 #define INEXACT_MATCH 0 //是不是数字 int IsNumeric(const char* ccharptr_CharacterList) { for ( ; *ccharptr_CharacterList; ccharptr_CharacterList++) if (*ccharptr_CharacterList < '0' || *ccharptr_CharacterList > '9') return 0; // false return 1; // true } //intCaseSensitive=0大小写不敏感 int strcmp_Wrapper(const char *s1, const char *s2, int intCaseSensitive) { if (intCaseSensitive) return !strcmp(s1, s2); else return !strcasecmp(s1, s2); } //intCaseSensitive=0大小写不敏感 int strstr_Wrapper(const char* haystack, const char* needle, int intCaseSensitive) { if (intCaseSensitive) return (int) strstr(haystack, needle); else return (int) strcasestr(haystack, needle); } pid_t GetPIDbyName_implements(const char* cchrptr_ProcessName, int intCaseSensitiveness, int intExactMatch) { char chrarry_CommandLinePath[100] ; char chrarry_NameOfProcess[300] ; char* chrptr_StringToCompare = NULL ; pid_t pid_ProcessIdentifier = (pid_t) -1 ; struct dirent* de_DirEntity = NULL ; DIR* dir_proc = NULL ; int (*CompareFunction) (const char*, const char*, int) ; if (intExactMatch) CompareFunction = &strcmp_Wrapper; else CompareFunction = &strstr_Wrapper; dir_proc = opendir(PROC_DIRECTORY) ; if (dir_proc == NULL) { perror("Couldn't open the " PROC_DIRECTORY " directory") ; return (pid_t) -2 ; } // Loop while not NULL while ( (de_DirEntity = readdir(dir_proc)) ) { if (de_DirEntity->d_type == DT_DIR) { if (IsNumeric(de_DirEntity->d_name)) { strcpy(chrarry_CommandLinePath, PROC_DIRECTORY) ; strcat(chrarry_CommandLinePath, de_DirEntity->d_name) ; strcat(chrarry_CommandLinePath, "/cmdline") ; FILE* fd_CmdLineFile = fopen (chrarry_CommandLinePath, "rt") ; //open the file for reading text if (fd_CmdLineFile) { fscanf(fd_CmdLineFile, "%s", chrarry_NameOfProcess) ; //read from /proc//cmdline fclose(fd_CmdLineFile); //close the file prior to exiting the routine if (strrchr(chrarry_NameOfProcess, '/')) chrptr_StringToCompare = strrchr(chrarry_NameOfProcess, '/') +1 ; else chrptr_StringToCompare = chrarry_NameOfProcess ; //printf("Process name: %s\n", chrarry_NameOfProcess); //这个是全路径,比如/bin/ls //printf("Pure Process name: %s\n", chrptr_StringToCompare ); //这个是纯进程名,比如ls //这里可以比较全路径名,设置为chrarry_NameOfProcess即可 if ( CompareFunction(chrptr_StringToCompare, cchrptr_ProcessName, intCaseSensitiveness) ) { pid_ProcessIdentifier = (pid_t) atoi(de_DirEntity->d_name) ; closedir(dir_proc) ; return pid_ProcessIdentifier ; } } } } } closedir(dir_proc) ; return pid_ProcessIdentifier ; } //简单实现 pid_t GetPIDbyName_Wrapper(const char* cchrptr_ProcessName) { return GetPIDbyName_implements(cchrptr_ProcessName, 0,0);//大小写不敏感} int main() { pid_t pid = GetPIDbyName_Wrapper("bash") ; // If -1 = not found, if -2 = proc fs access error printf("PID %d\n", pid); return EXIT_SUCCESS ; } 通过比较全路径,一定程度上避免了1的问题。
本文档为【Linux下C_C 查找某一进程】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_109139
暂无简介~
格式:doc
大小:41KB
软件:Word
页数:0
分类:互联网
上传时间:2019-09-02
浏览量:15