Thread: How to check if a process is runing using PID

  1. #1
    Registered User
    Join Date
    Aug 2020
    Posts
    3

    How to check if a process is runing using PID

    Hello!
    I need to find 3 ways and explain the advantages and disadvantages of them..
    The first that I used is kill like that:

    Code:
    
    #include"stdio.h"
    
    
    #include<signal.h>
    
    
    #include<unistd.h>
    
    #include<errno.h>
    
    #include<stdlib.h>
    
    int main(int argc, char **argv ){
    
    long a = strtol(argv[1], NULL, 10);
    
    kill( a, 0);
    
    int b= errno;
    
    
    if(errno == 3){
    
    
    printf("Process %s does not exist \n",argv[1]);
    
    
    }
    
    
    else if(errno == 1){
    
    
    printf("Process %s exists but we have no permission \n",argv[1]);
    
    
    }
    
    
    else{
    
    
    printf("Process %s exists \n",argv[1]);
    
    
    }
    
    
    }
    I did this with kill but I have no idea how to find 2 more and explain the advantages and disadvantages

    Please help!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Here's one perhaps.
    waitpid(2): wait for process to change state - Linux man page

    There's another, but I'm keeping an air of mystery for a while.
    To see if you can find it (or someone else blurts it out).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2020
    Posts
    3
    Thanks man, this is a good way the problem is what are the advantages and disadvantages ? This is the hard part that I can't explain

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > this is a good way the problem is what are the advantages and disadvantages ?
    In the 4 minutes it took you to reply, did you write and test the code to use waitpid?

    How much more/less code did you generate?
    How easy was it for you to comprehend the manual page to achieve what you want?
    How long did it take you to get to a working program?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2020
    Posts
    3
    I have no clue on how to use it
    Everywhere I look it say that I can use -1 0 1 in the pid and I wish to put my own also there is a stract involved that I have no idea how to use
    Could you help with a sample code?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yeah, waitpid() isn't going to work.

    Unless the pid is a genuine child process, then the error return is always "No child processes" regardless of whether the process exists or not.

    Plan B - look in "/proc"

    Eg
    ls /proc/$$
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You could execute "ps -p $pid" and check the return code.
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How To Check if process is associated with invoking terminal?
    By olig1905 in forum Linux Programming
    Replies: 2
    Last Post: 11-09-2011, 09:02 AM
  2. Replies: 6
    Last Post: 01-12-2010, 10:21 PM
  3. Check Running Process Dev-C++
    By gamesplant in forum Windows Programming
    Replies: 23
    Last Post: 10-19-2009, 03:20 AM
  4. [c++] check if a process is already running
    By liquid_ice in forum Linux Programming
    Replies: 0
    Last Post: 05-12-2009, 03:30 AM
  5. how to check if a child process is done?
    By ichijoji in forum Linux Programming
    Replies: 1
    Last Post: 01-28-2005, 01:06 AM

Tags for this Thread