Linux下C-C++查找某一进程_第1页
Linux下C-C++查找某一进程_第2页
Linux下C-C++查找某一进程_第3页
Linux下C-C++查找某一进程_第4页
Linux下C-C++查找某一进程_第5页
已阅读5页,还剩3页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Linux下C/C+查找某一进程一、前言:要在程序中启动某一程序,如果程序已经存在了,就不再启动。查找了N篇文档,有所收获,总结一下。二、实现大体分两种:1、exec或popen执行ps的命令行,然后运用某几个字符串匹配函数。#include#include#include#includeintmain()FILE*pstr;charcmd128,buff512,*p;pid_tpID;intpidnum;char*name=ping;/要查找的进程名intret=3;memset(cmd,0,sizeof(cmd);sprintf(cmd,ps-ef|grep%s,name);pstr=po

2、pen(cmd,r);/if(pstr=NULL)return1;memset(buff,0,sizeof(buff);fgets(buff,512,pstr);p=strtok(buff,);p=strtok(NULL,);这句是否去掉,取决于当前系统中ps后,进程ID号是否是第一个字段pclose(pstr);if(p=NULL)return1;if(strlen(p)=0)return1;if(pidnum=atoi(p)=0)return1;printf(pidnum:%dn,pidnum);pID=(pid_t)pidnum;ret=kill(pID,0);/这里不是要杀死进程,而是

3、验证一下进程是否真的存在,返回0表示真的存在printf(ret=%dn,ret);if(0=ret)printf(process:%sexist!n,name);elseprintf(process:%snotexist!n,name);return0;2、读取/proc文件找到个代码如下:读取/proc文件查找进程#ifndef_cplusplus/识别C/C+的宏,望文生义#define_GNU_SOURCE#endif#include#include#include/foropendir(),readdir(),closedir()#include/forstat()#ifdef_cp

4、lusplus#include#include#include#include#else#include#include#include#include#endif#definePROC_DIRECTORY/proc/TOC o 1-5 h z#defineCASE_SENSITIVE1#defineCASE_INSENSITIVE0#defineEXACT_MATCH1#defineINEXACT_MATCH0/是不是数字intIsNumeric(constchar*ccharptr_CharacterList)for(;*ccharptr_CharacterList;ccharptr_Ch

5、aracterList+)if(*ccharptr_CharacterList9)return0;/falsereturn1;/true/intCaseSensitive=0大小写不敏感intstrcmp_Wrapper(constchar*s1,constchar*s2,intintCaseSensitive)if(intCaseSensitive)return!strcmp(s1,s2);elsereturn!strcasecmp(s1,s2);/intCaseSensitive=0大小写不敏感intstrstr_Wrapper(constchar*haystack,constchar*n

6、eedle,intintCaseSensitive)if(intCaseSensitive)return(int)strstr(haystack,needle);elsereturn(int)strcasestr(haystack,needle);#ifdef_cpluspluspid_tGetPIDbyName(constchar*cchrptr_ProcessName,intintCaseSensitiveness,intintExactMatch)#elsepid_tGetPIDbyName_implements(constchar*cchrptr_ProcessName,intintC

7、aseSensitiveness,intintExactMatch)#endifcharchrarry_CommandLinePath100;charchrarry_NameOfProcess300;char*chrptr_StringToCompare=NULL;pid_tpid_ProcessIdentifier=(pid_t)-1;structdirent*de_DirEntity=NULL;DIR*dir_proc=NULL;int(*CompareFunction)(constchar*,constchar*,int);if(intExactMatch)CompareFunction

