I have have an application that forks quite a few child processes. I would like to store these child pid's in an array so when MAX_CHILD is reached. I can kill off the oldest ones.
Any way of accomplishing this ?
note... In my real application I fork and do not wait for process to finish
here is a test snippet...
---main
pid_t pid;
if ((pid = fork()) < 0) {
printf("[*]- ERROR -> Fork returned -1\n\n");
}
else if (pid == 0) {
// Success
// Child process begins here
printf("Inside Child @ PID %i . Check ps\n", pid);
sleep(30);
}
else {
// Still good here
// Maybe store pid to global array here?
printf("Back to parent PID: %i CHILD: %i\n\n", getpid(), pid);
}
// pid comes out as 0 here as expected
printf("PID: %i\n", pid);
So I would like to store pid into a int array maybe? How do you correctly cast pid_t? I want to be able to call a cleanup() function and have it step through a list of child PIDs to see if they are still active, if yes, then kill the oldest.
Any advice would be appreciated. Thank you.