How to list the running processes in C?

Go To StackoverFlow.com

1

I want to write a console-based process manager. In the first step I think I should have a list of running proceses. I'm using Windows.

2012-04-04 15:38
by The Pianist
So do you need to know how to do this on the Windows or Mac platform? They're different beasts - Ben Zotto 2012-04-04 15:39
my project is a Windows-based, but I want to know them both - The Pianist 2012-04-04 15:41
You are asking two completely different questions. It's not fair to ask both at the same time. Pick one, sort that out, and then come back for the other - David Heffernan 2012-04-04 15:46
OK,let me know how to do it on Windows - The Pianist 2012-04-04 15:49


1

You can look at this example on MSDN. This might also help.

2012-04-04 15:59
by npclaudiu
You might also want to read some books about the WIN32 platform. I recommend the book Programming Windows by Charles Petzold - npclaudiu 2012-04-04 16:02


0

Well, since you wanted to know how to do this on mac, this can't hurt:

struct ProcessList {
    pid_t value;
    struct ProcessList *next;
};

struct ProcessList *getProcesses()
{
    struct ProcessList *process = malloc(sizeof(struct ProcessList));
    struct ProcessList *next = process;
    ProcessSerialNumber psn = { 0, kNoProcess };

    GetProcessPID(&psn, &process->value);

    while (noErr == GetNextProcess(&psn)) {
        pid_t pid;
        if (noErr == GetProcessPID(&psn, &pid)) {
            next = next->next = malloc(sizeof(struct ProcessList));
            next->value = pid;
        }
    }

    next->next = NULL;
    return process;
}

Obviously, this returns a linked list of the processes running, until you hit NULL for a process list.

2012-04-04 16:01
by Richard J. Ross III
Should I include any header file? How can I print them - The Pianist 2012-04-04 16:25
@NimaAhmadi this is for Mac, And I didn't have to include any header files when compiled in Xcode - Richard J. Ross III 2012-04-04 16:30
@NimaAhmadi excuse me, after further research, it appears that it is a part of the Carbon Framework, and you must include that in your project - Richard J. Ross III 2012-04-04 16:36


0

You can use the CreateToolhelp32Snapshot function to create a snapshot of the currently running processes. Then you can use Process32First and Process32Next to enumerate through this list.

2012-04-04 16:02
by Norbert Willhelm
may I have an example code - The Pianist 2012-04-04 16:26
This is anm example: http://msdn.microsoft.com/en-us/library/windows/desktop/ms686701(v=vs.85).aspx Ask me if you have further questions - Norbert Willhelm 2012-04-04 19:27
Ads