Thread: C Procceses/ Process ID help

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    55

    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.

  2. #2
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    Code:
    while(1);
    infinite loop

    Code:
    #include <unistd.h>
    #include <stdio.h>
    
    int main(void)
    {
       printf("My process ID : %d\n", getpid());
       printf("My parent's ID: %d\n", getppid());
    
       return 0;
    }
    thats for Linux, dont know about windows
    good luck
    Last edited by Crossfire; 03-23-2013 at 11:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. threads vs procceses
    By std10093 in forum Tech Board
    Replies: 17
    Last Post: 02-19-2013, 08:01 AM
  2. how to get process info ( to extract process thread id )
    By umen242 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2009, 01:08 PM
  3. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 02:14 PM
  4. Process sending file descriptors to another process
    By Yasir_Malik in forum C Programming
    Replies: 4
    Last Post: 04-07-2005, 07:36 PM
  5. Child Process & Parent Process Data :: Win32
    By kuphryn in forum Windows Programming
    Replies: 5
    Last Post: 09-11-2002, 12:19 PM

Tags for this Thread