Linux下用C语言判断程序是否已运行
2017-12-11 09:40
239 查看
通过程序名获得进程号,然后和当前程序进程号做对比。
另外一种兼容嵌入式设备。因为嵌入式设备可能没有awk命令,因此采用下面这个通用的方法。
char* getPidFromStr(const char *str)
{
static char sPID[8] = {0};
int tmp = 0;
int pos1 = 0;
int pos2 = 0;
int i = 0;
int j = 0;
for (i=0; i<strlen(str); i++) {
if ( (tmp==0) && (str[i]>='0' && str[i]<='9') ) {
tmp = 1;
pos1 = i;
}
if ( (tmp==1) && (str[i]<'0' || str[i]>'9') ) {
pos2 = i;
break;
}
}
for (j=0,i=pos1; i<pos2; i++,j++) {
sPID[j] = str[i];
}
return sPID;
}
int isRunning()
{
int ret = 0;
char sCurrPid[16] = {0};
sprintf(sCurrPid, "%d", getpid());
FILE *fstream=NULL;
char buff[1024] = {0};
if(NULL==(fstream=popen("ps -e -o pid,comm | grep a.out | grep -v PID | grep -v grep", "r")))
{
fprintf(stderr,"execute command failed: %s", strerror(errno));
return -1;
}
while(NULL!=fgets(buff, sizeof(buff), fstream)) {
char *oldPID = getPidFromStr(buff);
if ( strcmp(sCurrPid, oldPID) != 0 ) {
printf("程序已经运行,PID=%s\n", oldPID);
ret = 1;
}
}
pclose(fstream);
return ret;
}
int isRunning() { int ret = 0; char sCurrPid[16] = {0}; sprintf(sCurrPid, "%d\n", getpid()); FILE *fstream=NULL; char buff[1024] = {0}; // a.out为你的可执行程序名 if(NULL==(fstream=popen("ps -aux | grep a.out | grep -v grep | awk '{print $2}'", "r"))) { fprintf(stderr,"execute command failed: %s", strerror(errno)); return -1; } while(NULL!=fgets(buff, sizeof(buff), fstream)) { if (strlen(buff) > 0) { if (strcmp(buff, sCurrPid) !=0) { printf("%s, %s\n", buff, sCurrPid); ret = 1; break; } } } pclose(fstream); return ret; }
另外一种兼容嵌入式设备。因为嵌入式设备可能没有awk命令,因此采用下面这个通用的方法。
char* getPidFromStr(const char *str)
{
static char sPID[8] = {0};
int tmp = 0;
int pos1 = 0;
int pos2 = 0;
int i = 0;
int j = 0;
for (i=0; i<strlen(str); i++) {
if ( (tmp==0) && (str[i]>='0' && str[i]<='9') ) {
tmp = 1;
pos1 = i;
}
if ( (tmp==1) && (str[i]<'0' || str[i]>'9') ) {
pos2 = i;
break;
}
}
for (j=0,i=pos1; i<pos2; i++,j++) {
sPID[j] = str[i];
}
return sPID;
}
int isRunning()
{
int ret = 0;
char sCurrPid[16] = {0};
sprintf(sCurrPid, "%d", getpid());
FILE *fstream=NULL;
char buff[1024] = {0};
if(NULL==(fstream=popen("ps -e -o pid,comm | grep a.out | grep -v PID | grep -v grep", "r")))
{
fprintf(stderr,"execute command failed: %s", strerror(errno));
return -1;
}
while(NULL!=fgets(buff, sizeof(buff), fstream)) {
char *oldPID = getPidFromStr(buff);
if ( strcmp(sCurrPid, oldPID) != 0 ) {
printf("程序已经运行,PID=%s\n", oldPID);
ret = 1;
}
}
pclose(fstream);
return ret;
}
相关文章推荐
- [linux基础] 通过程序名字判断程序是否在运行
- Linux下判断程序是否高级权限运行
- shell脚本判断某个linux程序是否在正常运行,如果不是就启动他
- Linux 判断进程是否已经运行的程序
- Delphi 中实现判断某个外部程序是否运行(如果运行则执行一个命令否则执行一个命令)
- 判断程序是否第一次运行
- C# WINFORM判断程序是否运行,且只能运行一个实例
- 判断程序是否在VMWare内运行
- ios 判断程序是否是第一次运行
- 判断手机操作系统版本是否允许运行程序
- 监测程序详细介绍,快捷键的设置;txt文件里面一行行的读取、删除、添加字符串等;根据程序路径名以及程序名判断程序是否在运行;
- C# WINFORM判断程序是否运行,且只能运行一个实例
- C#.NET Form设置/取消开机自动运行,判断程序是否已经设置成开机自动启动
- Installshield 在安装或者卸载过程中,判断某一程序是否正在运行
- C#判断程序是否运行
- Inno 4000 Setup安装、卸载时判断是否程序正在运行,安装完成时自动打开网页的脚本
- Inno Setup安装、卸载时判断是否程序正在运行
- 判断程序是否在VMWare内运行
- linux下使用c判断文件夹是否为空的小程序
- 判断程序是否是以 管理员 身份运行 visual c++