C Procceses/ Process ID help
What I have: A basic program for infinitely looping after and testing for an active process. It has a delay system built in to make it so it is not constantly iterating.
What I need: A way to get process IDs from other Processes other than my program. With a way to use that ID to detect if that process is active on the computer or not.
Parts of my code that need changing:
/* Code for handling the process would go here */
/* Code for detecting the target would go here */
What the goal of my program is: Perform operations to terminate the process of cmd.exe when it is active on the user's computer. Then output the status of the process and the time it took to find that process to the user.
Link(s) of tutorials that did not help:
Process Group Functions - The GNU C Library
It contained functions that I needed, but I need more information on how to apply them to other processes instead of the parent of my program and its' children.
Here is the code I prewrote for this so far:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ZERO 0
#define INFINITE 1000000000000000
#ifdef _WIN32
#define SYSTEM "Windows"
#endif // _WIN32
#ifdef linux
#define SYSTEM "Linux"
#endif // linux
#define TARGET "cmd.exe"
void Delay( int seconds );
int FindProcess(void);
int main()
{
time_t start, stop;
start = time(NULL);
int status = FindProcess();
do {
#ifdef _WIN32
system("cls");
#endif // _WIN32
#ifdef linux
system("clear");
#endif // linux
printf("%s is currently %s \n \n", TARGET ,( status==1 ) ? "ENABLED" : "DISABLED" );
Delay(2);
if ( status==1 ) {
/* Code for handling the process would go here */
stop = time( NULL );
printf("Process was found after %.0f seconds \n", difftime( start, stop ) );
start = time( NULL );
Delay(2);
}
} while ( ZERO < INFINITE ); /* Creates an almost infinite loop */
#ifdef _WIN32
system("pause>nul");
#endif // _WIN32
return 0;
}
int FindProcess(void)
{
int PROCESS=0;
/* Code for detecting the target would go here */
return PROCESS;
}
void Delay( int seconds )
{
clock_t end_of_delay = ( clock() + ( seconds * CLOCKS_PER_SEC ) );
while ( clock() < end_of_delay ) {};
}
I would appreciate the help for making this program, I am doing this as a learning experience and not homework. Thanks for your time.