Chinaunix首页 | 论坛 | 博客
  • 博客访问: 623664
  • 博文数量: 138
  • 博客积分: 3067
  • 博客等级: 中校
  • 技术积分: 1565
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-21 12:44
文章分类

全部博文(138)

文章存档

2016年(5)

2014年(4)

2012年(1)

2011年(2)

2010年(10)

2009年(19)

2008年(97)

我的朋友

分类: C/C++

2008-07-09 18:46:17

windows process:
1. not all process's [parent process ID] > [current process ID]
2. some process's [parent process] do not exit, which means you can get a process's [parent process ID], but you can't find the process(may be just I can't find).

Note: all process have it [parent process]. current process will take another process(finally it may be [init] with PID[1]), if you killed the [parent process].

3. parent process's process info are always befoe child process's info in the processes info which you get by use command "ps -ef" on linux or "CreateToolhelp32Snapshot" on Windows.



follows are an example program , which can print process tree on Windows.
 
 
 
 
 
 
 
 

//////////////////////////////////

// file processTree.h

//////////////////////////////////

#pragma once

#include <vector>
#include <string>
#include <iostream>

class Node;
typedef std::vector<Node> NodeVector;

class Node
{
public:
    DWORD _id;
    std::string _name;
    NodeVector _nodeVector;
public:
    Node(DWORD& id, std::string& name):_id(id),_name(name){};
};

typedef struct ProcessInfo
{
    DWORD _pID;
    DWORD _ID;
    std::string _name;
    
    ProcessInfo(DWORD& pID, DWORD& ID, std::string& name):_pID(pID),_ID(ID),_name(name){};
}ProcessInfo;


typedef std::vector<ProcessInfo> ProcessVector;
class ProcessTree
{
public:
    NodeVector rootVector;
public:
    static bool getAllProcess(ProcessVector&);
    ProcessTree(ProcessVector& pv);
    bool insertNode(ProcessInfo& pi);
    Node* findNode(Node* pNode, DWORD& id);

    std::string printInfo(Node*, int);
    std::string printInfo();
    void printInfo(std::ostream &);
    friend std::ostream& operator <<(std::ostream&, ProcessTree&);
};

 

 

 

 

 

 

//////////////////////////////////

// file: processTree.cpp

//////////////////////////////////

#include <vector>
#include <algorithm>
#include <iostream>
#include <windows.h>
#include <tlhelp32.h>

#include "processTree.h"

using namespace std;

bool ProcessTree::getAllProcess(ProcessVector& pv)
{
    HANDLE hProcessSnap;
    HANDLE hProcess;
    PROCESSENTRY32 pe32;
    DWORD dwPriorityClass;

    // Take a snapshot of all processes in the system.

    hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
    if( hProcessSnap == INVALID_HANDLE_VALUE )
    {
        cout << "error\n";
        return false;
    }

    // Set the size of the structure before using it.

    pe32.dwSize = sizeof( PROCESSENTRY32 );

    // Retrieve information about the first process,

    // and exit if unsuccessful

    if( !Process32First( hProcessSnap, &pe32 ) )
    {
        cout << "error\n";
        CloseHandle( hProcessSnap ); // clean the snapshot object

        return false;
    }

    pv.clear();
    // Now walk the snapshot of processes, and

    // display information about each process in turn

    do
    {
        ProcessInfo pi(pe32.th32ParentProcessID,
            pe32.th32ProcessID,
            std::string(pe32.szExeFile));
        pv.push_back(pi);        
    } while( Process32Next( hProcessSnap, &pe32 ) );

    CloseHandle( hProcessSnap );
    return true;
}

Node* ProcessTree::findNode(Node* pNode, DWORD& id)
{
    if (pNode == NULL)
    {
        return NULL;
    }
    if (pNode->_id == id)
    {
        return pNode;
    }
    for(NodeVector::iterator it = pNode->_nodeVector.begin();
        it != pNode->_nodeVector.end(); ++it)
    {
        Node* tmpNode = findNode(&(*it), id);
        if (tmpNode != NULL)
        {
            return tmpNode;
        }
    }
    return NULL;
}

bool ProcessTree::insertNode(ProcessInfo& pi)
{
    if (rootVector.empty())
    {
        rootVector.push_back(Node(pi._ID, pi._name));
        return true;
    }

    bool findPID = false;
    for (NodeVector::iterator it = rootVector.begin();
        it != rootVector.end(); ++it)
    {
        Node* pNode = this->findNode(&(*it), pi._pID);
        if (pNode != NULL)
        {
            findPID = true;
            pNode->_nodeVector.push_back(Node(pi._ID, pi._name));
            break;
        }
    }
    if (!findPID)
    {
        rootVector.push_back(Node(pi._ID, pi._name));
        //printf("\n%d\t%d\t%s\n", pi._pID, pi._ID, pi._name.c_str());

    }
    return true;
}
ProcessTree::ProcessTree(ProcessVector& pv)
{
    for(ProcessVector::iterator it = pv.begin();it != pv.end(); ++it)
    {
        insertNode(*it);
    }
}

std::string ProcessTree::printInfo(Node* pNode, int deepth)
{
    std::string info;
    if (pNode == NULL) {
        return info;
    }
    std::string format_str = "\n";
    for(int i=0; i<deepth-1; ++i)
    {
        format_str += "\t";
        ;
    }
    if (deepth>0)
    {
        format_str += " |----";
    }
    info += format_str;
    char char_id[10];
    sprintf(char_id, "%d", pNode->_id);
    info += string(char_id);
    info += ":";
    info += pNode->_name;

    if (pNode->_nodeVector.empty())
    {
        return info;
    }
    else
    {
        std::string child_str;
        for(NodeVector::iterator it = pNode->_nodeVector.begin();
            it != pNode->_nodeVector.end(); ++it)
        {
            child_str += printInfo(&(*it), deepth+1);
        }
        return info + child_str;
    }
}
std::string ProcessTree::printInfo()
{
    std::string info;
    for(NodeVector::iterator it = rootVector.begin();
        it != rootVector.end(); ++it)
    {
        info += printInfo(&(*it), 1);
    }
    return info;
}
void ProcessTree::printInfo(std::ostream& os)
{
    os << printInfo();
}

std::ostream& operator <<(ostream& os, ProcessTree& pt)
{
    pt.printInfo(os);
    return os;
}

 

 

 

 

 

//////////////////////////////////

// file main.cpp

//////////////////////////////////

#include "processTree.h"

bool printProcessTree()
{
    ProcessVector pv;
    ProcessTree::getAllProcess(pv);
    ProcessTree pt(pv);
    std::cout <<pt << std::endl;
    return true;

}

int main(int argc, char* argv[])
{
    printProcessTree();
    return 0;
}

阅读(958) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~