8、=&strcmp_Wrapper;elseCompareFunction=&strstr_Wrapper;dir_proc=opendir(PROC_DIRECTORY);if(dir_proc=NULL)perror(CouldntopenthePROC_DIRECTORYdirectory);return(pid_t)-2;/LoopwhilenotNULLwhile(de_DirEntity=readdir(dir_proc)if(de_DirEntity-d_type=DT_DIR)if(IsNumeric(de_DirEntity-d_name)strcpy(chrarry_Comm

9、andLinePath,PROC_DIRECTORY);strcat(chrarry_CommandLinePath,de_DirEntity-d_name);strcat(chrarry_CommandLinePath,/cmdline);FILE*fd_CmdLineFile=fopen(chrarry_CommandLinePath,rt);/openthefileforreadingtextif(fd_CmdLineFile)fscanf(fd_CmdLineFile,%s,chrarry_NameOfProcess);/readfrom/proc/cmdlinefclose(fd_C

10、mdLineFile);/closethefilepriortoexitingtheroutineif(strrchr(chrarry_NameOfProcess,/)chrptr_StringToCompare=strrchr(chrarry_NameOfProcess,/)+1;elsechrptr_StringToCompare=chrarry_NameOfProcess;/printf(Processname:%sn,chrarry_NameOfProcess);这个是全路径,比如/bin/ls/printf(PureProcessname:%sn,chrptr_StringToCom

11、pare);这个是纯进程名,比如ls这里可以比较全路径名,设置为chrarry_NameOfProcess即可if(CompareFunction(chrptr_StringToCompare,cchrptr_ProcessName,intCaseSensitiveness)pid_ProcessIdentifier=(pid_t)atoi(de_DirEntity-d_name);closedir(dir_proc);returnpid_ProcessIdentifier;closedir(dir_proc);returnpid_ProcessIdentifier;#ifdef_cplusp

12、luspid_tGetPIDbyName(constchar*cchrptr_ProcessName)returnGetPIDbyName(cchrptr_ProcessName,CASE_INSENSITIVE,EXACT_MATCH);#else/Ccannotoverloadfunctions-fixed/*这里是原文的实现,但貌似需要某些牛X的设置,因不需要,删之pid_tGetPIDbyName_Wrapper(constchar*cchrptr_ProcessName,.)intintTempArgument;intintInputArguments2;/intInputArgum

13、ents0=0;/intInputArguments1=0;memset(intInputArguments,0,sizeof(intInputArguments);intintInputIndex;va_listargptr;va_start(argptr,cchrptr_ProcessName);for(intInputIndex=0;(intTempArgument=va_arg(argptr,int)!=15;+intInputIndex)intInputArgumentsintInputIndex=intTempArgument;va_end(argptr);returnGetPID

14、byName_implements(cchrptr_ProcessName,intInputArguments0,intInputArguments1);#defineGetPIDbyName(ProcessName,.)GetPIDbyName_Wrapper(ProcessName,#_VA_ARGS_,(int)15)*/简单实现pid_tGetPIDbyName_Wrapper(constchar*cchrptr_ProcessName)returnGetPIDbyName_implements(cchrptr_ProcessName,0,0);/大小写不敏感#endifintmain

15、()pid_tpid=GetPIDbyName_Wrapper(bash);/If-1=notfound,if-2=procfsaccesserrorprintf(PID%dn,pid);returnEXIT_SUCCESS;通过比较全路径,能一定程度上避免第1种方法的问题。以下是整理后的C语言实现:#include#include#include/foropendir(),readdir(),closedir()#include/forstat()#include#include#include#include#definePROC_DIRECTORY/proc/#defineCASE_SE

16、NSITIVE1#defineCASE_INSENSITIVE0#defineEXACT_MATCH1#defineINEXACT_MATCH0/是不是数字intIsNumeric(constchar*ccharptr_CharacterList)for(;*ccharptr_CharacterList;ccharptr_CharacterList+)if(*ccharptr_CharacterList9)return0;/falsereturn1;/true/intCaseSensitive=0大小写不敏感intstrcmp_Wrapper(constchar*s1,constchar*s2

17、,intintCaseSensitive)if(intCaseSensitive)return!strcmp(s1,s2);elsereturn!strcasecmp(s1,s2);/intCaseSensitive=0大小写不敏感intstrstr_Wrapper(constchar*haystack,constchar*needle,intintCaseSensitive)if(intCaseSensitive)return(int)strstr(haystack,needle);elsereturn(int)strcasestr(haystack,needle);pid_tGetPIDb

18、yName_implements(constchar*cchrptr_ProcessName,intintCaseSensitiveness,intintExactMatch)charchrarry_CommandLinePath100;charchrarry_NameOfProcess300;char*chrptr_StringToCompare=NULL;pid_tpid_ProcessIdentifier=(pid_t)-1;structdirent*de_DirEntity=NULL;DIR*dir_proc=NULL;int(*CompareFunction)(constchar*,

19、constchar*,int);if(intExactMatch)CompareFunction=&strcmp_Wrapper;elseCompareFunction=&strstr_Wrapper;dir_proc=opendir(PROC_DIRECTORY);if(dir_proc=NULL)perror(CouldntopenthePROC_DIRECTORYdirectory);return(pid_t)-2;/LoopwhilenotNULLwhile(de_DirEntity=readdir(dir_proc)if(de_DirEntity-d_type=DT_DIR)if(I

20、sNumeric(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);/openthefileforreadingtextif(fd_CmdLineFile)fscanf(fd_CmdLineFile,%s,chrarry_NameOfProcess);/readfrom/proc/cmdlinefclose(fd_CmdLineFile);/closethefilepriortoexitingtheroutineif(strrchr(chrarry_NameOfProcess,/)chrptr_StringToCompare=strrchr(chrarry_NameOfProcess,/)+1;elsechrptr_StringToC

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论