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.
You can look at this example on MSDN. This might also help.
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.
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.