LINUX下用C判断一个进程是否活着

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/quantum7/article/details/83058775

  网上找了一段代码,有点问题。于是修改了,发出来。


/**
 进程名可以不等于执行文件名。
 这时要传递另外一个参数。
不考虑进程名是pts这种故意捣乱的情况。
通过ps,检查输出结果是否是进程名。

参考字串如下:
 9548 pts/19   00:00:25 gh_main

 */
int   process_check_state(const char* psProcessName)
{
    int state = -1;
    
    FILE *fstream=NULL;    
    char buff[1024] = {0};

    //用空格,是去掉类似dah_main的噪声
    sprintf(buff, "ps -A | grep \" %s\"", psProcessName); 
    if (NULL==(fstream=popen(buff, "r")))
    {
        return -1;
    }

    while (NULL != fgets(buff, sizeof(buff), fstream))
    {
        if (strlen(buff) <= 0)
        {
            break;
        }
        
        char* psHead = strstr(buff, psProcessName);
        if (psHead == NULL)
        {
            continue;
        }

        int pos = strlen(psHead)-1;
        if (psHead[pos] == '\n')
        {
            psHead[pos] = 0;
        }

        //GH_LOG_INFO("|||%s|||", psHead);
        if (!strcmp(psHead, psProcessName))
        {
            state = 0;
            break;
        }
    }

    pclose(fstream);
    
    return state;
}

 

展开阅读全文

判断一个进程是否运行

08-16

贴出代码如下:rn#include rn#include rn#include rn#include rnrnusing namespaces std;rnrnvoid main()rnrn string M_ProcessID="qq.exe";rn DWORD lpidProcesses[1024], cb = 0, cbNeeded = 0;rn HMODULE lphMods[1024];rn HANDLE hProcess = NULL;rn char szProcessID[MAX_PATH];rnrn memset(lpidProcesses, 0, sizeof(lpidProcesses));rn memset(lphMods, 0, sizeof(lphMods));rn memset(szProcessID, 0, sizeof(szProcessID));rnrn if ( !::EnumProcesses( lpidProcesses, sizeof(lpidProcesses), &cb ) )rn rn cout << " error " ;rn return ;rn rnrn for(int i=0; i< (int) (cb / sizeof(DWORD)); i++)rn rn memset(szProcessID, 0, sizeof(szProcessID));rn hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, lpidProcesses[i]);rn ::EnumProcessModules(hProcess, lphMods, sizeof(lphMods), &cbNeeded);rn ::GetModuleFileNameEx( hProcess, lphMods[0], szProcessID,sizeof(szProcessID));rn CloseHandle(hProcess);rn rnrn if (!strcmp(szProcessID,M_ProcessID))rn rn cout << "The process has opened!" ;rn rn elsern rnrn cout <<"The process need to open!" << endl;rn rnrnrnrn错误提示:rn1>c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(226) : error C2146: syntax error : missing ';' before identifier 'Internal'rn1>c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(226) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-intrn1>c:\program files\microsoft visual studio 8\vc\platformsdk\include\winbase.h(226) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-intrn……………………rn(下面都是类似的错误)rnrnwinbase.h文件中的部分:rntypedef struct _OVERLAPPED rn ULONG_PTR Internal; //226行rn ULONG_PTR InternalHigh;rn union rn struct rn DWORD Offset;rn DWORD OffsetHigh;rn ;rnrn PVOID Pointer;rn ;rnrn本人小白,rn代码也是参考网上的弄出来的,错误提示让我很不明白,应该是和是否用MFC有关……rn还望大神指导! 论坛

没有更多推荐了,返回首页